获取伦敦时间

发布于 2024-08-26 14:54:33 字数 352 浏览 2 评论 0原文

如何获取伦敦当前的本地挂钟时间(自 1970 年 1 月 1 日起以毫秒为单位)?由于我的应用程序可以在任何位置的服务器上运行,因此我认为我需要使用“欧洲/伦敦”时区。我还需要考虑夏令时,即应用程序应在“夏季”期间增加一个小时。

我更喜欢使用标准 java.util 库。

这是正确的吗?

TimeZone tz = TimeZone.getTimeZone("Europe/London") ;
Calendar cal = Calendar.getInstance(tz);
return cal.getTime().getTime() + tz.getDSTSavings();

谢谢

How can I get the current local wall clock time (in number of millis since 1 Jan 1970) in London? Since my application can run on a server in any location, I think I need to use a TimeZone of "Europe/London". I also need to take Daylight Savings into account i.e. the application should add an hour during the "summer".

I would prefer to use the standard java.util libraries.

Is this correct?

TimeZone tz = TimeZone.getTimeZone("Europe/London") ;
Calendar cal = Calendar.getInstance(tz);
return cal.getTime().getTime() + tz.getDSTSavings();

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风苍溪 2024-09-02 14:54:33

我不确定这个数字代表什么,因为“自 1970 年 1 月 1 日以来的毫秒数”不会根据位置或夏令时而变化。但是,也许这个计算对您有用:

TimeZone london = TimeZone.getTimeZone("Europe/London");
long now = System.currentTimeMillis();
return now + london.getOffset(now);

使用 UTC 时间或本地时间可以更好地为大多数应用程序提供服务;这实际上两者都不是。您可以像这样获取 UTC 时间和特定区域的时间:

Instant now = Instant.now(); /* UTC time */
ZonedDateTime local = now.atZone(ZoneId.of("Europe/London"));

I'm not sure what this quantity represents, since the "number of millis since 1 Jan 1970" doesn't vary based on location or daylight saving. But, perhaps this calculation is useful to you:

TimeZone london = TimeZone.getTimeZone("Europe/London");
long now = System.currentTimeMillis();
return now + london.getOffset(now);

Most applications are better served using either UTC time or local time; this is really neither. You can get the UTC time and time in a particular zone like this:

Instant now = Instant.now(); /* UTC time */
ZonedDateTime local = now.atZone(ZoneId.of("Europe/London"));
半山落雨半山空 2024-09-02 14:54:33

其他人说这样做可能不是一个好主意 - 我相信这取决于您的情况,但使用 UTC 肯定是值得考虑的事情。

然而,我认为你在这里错过了一些东西:自 1970 年 1 月 1 日以来发生的秒数UTC(这就是 Unix 纪元的定义方式 - 实际上与伦敦相同,因为该日期的偏移量为 0)可以通过以下任何表达式获得:

System.currentTimeMillis()
new Date().getTime()
Calendar.getInstance().getTime().getTime()

如果您考虑一下,自该特定时刻以来的毫秒数不会根据您所在的时区而改变哦

,还有正常的建议 - 要获得更好的日期和时间 API,请参阅 Joda Time

Others have said that it may well not be a good idea to do this - I believe it depends on your situation, but using UTC is certainly something to consider.

However, I think you've missed something here: the number of seconds which have occurred since January 1st 1970 UTC (which is how the Unix epoch is always defined - and is actually the same as in London, as the offset on that date was 0) is obtainable with any of these expressions:

System.currentTimeMillis()
new Date().getTime()
Calendar.getInstance().getTime().getTime()

If you think about it, the number of milliseconds since that particular instant doesn't change depending on which time zone you're in.

Oh, and the normal suggestion - for a much better date and time API, see Joda Time.

风筝有风,海豚有海 2024-09-02 14:54:33

要获取伦敦的当前时间:

SimpleDateFormat f = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
f.setTimeZone(TimeZone.getTimeZone("Europe/London"));
System.out.println(f.format(GregorianCalendar.getInstance().getTime()));

To get the current time in London:

SimpleDateFormat f = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
f.setTimeZone(TimeZone.getTimeZone("Europe/London"));
System.out.println(f.format(GregorianCalendar.getInstance().getTime()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文