gpt4 book ai didi

java - 如何将当前 UTC 时间转换为 Unix 时间戳

转载 作者:行者123 更新时间:2023-12-01 21:08:24 25 4
gpt4 key购买 nike

我正在尝试获取当前的Android设备时间,并将其转换为UTC时区,然后我需要将其转换为Unix Timestamp

我用谷歌搜索,找到了一些解决方案,尝试了一些,但没有任何帮助。

这就是我现在正在做的事情。

  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() 返回 1485399980000

当我将此时间戳放入在线工具中时,它没有返回正确的输出。

请指导我如何转换电流UTC time进入UnixTimestamp

最佳答案

您需要的要简单得多:

new Date().getTime()

已经是 UTC 时间了。要获得 Linux 时间戳,您必须将其除以 1000。

关于java - 如何将当前 UTC 时间转换为 Unix 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41869178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com