Android:将实际日期和时间转换为毫秒和其他时区
我必须将实际日期和时间转换为毫秒,并转换为其他时区GMT+3(我的时区是GMT-2)。我使用这个代码,但它返回我的时间,但进入我的时区…为什么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)
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是两个时区。
参考此