代码
public String testDate(){
TimeZone.setDefault(TimeZone.getTimeZone("US/Eastern"));
Calendar fromDate = Calendar.getInstance();
Date date= new Date();
System.out.println(fromDate);
System.out.println(date);
}
在调试下面的日历变量时,我的日历变量显示日期值 2013-12-09T00:00:00.000Z
和时间值 1386649779590
。
Calendar cal = Calendar.getInstance();
我在打印对象时看到的完整日历详细信息
System.out.println(cal);
控制台
java.util.GregorianCalendar[time=1386649779590,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="US/Eastern",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=US/Eastern,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2013,MONTH=11,WEEK_OF_YEAR=50,WEEK_OF_MONTH=2,DAY_OF_MONTH=9,DAY_OF_YEAR=343,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=29,SECOND=39,MILLISECOND=590,ZONE_OFFSET=-18000000,DST_OFFSET=0]
虽然我的 java.util.date 变量将日期显示为 Mon Dec 09 07:37:50 EST 2013
,同时调试日期变量
Date date= new Date();
我设置的默认 timezone
是在程序启动时指定的 EST
TimeZone.setDefault(TimeZone.getTimeZone("US/Eastern"));
我在 timezone
IST 工作。
我的问题是
为什么 Calendar
的 cal
和 Date()
的 date
不同?
最佳答案
根据 Oracle Documentation明确提到,
public static Calendar getInstance()
Gets a calendar using the default time zone and locale. The Calendar returned is based on the current time in the default time zone with the default locale.
而默认时区
是通过public static TimeZone getDefault()
获得的,它在TimeZone.getDefault()中被提及。那个
Gets the default TimeZone for this host. The source of the default TimeZone may vary with implementation.
它将返回计算机中设置的默认
时区,除非您使用public static void setDefault(TimeZone zone)
函数设置TimeZone
明确地。
我相信上面的解释回答了你的两个问题,
- java.util.Calendar. 的默认时区是什么?
- 为什么我的日历类型变量 cal 显示的时间不是 IST 或 EST。?
编辑:根据您编辑的问题
Why is cal of Calendar and date of Date() different?
当您调用 System.out.println(date);
时,toString()
函数将被调用,如果您查看 Source Code of Date你会发现它通过调用默认时区的 displayName
函数返回时区的 3 个字母简写,在你的情况下是 3 个字母简写 EST
,即 U.S.东部标准时间 (GMT-05:00) 印第安纳州(东部)
。
关于java - java.util.Calendar 的默认时区是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20471185/