【问题标题】:Android : convert actual date and time to millis and other timezoneAndroid:将实际日期和时间转换为毫秒和其他时区
【发布时间】:2012-05-03 14:54:10
【问题描述】:
我必须将实际日期和时间转换为毫秒和其他时区 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();
【问题讨论】:
标签:
android
time
timezone
【解决方案1】:
您需要使用 SimpleDateFormat。日历始终使用您机器上默认配置的时区。这是example 关于如何使用 SimpleDateFormat 实现此功能。
【解决方案2】:
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] 和 arg1 是两个时区。
参考这个LINK
【解决方案3】:
getTime() 方法返回相同的时间。它与时区无关。