java 在伦敦找时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2540253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Time in London
提问by dogbane
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".
如何获取伦敦当前的本地挂钟时间(自 1970 年 1 月 1 日以来的毫秒数)?由于我的应用程序可以在任何位置的服务器上运行,我想我需要使用“欧洲/伦敦”的时区。我还需要考虑夏令时,即应用程序应该在“夏季”期间增加一个小时。
I would prefer to use the standard java.util libraries.
我更喜欢使用标准的 java.util 库。
Is this correct?
这个对吗?
TimeZone tz = TimeZone.getTimeZone("Europe/London") ;
Calendar cal = Calendar.getInstance(tz);
return cal.getTime().getTime() + tz.getDSTSavings();
Thanks
谢谢
回答by erickson
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:
我不确定这个数量代表什么,因为“自 1970 年 1 月 1 日以来的毫秒数”不会因位置或夏令时而异。但是,也许这个计算对你有用:
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:
大多数应用程序最好使用 UTC 时间或本地时间;这真的不是。您可以像这样获取特定区域的 UTC 时间和时间:
Instant now = Instant.now(); /* UTC time */
ZonedDateTime local = now.atZone(ZoneId.of("Europe/London"));
回答by Jon Skeet
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.
其他人说这样做可能不是一个好主意 - 我相信这取决于你的情况,但使用 UTC 肯定是需要考虑的。
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:
但是,我认为您在这里错过了一些东西:自 1970 年 1 月 1 日UTC以来发生的秒数(这是始终定义 Unix 纪元的方式 - 实际上与伦敦相同,因为该日期的偏移量是0) 可通过以下任一表达式获得:
System.currentTimeMillis()
new Date().getTime()
Calendar.getInstance().getTime().getTime()
If you think about it, the number of milliseconds since that particular instantdoesn'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.
哦,还有正常的建议 - 要获得更好的日期和时间 API,请参阅Joda Time。
回答by Marcus Leon
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()));

