乔达时间将时区日期时间转换为毫秒 [英] Joda Time converting time zoned date time to milliseconds

查看:119
本文介绍了乔达时间将时区日期时间转换为毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么当我尝试获取一个与我自己的时区不同的DateTime的毫秒数时,为什么还给我我本地计算机时区的毫秒数?我希望获得UTC的毫秒数,但是我在应用程序中的本地设置已设置为EST(即使我实际上是在爱尔兰;))

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

代码:

  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 time: 2013-08-16T13:36:32.444Z
 current: Aug 16, 2013 9:36:32 AM
 local: Aug 16, 2013 9:36:32 AM

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

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

推荐答案

几点:


  • 您不需要调用 System.currentTimeMillis()。只需传递以下内容即可:

  • You don't need to call System.currentTimeMillis(). Just pass this:
    DateTime utc = new DateTime(DateTimeZone.UTC);




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

    • 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.

      转换错误是由于您如何调用 format .print 。传递整数时,默认行为是使用当前时区,即您的本地时间。您可以这样更改:

      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)
      




      • 但是,如果打算将其打印为日期字符串,那么为什么仍要先走毫秒呢?如果您通过原始的 DateTime ,则其时区信息将自动应用。

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

          这篇关于乔达时间将时区日期时间转换为毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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