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)
dro*_*oid 82
来自开发者博客:
System.currentTimeMillis()是标准的"墙"时钟(时间和日期),表示自纪元以来的毫秒数.挂钟可以由用户或电话网络设置(请参阅setCurrentTimeMillis(long)),因此时间可能会不可预测地向后或向前跳转.只有在与实际日期和时间对应很重要时,例如在日历或闹钟应用程序中,才应使用此时钟.间隔或经过时间测量应使用不同的时钟.如果您正在使用System.currentTimeMillis(),考虑听ACTION_TIME_TICK,ACTION_TIME_CHANGED并ACTION_TIMEZONE_CHANGED意向广播找出时间的变化时.
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)
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)
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() | 高的 | 科特林 |
这是一个人类可读的时间戳,可以在文件名中使用,以防有人需要我需要的相同的东西:
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)
您可以通过尝试以下代码在 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)
我想贡献现代答案。
\n\n String ts = String.valueOf(Instant.now().getEpochSecond());\n System.out.println(ts);\nRun Code Online (Sandbox Code Playgroud)\n\n刚才运行时的输出:
\n\n\n\n\n1543320466
\n
虽然除以 1000 不会让许多人感到惊讶,但自己进行时间转换可能会很难快速阅读,因此,当您可以避免时,这是一个坏习惯。 。
\n\n这Instant是现代 Java 日期和时间 API java.time 的一部分。它\xe2\x80\x99s 内置在新的 Android 版本、API 级别 26 及更高版本上。如果您正在为旧版 Android 编程,您可能会获得向后移植,请参见下文。如果你不想这样做,可以理解的是,我仍然使用内置转换:
String ts = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));\n System.out.println(ts);\nRun Code Online (Sandbox Code Playgroud)\n\n这与 sealskej 的答案相同。输出与之前相同。
\n\n是的,java.time 在旧版和新版 Android 设备上都能很好地工作。它只需要至少Java 6。
\n\norg.threeten.bp子包中导入日期和时间类。java.time.java.time。java.time到 Java 6 和 7(ThreeTen for JSR-310)。小智 5
Kotlin 中的解决方案:
val nowInEpoch = Instant.now().epochSecond
Run Code Online (Sandbox Code Playgroud)
确保您的最低 SDK 版本为 26。