Java中GMT中的毫秒数

问题描述 投票:4回答:5

我需要将毫秒转换为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);

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

java android date
5个回答
13
投票

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

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

2
投票

在转换过程中,您似乎弄乱了您的归属时区和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);

如果我正确理解您的观点,请告诉我?

干杯


1
投票

试试这个

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


1
投票

tl;dr

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

2013-07-04t 5:41:EZZ

…和…

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 [欧洲/柏林]

Details

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

java.time

如果自UTC,1970-01-01T00:00Z的1970年第一时刻的纪元参考日期起有毫秒数,则解析为InstantInstant类代表UTC时间轴上的一个时刻,分辨率为nanoseconds(小数部分最多九(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 [欧洲/柏林]


About java.time

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

现在在Joda-Timemaintenance mode项目建议迁移到java.time班。

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

从哪里获取java.time类?


0
投票

Date date = cal.getTime();

返回通过创建的日期

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.