【问题标题】:How to get current UTC timestamp in seconds如何以秒为单位获取当前的 UTC 时间戳
【发布时间】:2013-10-11 09:11:11
【问题描述】:

如何获取 UTC/GMT 的当前时间戳,然后如何将此时间戳转换为秒? Itry this 但它抛出异常

    SimpleDateFormat sdfu  = new SimpleDateFormat("yyyy-MM-dd kk:mm");
    Date udate = sdfu.parse(dateAndTimeUTC);
    long timeInMillisSinceEpoch123 = udate.getTime(); 
    long durationinSeconds2 = timeInMillisSinceEpoch123 / 1000;
    System.out.println("Time in Seconds UTC: " + durationinSeconds2);

有没有更好的方法将 UTC/GMT 时间戳转换为秒?

【问题讨论】:

标签: java timestamp


【解决方案1】:

使用 Java 8+,您可以使用:

Instant.now().getEpochSecond()

【讨论】:

    【解决方案2】:

    获取当前秒系统 long timestamp = System.currentTimeMillis() / 1000;

    【讨论】:

    • @Limnic 你的说法是错误的。 Java documentation 声明“**返回:**
      当前时间与 1970 年 1 月 1 日 UTC 午夜之间的差异,以毫秒为单位。”自UTC以来的时差是UTC时间,不受时区影响。
    猜你喜欢
    • 2011-11-26
    • 2015-09-22
    • 2014-08-24
    • 1970-01-01
    • 2021-05-28
    • 2017-11-29
    • 2016-07-04
    • 2017-04-25
    相关资源
    最近更新 更多