Java中GMT中的毫秒数

我需要将毫秒转换为GMT日期(在Android应用中),例如:

1372916493000

当我通过此代码转换它:

 Calendar cal = Calendar.getInstance(); cal.setTimeZone(TimeZone.getTimeZone("GMT")); cal.setTimeInMillis(millis); Date date = cal.getTime(); 

结果是07:41 07/04/2013 。 当我使用时,结果是一样的:

 Date date = new Date(millis); 

不幸的是,结果看起来不正确,看起来像我当地的时间。 我试图通过这项服务转换相同的号码,结果是05:41 07/04/2013 ,我相信这是正确的。 所以我有两个小时的差异。 任何人都有任何建议/提示我的转换有什么问题?

如果看起来不正确的结果意味着System.out.println(date)那么毫不奇怪,因为Date.toString将date转换为本地时区的字符串表示forms。 要在GMT中查看结果,您可以使用此function

 SimpleDateFormat df = new SimpleDateFormat("hh:ss MM/dd/yyyy"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String result = df.format(millis); 

在转换过程中,您似乎弄乱了您的归属时区和UTC时区。

我们假设您在伦敦 (目前伦敦比格林威治标准时间提前1小时), 毫秒是您家乡时区的时间 (在本例中为伦敦)。

然后,你可能应该:

 Calendar cal = Calendar.getInstance(); // Via this, you're setting the timezone for the time you're planning to do the conversion cal.setTimeZone(TimeZone.getTimeZone("Europe/London")); cal.setTimeInMillis(1372916493000L); // The date is in your home timezone (London, in this case) Date date = cal.getTime(); TimeZone destTz = TimeZone.getTimeZone("GMT"); // Best practice is to set Locale in case of messing up the date display SimpleDateFormat destFormat = new SimpleDateFormat("HH:mm MM/dd/yyyy", Locale.US); destFormat.setTimeZone(destTz); // Then we do the conversion to convert the date you provided in milliseconds to the GMT timezone String convertResult = destFormat.parse(date); 

如果我正确地指出你的观点,请告诉我?

干杯

尝试这个

 public class Test{ public static void main(String[] args) throws IOException { Test test=new Test(); Date fromDate = Calendar.getInstance().getTime(); System.out.println("UTC Time - "+fromDate); System.out.println("GMT Time - "+test.cvtToGmt(fromDate)); } private Date cvtToGmt( Date date ) { TimeZone tz = TimeZone.getDefault(); Date ret = new Date( date.getTime() - tz.getRawOffset() ); // if we are now in DST, back off by the delta. Note that we are checking the GMT date, this is the KEY. if ( tz.inDaylightTime( ret )) { Date dstDate = new Date( ret.getTime() - tz.getDSTSavings() ); // check to make sure we have not crossed back into standard time // this happens when we are on the cusp of DST (7pm the day before the change for PDT) if ( tz.inDaylightTime( dstDate )) { ret = dstDate; } } return ret; } } 

测试结果
UTC时间 – 2012年5月15日星期二16:24:14 IST 2012
GMT Time – Tue May 15 10:54:14 IST 2012

TL;博士

 Instant.ofEpochMilli( 1_372_916_493_000L ) // Moment on timeline in UTC. 

2013-07-04T05:41:33Z

…和…

 Instant.ofEpochMilli( 1_372_916_493_000L ) // Moment on timeline in UTC. .atZone( ZoneId.of( "Europe/Berlin" ) ) // Same moment, different wall-clock time, as used by people in this region of Germany. 

2013-07-04T07:41:33 + 02:00 [欧洲/柏林]

细节

您正在使用现在由java.time类取代的麻烦的旧日期时间类。

java.time

如果自UTC 1970,101-01-01T00:00Z的1970年第一时刻的纪元参考日期起有毫秒数,则解析为InstantInstant类表示UTC时间轴上的一个时刻,分辨率为纳秒 (最多九(9)位小数)。

 Instant instant = Instant.ofEpochMilli( 1_372_916_493_000L ) ; 

instant.toString():2013-07-04T05:41:33Z

要通过特定区域的挂钟时间透镜查看同一时刻,请应用时区( ZoneId )以获取ZonedDateTime

 ZoneId z = ZoneId.of( "Europe/Berlin" ) ; ZonedDateTime zdt = instant.atZone( z ) ; 

zdt.toString():2013-07-04T07:41:33 + 02:00 [欧洲/柏林]


关于java.time

java.time框架内置于Java 8及更高版本中。 这些类取代了麻烦的旧遗留日期时间类,如java.util.DateCalendarSimpleDateFormat

现在处于维护模式的Joda-Time项目建议迁移到java.time类。

要了解更多信息,请参阅Oracle教程 。 并搜索Stack Overflow以获取许多示例和解释。 规范是JSR 310 。

从哪里获取java.time类?

  • Java SE 8Java SE 9及更高版本
    • 内置。
    • 带有捆绑实现的标准Java API的一部分。
    • Java 9增加了一些小function和修复。
  • Java SE 6Java SE 7
    • 许多java.timefunction都被反向移植到ThreeTen-Backport中的 Java 6和7。
  • Android的
    • 更高版本的Android捆绑java.time类的实现。
    • 对于早期的Android, ThreeTenABP项目采用ThreeTen-Backport (如上所述)。 请参见如何使用ThreeTenABP ….

Date date = cal.getTime();

返回通过创建的日期

 public final Date getTime() { return new Date(getTimeInMillis()); } 

其中getTimeInMillis()返回没有任何TimeZone的毫秒数。

我建议在这里寻找如何做你想要的如何处理calendar-timezones-using-java