Java Joda Time 将时区日期时间转换为毫秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18274902/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:57:11  来源:igfitidea点击:

Joda Time converting time zoned date time to milliseconds

javadatetimetimezonejodatime

提问by cianBuckley

Can somebody please explain to me why when I try to get the milliseconds of a DateTime with a zone different from my own, it gives me back the milliseconds of my local machine's time zone? I'm looking to get the milliseconds of UTC, but my local settings of the application are set to EST (even though I'm actually in Ireland ;) )

有人可以向我解释为什么当我尝试获取与我自己的区域不同的 DateTime 的毫秒数时,它会返回我本地机器时区的毫秒数吗?我希望获得 UTC 的毫秒数,但我的应用程序本地设置被设置为 EST(即使我实际上在爱尔兰;))

Here's the code:

这是代码:

  DateTimeFormatter format = DateTimeFormat.mediumDateTime();

  DateTime local = new DateTime();
  DateTime utc = new DateTime(System.currentTimeMillis(), DateTimeZone.UTC);
  System.out.println("utc zone = " +utc.getZone());
  System.out.println("UTC time: " + utc);
  long current = utc.getMillis();

  System.out.println("current: " + format.print(current));
  System.out.println("local: " + format.print(local.getMillis()));

Here's what it prints:

这是它打印的内容:

 utc zone = UTC
 UTC time: 2013-08-16T13:36:32.444Z
 current: Aug 16, 2013 9:36:32 AM
 local: Aug 16, 2013 9:36:32 AM

Currently, I would have thought it should have the same date time as UTC time?

目前,我认为它应该与 UTC 时间具有相同的日期时间?

回答by Matt Johnson-Pint

A few points:

几点:

  • You don't need to call System.currentTimeMillis(). Just pass this:
  • 你不需要打电话System.currentTimeMillis()。只需通过这个:
    DateTime utc = new DateTime(DateTimeZone.UTC);
  • When you call .getMillis()the result is alwaysin UTC. Representing time as an integer that is not UTC-based is a bad idea, and not something that Joda Time will provide. Your assessment that the milliseconds are affected by your local time zone is incorrect.

  • The conversion errors are because of how you are calling format.print. When you pass an integer, the default behavior is to use the current time zone, which is you local time. You can change it like this:

  • 当您调用时.getMillis(),结果始终为 UTC。将时间表示为不是基于 UTC 的整数是一个坏主意,而不是 Joda Time 将提供的东西。您认为毫秒受本地时区影响的评估是不正确的。

  • 转换错误是因为您调用format.print. 当您传递一个整数时,默认行为是使用当前时区,即您的本地时间。你可以像这样改变它:

    format.withZone(DateTimeZone.UTC).print(current)
  • However, if the intention is to print it as a date string, why go to milliseconds first anyway? If you pass the original DateTime, then its time zone information will be applied automatically.
  • 但是,如果打算将其打印为日期字符串,为什么还要先转到毫秒?如果您传递原件DateTime,则将自动应用其时区信息。
    format.print(utc)
    format.print(local)

Put together, the whole thing would look like this:

放在一起,整个事情看起来像这样:

DateTime local = new DateTime();
DateTime utc = new DateTime(DateTimeZone.UTC);

System.out.println("local zone = " + local.getZone());
System.out.println("  utc zone = " + utc.getZone());

DateTimeFormatter format = DateTimeFormat.mediumDateTime();
System.out.println(" local: " + format.print(local));
System.out.println("   utc: " + format.print(utc));
System.out.println("millis: " + utc.getMillis());