Joda Time converting time zoned date time to milliseconds

旧时模样 提交于 2019-12-30 03:43:28

问题


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 ;) )

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?


回答1:


A few points:

  • You don't need to call System.currentTimeMillis(). Just pass this:
    DateTime utc = new DateTime(DateTimeZone.UTC);
  • When you call .getMillis() the result is always in 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:

    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.
    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());


来源:https://stackoverflow.com/questions/18274902/joda-time-converting-time-zoned-date-time-to-milliseconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!