简体   繁体   中英

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.

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

Date date= new Date();

where as my default timezone that i have set is EST specified on program start

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

And i am working from a timezone IST .

My question is

Why is cal of Calendar and date of Date() different ?

According to Oracle Documentation it is clearly mentioned that,

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

Gets the default TimeZone for this host. The source of the default TimeZone may vary with implementation.

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.

I believe the above explanation answers your both the question,

  1. What is the default timezone of java.util.Calendar.?
  2. Why is my variable cal of type Calendar shows a time that is not IST or EST.?

EDIT: As per your edited question

Why is cal of Calendar and date of Date() different?

When you call 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) .

The date itself doesn't have any time zone. It's toString() method uses the current default time zone to return a String

Typically, you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running. For example, for a program running in Japan, getDefault creates a TimeZone object based on Japanese Standard Time.

Check TimeZone for more

You mention "cdate", and I notice there is a field inside the Calendar object called 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).

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; worry about things that actually affect your program.

The toString() of a Calendar is not very pretty and is intended for debugging; 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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