8

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 ?

Dileep
  • 5,362
  • 3
  • 22
  • 38
  • It's very hard to follow your question. You can easily check the time zone of a calendar by calling `getTimeZone`. If that doesn't give you enough information, please post a short but *complete* program demonstrating the problem. We have no idea what you mean by "My calendar variable shows a time as [...]" when we don't know how you're seeing that value. – Jon Skeet Dec 09 '13 at 12:56
  • 1
    @JonSkeet i have posted the result that i have checked. But i did't get the timezone – Dileep Dec 09 '13 at 12:57
  • What do you mean "I have posted the result that I have checked"? Where? Your whole question is hard to understand. – Jon Skeet Dec 09 '13 at 12:58
  • 1
    @JonSkeet 2013-12-09T00:00:00.000Z this is the value i found while debuging the Calendar variable – Dileep Dec 09 '13 at 13:04
  • @JonSkeet i was asking that the getDefault timezone is the default timezone for Date variable right.?? So what is the default timeZone value for Calendar. since both the values are not same, i belive they are taking different values as there timezone.! – Dileep Dec 09 '13 at 13:06
  • No, you weren't. You were asking what the default time zone was for Calendar, which isn't the same as Date. You never asked about the default time zone for Date, which is only actually relevant in the toString call. (A `Date` instance doesn't have a time zone; `toString` just uses the current default time zone.) And you *still* haven't said exactly what produced the "value you found" - yes, you were debugging, but that doesn't actually tell us much. A short but complete program would have been *much* more informative. – Jon Skeet Dec 09 '13 at 13:09
  • @JonSkeet sorry if you did't get my point, but i believe i posted every relevant code here . Everything here happens in 2 line so i just posted that code here. Hope i you get what i have asked – Dileep Dec 09 '13 at 13:18
  • No, you haven't posted "every relevant code". We still don't know how you're seeing 2013-12-09T00:00:00.000Z - it may well be a debugger visualization. A short but complete program would have made this question *much* simpler, as it wouldn't have relied on how you happen to be debugging your code. (We don't even know which IDE you're using.) Please read http://tinyurl.com/so-list and http://tinyurl.com/so-hints – Jon Skeet Dec 09 '13 at 13:21
  • 1
    I'll just toss out there that in no timezone is epoch 1386592669972 equal to `2013-12-09T00:00:00.000Z`. Epoch 1386592669972 is `2013-12-09T12:37:49.972Z`. You need to show exactly how you saw the first date - debugger or `toString()` or what. A [SSCCE](http://sscce.org/) would be best. – dcsohl Dec 09 '13 at 13:56
  • @JonSkeet I have made the change as instructed please take a look :) – Dileep Dec 10 '13 at 04:50
  • You've given various bits of output which I don't think are from the same run. Show the output from *one run* otherwise it's hopeless. I would also *strongly* advise you to ignore whatever bits and bobs you're seeing while debugging - it's not clear what you mean by a "cdate" value for example. It's also unclear what you're confused about now. The console output *shows* that the calendar has a time zone of US Eastern, and the `Date` output is showing EST as well - what's confusing? – Jon Skeet Dec 10 '13 at 06:46

4 Answers4

19

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 U.S. Eastern Standard Time (GMT-05:00) Indiana (East).

Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
4

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

Linga
  • 10,379
  • 10
  • 52
  • 104
3

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

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • yes the Timezones is default value is used by Date but calender and date are different they use different timezone values – Dileep Dec 09 '13 at 13:00
  • @Dileep: No, they really don't. If you believe they do, you should show that in a short but complete program demonstrating the problem, as I've asked before. – Jon Skeet Dec 09 '13 at 13:10
1

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.

dcsohl
  • 7,186
  • 1
  • 26
  • 44