如何将当前 UTC 时间转换为 Linux 时间戳
How to convert current UTC time into Linux Timestamp
我正在尝试获取当前 android 设备时间,并将其转换为 UTC 时区,然后我需要将其转换为 Unix Timestamp
。
我 google 它找到了一些解决方案,尝试了一些,但对我没有任何帮助。
这就是我现在正在做的。
Date date;
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
date= dateFormatLocal.parse( dateFormatGmt.format(new Date()) );
date.getTime();
输出:
- 日期(
Its returning the correct UTC date time
) Thu Jan 26 08:06:20 GMT+05:00 2017
- date.getTime() returns
1485399980000
当我把这个时间戳放在在线工具中时,它没有返回正确的输出。
请指导我如何将当前 UTC time
转换为 UnixTimestamp
你需要的更简单:
new Date().getTime()
它已经是 UTC 时间了。要获得 Linux 时间戳,您必须将其除以 1000。
我正在尝试获取当前 android 设备时间,并将其转换为 UTC 时区,然后我需要将其转换为 Unix Timestamp
。
我 google 它找到了一些解决方案,尝试了一些,但对我没有任何帮助。
这就是我现在正在做的。
Date date;
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
date= dateFormatLocal.parse( dateFormatGmt.format(new Date()) );
date.getTime();
输出:
- 日期(
Its returning the correct UTC date time
)Thu Jan 26 08:06:20 GMT+05:00 2017
- date.getTime() returns
1485399980000
当我把这个时间戳放在在线工具中时,它没有返回正确的输出。
请指导我如何将当前 UTC time
转换为 UnixTimestamp
你需要的更简单:
new Date().getTime()
它已经是 UTC 时间了。要获得 Linux 时间戳,您必须将其除以 1000。