Calendar.getInstance(TimeZone.getTimeZone(“UTC”))不返回UTC时间

我真的很困惑,我得到的Calendar.getInstance(TimeZone.getTimeZone("UTC"))方法调用的结果,它返回IST时间。

这是我使用的代码

 Calendar cal_Two = Calendar.getInstance(TimeZone.getTimeZone("UTC")); System.out.println(cal_Two.getTime()); 

我得到的回应是:

 Sat Jan 25 15:44:18 IST 2014 

所以我试着改变默认时区为UTC,然后我检查,然后它工作正常

 Calendar cal_Two = Calendar.getInstance(TimeZone.getTimeZone("UTC")); System.out.println(cal_Two.getTime()); TimeZone tz = TimeZone.getDefault() ; TimeZone.setDefault(TimeZone.getTimeZone("UTC")); Calendar cal_Three = Calendar.getInstance(); System.out.println(cal_Three.getTime()); TimeZone.setDefault(tz); 

结果:

 Sat Jan 25 16:09:11 IST 2014 Sat Jan 25 10:39:11 UTC 2014 

我在这里错过了什么?

System.out.println(cal_Two.getTime())调用从getTime()返回Date 。 这是转换为printlnstring的Date ,并且转换将在您的情况下使用默认的IST时区。

您需要明确使用DateFormat.setTimeZone()来打印所需时区中的Date

编辑:@Laurynas,考虑这个:

 TimeZone timeZone = TimeZone.getTimeZone("UTC"); Calendar calendar = Calendar.getInstance(timeZone); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy", Locale.US); simpleDateFormat.setTimeZone(timeZone); System.out.println("Time zone: " + timeZone.getID()); System.out.println("default time zone: " + TimeZone.getDefault().getID()); System.out.println(); System.out.println("UTC: " + simpleDateFormat.format(calendar.getTime())); System.out.println("Default: " + calendar.getTime()); 

java.util.Date独立于时区。 当您通过Calendar实例将其时区设置为UTC时打印cal_Two时, cal_Two.getTime()将返回没有时区的Date实例(并始终处于默认时区)

 Calendar cal_Two = Calendar.getInstance(TimeZone.getTimeZone("UTC")); System.out.println(cal_Two.getTime()); System.out.println(cal_Two.getTimeZone()); 

输出:

  Sat Jan 25 16:40:28 IST 2014 sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null] 

TimeZone.setDefault()的javadoc

设置getDefault方法返回的TimeZone。 如果zone为空,则将默认值重置为VM初次启动时的值。

因此,在cal_Two之前移动你的setDefault()被实例化,你将得到正确的结果。

 TimeZone.setDefault(TimeZone.getTimeZone("UTC")); Calendar cal_Two = Calendar.getInstance(TimeZone.getTimeZone("UTC")); System.out.println(cal_Two.getTime()); Calendar cal_Three = Calendar.getInstance(); System.out.println(cal_Three.getTime()); 

输出:

 Sat Jan 25 11:15:29 UTC 2014 Sat Jan 25 11:15:29 UTC 2014 
 Calendar currentTime = Calendar.getInstance(TimeZone.getTimeZone("UTC")); currentTime.set(Calendar.ZONE_OFFSET, TimeZone.getTimeZone("UTC").getRawOffset()); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, currentTime.get(Calendar.HOUR_OF_DAY)); calendar.getTimeInMillis() 

正在为我工​​作

  Following code is the simple example to change the timezone 
 public static void main(String[] args) { //get time zone TimeZone timeZone1 = TimeZone.getTimeZone("Asia/Colombo"); Calendar calendar = new GregorianCalendar(); //setting required timeZone calendar.setTimeZone(timeZone1); System.out.println("Time :" + calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND)); } 

如果你想看到时区列表,这里是follwing代码

 public static void main(String[] args) { String[] ids = TimeZone.getAvailableIDs(); for (String id : ids) { System.out.println(displayTimeZone(TimeZone.getTimeZone(id))); } System.out.println("\nTotal TimeZone ID " + ids.length); } private static String displayTimeZone(TimeZone tz) { long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset()); long minutes = TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset()) - TimeUnit.HOURS.toMinutes(hours); // avoid -4:-30 issue minutes = Math.abs(minutes); String result = ""; if (hours > 0) { result = String.format("(GMT+%d:%02d) %s", hours, minutes, tz.getID()); } else { result = String.format("(GMT%d:%02d) %s", hours, minutes, tz.getID()); } return result; } 

尝试使用GMT而不是UTC 。 他们指的是同一个时区,但是GMT这个名字更常见,而且可能会起作用。