Jodatime转换时间划分日期时间到millis [英] Jodatime converting time zoned date time to millis

查看:130
本文介绍了Jodatime转换时间划分日期时间到millis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么当我试图获得一个与我自己的区域不同的DateTime的毫秒时,它会让我回到我本机的时区的毫秒数?我正在寻求UTC的毫秒数,但我的本地应用程序设置设置为EST(即使我实际上是在爱尔兰)。



这里是代码:

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

这是打印的:

  utc zone = UTC 
UTC时间:2013-08-16T13:36:32.444Z
当前:2013年8月16日9:36:32 AM
本地:2013年8月16日9:36:32 AM

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

解决方案

几点:




  • 您不需要调用 System.currentTimeMillis()。只需传递:



  DateTime utc = new DateTime(DateTimeZone 。世界标准时间); 




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


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




  format.withZone(DateTimeZone.UTC).print(current)




  • 如果打算把它打印成一个日期字符串,为什么要先毫秒呢?如果您通过原始的 DateTime ,则会自动应用其时区信息。



  format.print(utc)
format.print(local)

整体来说,整体看起来像这样:

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


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?

解决方案

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

这篇关于Jodatime转换时间划分日期时间到millis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆