Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
到目前为止的毫秒(以Java中的GMT为单位)_Java_Android_Date - Fatal编程技术网

到目前为止的毫秒(以Java中的GMT为单位)

到目前为止的毫秒(以Java中的GMT为单位),java,android,date,Java,Android,Date,我需要将毫秒转换为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

我需要将毫秒转换为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
,我相信这是正确的。所以我有两个小时的差距。有人对我的转换有什么建议/提示吗?

试试这个

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年5月15日星期二10:54:14

返回通过创建的日期

public final Date getTime() {
    return new Date(getTimeInMillis());
}
其中
getTimeInMillis()
返回没有任何时区的毫秒


我建议您在这里了解如何做您想要的事情

在转换过程中,您似乎被家庭时区和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);
请让我知道我是否正确理解你的观点


欢呼

如果看起来不正确的结果意味着
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);
tl;博士 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-01-01T00:00Z中,从1970年第一个时刻的历元参考日期算起有毫秒数,则将其解析为
瞬间
。该类表示时间线上的一个时刻,分辨率为(小数点的九(9)位)

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

要通过特定区域的挂钟时间镜头查看相同的同时时刻,请应用时区(
ZoneId
)以获得
zoneDateTime

ZoneId z = ZoneId.of( "Europe/Berlin" ) ; 
ZonedDateTime zdt = instant.atZone( z ) ;
zdt.toString():2013-07-04T07:41:33+02:00[欧洲/柏林]


关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到类

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

从哪里获得java.time类

  • ,及以后
    • 内置的
    • 标准JavaAPI的一部分,带有捆绑实现
    • Java9添加了一些次要功能和修复
    • 大部分java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类
    • 对于早期的Android,该项目采用了ThreeTen Backport(如上所述)。看

因此,要明确的是,您希望使用GMT时区将时间戳打印为字符串吗?请尝试查看以下答案:[如何将本地日期转换为GMT][1][1]:您是否考虑了夏令时?请参见这里的FYI,一些麻烦的旧日期时间类,如
java.util.date
java.util.Calendar
,和
java.text.simpleDataFormat
现在是遗留的,被类取代。大部分java.time功能都在项目中向后移植到Java6和Java7。进一步适应项目中早期的Android。请参阅。此代码不适用于我..显示IlligalArgumentException但以毫秒为单位的时间是基于GMT还是基于设备/桌面所在的时区生成的?
Instant instant = Instant.ofEpochMilli( 1_372_916_493_000L ) ;
ZoneId z = ZoneId.of( "Europe/Berlin" ) ; 
ZonedDateTime zdt = instant.atZone( z ) ;