Android获取当前时间戳?

Rja*_*jdi 232 java android timestamp epoch kotlin

我想得到当前的时间戳:1320917972

int time = (int) (System.currentTimeMillis());
Timestamp tsTemp = new Timestamp(time);
String ts =  tsTemp.toString();
Run Code Online (Sandbox Code Playgroud)

Rja*_*jdi 279

解决方案是:

Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
Run Code Online (Sandbox Code Playgroud)

  • @kjdion84 对不起,但你为什么认为这很重要?只是根据问题。 (2认同)

dro*_*oid 82

来自开发者博客:

System.currentTimeMillis()是标准的"墙"时钟(时间和日期),表示自纪元以来的毫秒数.挂钟可以由用户或电话网络设置(请参阅setCurrentTimeMillis(long)),因此时间可能会不可预测地向后或向前跳转.只有在与实际日期和时间对应很重要时,例如在日历或闹钟应用程序中,才应使用此时钟.间隔或经过时间测量应使用不同的时钟.如果您正在使用System.currentTimeMillis(),考虑听ACTION_TIME_TICK,ACTION_TIME_CHANGEDACTION_TIMEZONE_CHANGED意向广播找出时间的变化时.

  • 从http://developer.android.com/reference/java/lang/System.html#nanoTime%28%29我发现`System.nanoTime()`是`System.currentTimeMillis()`的替代品,它有没有不可预测的波动,专为测量持续时间差异而设计. (11认同)
  • 好的,谢谢,但那不解决我的问题 (5认同)
  • @ ana01"零值通常是设备上次启动的时间" - 因此只有在比较同一设备上的持续时间差异时才能使用它.例如,对数据库存储没用. (2认同)

sea*_*kej 32

1320917972是Unix时间戳,使用自1970年1月1日00:00:00 UTC以来的秒数.您可以使用TimeUnit类进行单位转换 - 从System.currentTimeMillis()秒到秒.

String timeStamp = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
Run Code Online (Sandbox Code Playgroud)


Pra*_*ani 25

您可以使用SimpleDateFormat类:

SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
String format = s.format(new Date());
Run Code Online (Sandbox Code Playgroud)

  • 这不是时间戳(自纪元以来的时间) (19认同)
  • 不是时间戳,而且根本没有效率(创建 SimpleDateFormat、创建日期、转换)。躲开它。 (2认同)
  • 您正在回答其他问题,但它也是有效的。 (2认同)

Hit*_*its 23

使用以下方法获取当前时间戳.这对我来说可以.

/**
 * 
 * @return yyyy-MM-dd HH:mm:ss formate date as string
 */
public static String getCurrentTimeStamp(){
    try {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDateTime = dateFormat.format(new Date()); // Find todays date

        return currentDateTime;
    } catch (Exception e) {
        e.printStackTrace();

        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 那不是时间戳. (35认同)

Pra*_*nav 11

它很简单:

long millis = new Date().getTime();
Run Code Online (Sandbox Code Playgroud)

如果你想要特定的格式,那么你需要像下面的格式化程序

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String millisInString  = dateFormat.format(new Date());
Run Code Online (Sandbox Code Playgroud)


ucM*_*dia 10

2023 年 11 月
这是最广为人知的方法的比较列表

乐趣 准确性 返回
System.nanoTime() 精确的
正在运行的 Java 虚拟机时间源的当前值的高分辨率(以纳秒为单位)。
System.currentTimeMillis() 高的
当前时间与 UTC 时间 1970 年 1 月 1 日午夜之间的差异(以毫秒为单位)非常有效。
Calendar.getInstance().getTimeInMillis() 高的
当前时间距纪元的 UTC 毫秒数效率低。
new Date().getTime() 高的 低效率
此日期表示自 1970 年 1 月 1 日 00:00:00 GMT 以来的毫秒数。
Instant.now().getEpochSecond() 中等的 需要 Api 26
从 1970-01-01T00:00:00Z 纪元开始的秒数。
时钟.系统.now() 高的 科特林


184*_*615 8

这是一个人类可读的时间戳,可以在文件名中使用,以防有人需要我需要的相同的东西:

package com.example.xyz;

import android.text.format.Time;

/**
 * Clock utility.
 */
public class Clock {

    /**
     * Get current time in human-readable form.
     * @return current time as a string.
     */
    public static String getNow() {
        Time now = new Time();
        now.setToNow();
        String sTime = now.format("%Y_%m_%d %T");
        return sTime;
    }
    /**
     * Get current time in human-readable form without spaces and special characters.
     * The returned value may be used to compose a file name.
     * @return current time as a string.
     */
    public static String getTimeStamp() {
        Time now = new Time();
        now.setToNow();
        String sTime = now.format("%Y_%m_%d_%H_%M_%S");
        return sTime;
    }

}
Run Code Online (Sandbox Code Playgroud)


Muj*_*han 8

您可以通过尝试以下代码在 Android 中获取当前时间戳

time.setText(String.valueOf(System.currentTimeMillis()));
Run Code Online (Sandbox Code Playgroud)

和时间戳到时间格式

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateString = formatter.format(new Date(Long.parseLong(time.getText().toString())));
time.setText(dateString);
Run Code Online (Sandbox Code Playgroud)


Ole*_*.V. 5

java.time

\n\n

我想贡献现代答案。

\n\n
    String ts = String.valueOf(Instant.now().getEpochSecond());\n    System.out.println(ts);\n
Run Code Online (Sandbox Code Playgroud)\n\n

刚才运行时的输出:

\n\n
\n

1543320466

\n
\n\n

虽然除以 1000 不会让许多人感到惊讶,但自己进行时间转换可能会很难快速阅读,因此,当您可以避免时,这是一个坏习惯。 。

\n\n

Instant是现代 Java 日期和时间 API java.time 的一部分。它\xe2\x80\x99s 内置在新的 Android 版本、API 级别 26 及更高版本上。如果您正在为旧版 Android 编程,您可能会获得向后移植,请参见下文。如果你不想这样做,可以理解的是,我仍然使用内置转换:

\n\n
    String ts = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));\n    System.out.println(ts);\n
Run Code Online (Sandbox Code Playgroud)\n\n

这与 sealskej 的答案相同。输出与之前相同。

\n\n

问:我可以在 Android 上使用 java.time 吗?

\n\n

是的,java.time 在旧版和新版 Android 设备上都能很好地工作。它只需要至少Java 6

\n\n
    \n
  • 在 Java 8 及更高版本以及较新的 Android 设备(从 API 级别 26 开始)中,现代 API 是内置的。
  • \n
  • 在非 Android Java 6 和 7 中,获取 ThreeTen Backport,即新类的向后移植(ThreeTen for JSR 310;请参阅底部的链接)。
  • \n
  • 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它\xe2\x80\x99s 称为ThreeTenABP。并确保您从以下位置导入日期和时间类org.threeten.bp子包中导入日期和时间类。
  • \n
\n\n

链接

\n\n\n

  • 几乎完美的答案(我投了赞成票),但是,至少在我看来,您应该删除 ThreeTen Backport 参考,因为这是一个关于 Android 而不是一般 Java 的问题。对于 Android 初学者来说,这可能会让人感到困惑。 (2认同)

小智 5

Kotlin 中的解决方案:

val nowInEpoch = Instant.now().epochSecond
Run Code Online (Sandbox Code Playgroud)

确保您的最低 SDK 版本为 26。