Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.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/8/logging/2.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:将实际日期和时间转换为毫秒和其他时区_Android_Time_Timezone - Fatal编程技术网

Android:将实际日期和时间转换为毫秒和其他时区

Android:将实际日期和时间转换为毫秒和其他时区,android,time,timezone,Android,Time,Timezone,我必须将实际日期和时间转换为毫秒,并转换为其他时区GMT+3(我的时区是GMT-2)。我使用这个代码,但它返回我的时间,但进入我的时区…为什么 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-3")); cal.get(Calendar.DAY_OF_MONTH); cal.get(Calendar.MONTH); cal.get(Calendar.YEAR); cal.get(Calendar.HOUR_OF_DAY)

我必须将实际日期和时间转换为毫秒,并转换为其他时区GMT+3(我的时区是GMT-2)。我使用这个代码,但它返回我的时间,但进入我的时区…为什么

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
cal.get(Calendar.DAY_OF_MONTH);
cal.get(Calendar.MONTH);
cal.get(Calendar.YEAR);
cal.get(Calendar.HOUR_OF_DAY);
cal.get(Calendar.MINUTE);
cal.get(Calendar.SECOND);
cal.get(Calendar.MILLISECOND);
long timez = cal.getTime().getTime();

您需要使用SimpleDataFormat。日历在您的计算机上始终使用默认配置的时区。下面介绍如何使用SimpleDataFormat实现此功能。

getTime()方法同时返回。它与时区无关

Date date = new Date();
  DateFormat firstFormat = new SimpleDateFormat();
  DateFormat secondFormat = new SimpleDateFormat();
  TimeZone firstTime = TimeZone.getTimeZone(args[0]);
  TimeZone secondTime = TimeZone.getTimeZone(args[1]);
  firstFormat.setTimeZone(firstTime);
  secondFormat.setTimeZone(secondTime);
  System.out.println("-->"+args[0]+": " + firstFormat.format(date));
  System.out.println("-->"+args[1]+": " + secondFormat.format(date));
  }
其中arg[0]和arg是两个时区。 参考此