我知道这一直是一个热门话题......但我找不到合适的答案。
我有以毫秒为单位的当前 UTC 时间,我需要将其与我机器中的当前时间进行比较(因此它们应该匹配)。
long myUTCtime = .....; // This reflects the UTC time
// myDate is in UTC, ok, I agree
Date myDate = new Date();
// When I use getTime() I get the localtime in ms, although not in UTC! but in DST
long milis = myDate.getTime();
// I get here a 60 minute difference
long difference = milis - myUTCtime;
我该怎么做?
提前致谢。
编辑:我不需要日历或任何东西。我没有向用户显示信息,我只想使用 UTC 时间,并且 Date 应该始终为 UTC
最佳答案
leonbloy 是正确的。您不需要进行任何偏移量计算。 Date.getTime()返回自纪元以来的毫秒数,不考虑您当前的时区。这让人们感到困惑,因为 Date.toString() 使用您本地的时区返回日期字符串。
只要您的系统时间是正确的,您就可以直接比较您的时间和 UTC 时间的长值。
关于java.util.Date 和 UTC 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5967730/