简体   繁体   English

java.util.Calendar的默认时区是什么?

[英]What is the default timezone for java.util.Calendar.?

Code

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);
}

My calendar variable shows a cdate value 2013-12-09T00:00:00.000Z and time value 1386649779590 while debugging the calendar variable below. 我的日历变量在调试下面的日历变量时显示cdate值2013-12-09T00:00:00.000Z和时间值1386649779590

Calendar cal = Calendar.getInstance();

Complete Calendar details which i have seen while printing the object 我在打印对象时看到的完整日历详细信息

System.out.println(cal);

Console 安慰

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]

While my java.util.date variable shows a date as Mon Dec 09 07:37:50 EST 2013 , while debugging the date variable 虽然我的java.util.date变量显示日期为Mon Dec 09 07:37:50 EST 2013 ,同时调试日期变量

Date date= new Date();

where as my default timezone that i have set is EST specified on program start 我设置的默认timezone是在程序启动指定的EST

TimeZone.setDefault(TimeZone.getTimeZone("US/Eastern"));

And i am working from a timezone IST . 我正在从timezone IST工作

My question is 我的问题是

Why is cal of Calendar and date of Date() different ? 为什么calCalendardateDate()有什么不同?

According to Oracle Documentation it is clearly mentioned that, 根据Oracle文档 ,明确提到,

public static Calendar getInstance() 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. 返回的日历基于默认时区中具有默认语言环境的当前时间。

And the default time zone is got by public static TimeZone getDefault() and it is mentioned in TimeZone.getDefault() that default time zonepublic static TimeZone getDefault()并在TimeZone.getDefault()中提到

Gets the default TimeZone for this host. 获取此主机的默认TimeZone。 The source of the default TimeZone may vary with implementation. 默认TimeZone的来源可能因实施而异。

It will return the default timezone set in your computer until and unless you have used public static void setDefault(TimeZone zone) function to set the TimeZone explicitly. 除非您使用public static void setDefault(TimeZone zone)函数显式设置TimeZone ,否则它将返回计算机中设置的default时区。

I believe the above explanation answers your both the question, 我相信上面的解释可以回答你的两个问题,

  1. What is the default timezone of java.util.Calendar.? java.util.Calendar的默认时区是什么?
  2. Why is my variable cal of type Calendar shows a time that is not IST or EST.? 为什么我的日历类型的变量cal显示的时间不是IST或EST。

EDIT: As per your edited question 编辑:根据您编辑的问题

Why is cal of Calendar and date of Date() different? 为什么日历的cal和Date()的日期不同?

When you call System.out.println(date); 当你调用System.out.println(date); then toString() function is invoked and if you look at Source Code of Date you will find that it returns 3 letters shorthand for timezone by invoking the displayName function of default time zone which is 3 letters shorthand in your case EST , which is US Eastern Standard Time (GMT-05:00) Indiana (East) . 然后调用toString()函数,如果你查看日期的源代码,你会发现它通过调用默认时区的displayName函数来返回时区的3个字母简写,这是你的案例EST 3个字母的简写,即US Eastern Standard Time (GMT-05:00) Indiana (East)

The date itself doesn't have any time zone. 日期本身没有任何时区。 It's toString() method uses the current default time zone to return a String 它的toString()方法使用当前的默认时区来返回String

Typically, you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running. 通常,您使用getDefault获取TimeZone ,它根据程序运行的时区创建TimeZone For example, for a program running in Japan, getDefault creates a TimeZone object based on Japanese Standard Time. 例如,对于在日本运行的程序, getDefault会根据日语标准时间创建一个TimeZone对象。

Check TimeZone for more 检查TimeZone以获取更多信息

You mention "cdate", and I notice there is a field inside the Calendar object called cdate . 你提到“cdate”,我注意到Calendar对象里面有一个名为cdate In running your sample code, I see that the cdate field is indeed initialized to 2013-12-10T00:00:00.000Z (it now being 24 hours later of course). 在运行示例代码时,我看到cdate字段确实初始化为2013-12-10T00:00:00.000Z (当然现在是24小时后)。

So? 所以? I don't know why you are looking at internal fields of a class when you are never going to directly use them. 当你永远不会直接使用它们时,我不知道你为什么要查看类的内部字段。

Your solution, then, is to ignore it. 那么,你的解决方案是忽略它。 Don't worry about the cdate field of your Calendar; 不要担心日历的cdate字段; worry about things that actually affect your program. 担心实际影响你的程序的事情。

The toString() of a Calendar is not very pretty and is intended for debugging; CalendartoString()不是很漂亮,用于调试; you should call cal.getTime() which will give you a java.util.Date that you can then print out either directly or by using a java.text.DateFormatter . 你应该调用cal.getTime() ,它会给你一个java.util.Date ,你可以直接或使用java.text.DateFormatter打印出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM