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,
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 U.S. 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With