Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.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
Android设备时区不正确地返回GMT(00:00),并且总是以0的rawOffset返回_Android_Calendar_Timezone_Utc_Gmt - Fatal编程技术网

Android设备时区不正确地返回GMT(00:00),并且总是以0的rawOffset返回

Android设备时区不正确地返回GMT(00:00),并且总是以0的rawOffset返回,android,calendar,timezone,utc,gmt,Android,Calendar,Timezone,Utc,Gmt,过去的几天是一场噩梦,我甚至考虑卸载然后重新安装Android Studio,看看我的构建是否是问题所在 我正在做的一个应用程序需要在图片中有一个时间戳,时间戳也必须显示时区。 我试了通常的方法 Calendar now = Calendar.getInstance(); TimeZone timeZone = now.getTimeZone(); 方法,对于我所有的android测试设备,都返回GMT 00:00。我花了两天时间试图找出为什么所有stackOveflow示例都不起作用,并认为

过去的几天是一场噩梦,我甚至考虑卸载然后重新安装Android Studio,看看我的构建是否是问题所在

我正在做的一个应用程序需要在图片中有一个时间戳,时间戳也必须显示时区。 我试了通常的方法

Calendar now = Calendar.getInstance();
TimeZone timeZone = now.getTimeZone();
方法,对于我所有的android测试设备,都返回GMT 00:00。我花了两天时间试图找出为什么所有stackOveflow示例都不起作用,并认为这一定是我的构建,因为即使在设备上更改时区也会导致GMT时间返回。
在卸载之前,我遇到了解决方案。

解决方案。我找到的答案是针对另一个问题的,但它对我有效。 首先将时区设置为Null,然后选择正确的设备时区。 如果启用emulator,时区将保持UTC

        TimeZone.setDefault(null);
        System.setProperty("user.timezone", "");
        TimeZone.setDefault(null);

        //get Calendar instance
        Calendar now = Calendar.getInstance();

        //get current TimeZone using getTimeZone method of Calendar class
        TimeZone timeZone = now.getTimeZone();

        //display current TimeZone using getDisplayName() method of TimeZone class
        String strTimeZone=timeZone.getDisplayName(false,TimeZone.SHORT);
        long lngTimeZoneOffset= timeZone.getRawOffset();
我希望这能节省一些人几天的时间