Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date now = cal.getTime();
看起来很简单,但是如果我在IntelliJ的调试器中检查值,我会得到不同的日期:
cal
: java.util.GregorianCalendar [time = 1405690214219,areFieldsSet = true,lenient = true,zone = GMT,firstDayOfWeek = 2,minimalDaysInFirstWeek = 4,ERA = 1,YEAR = 2014,MONTH = 6,WEEK_OF_YEAR = 29,WEEK_OF_MONTH = 3,DAY_OF_MONTH = 18,DAY_OF_YEAR = 199,DAY_OF_WEEK = 6,DAY_OF_WEEK_IN_MONTH = 3,AM_PM = 1,HOUR = 1,HOUR_OF_DAY = 13,MINUTE = 30,SECOND = 14,MILLISECOND = 219,ZONE_OFFSET = 0,DST_OFFSET = 0]
now
: Fri Jul 18 10:30:14 BRT 2014
如您所见,cal
比now
快3小时...我做错了什么?
提前致谢。
[编辑] 看起来在上面的代码之前添加TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
可以解决问题...