我必须将实际的日期和时间转换为millis和其他时区GMT+3 (我的时区是GMT-2)。我使用这段代码,但它返回给我时间,但进入我的timezone....why?
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();发布于 2012-05-03 22:59:17
您需要使用SimpleDateFormat。日历始终使用计算机上默认配置的时区。下面是关于如何使用SimpleDateFormat实现此功能的example 。
发布于 2012-05-03 23:03:04
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和arg1是两个时区。请参阅此LINK
发布于 2012-05-03 22:59:48
getTime()方法同时返回。它与时区无关。
https://stackoverflow.com/questions/10433928
复制相似问题