Sunday, October 23, 2011

Java Date Explanation

In this post, let's look into some basics about Date class which creates lots of confusion not only among beginners but even among experienced programmers. It is understandable given that even sun folks couldn't understand it correctly when they initially wrote the Date class. That is the reason the Date class has below explanation in it's java class.

Prior to JDK 1.1, the class Date had two additional functions. It allowed the interpretation of dates as year, month, day, hour, minute, and second values. It also allowed the formatting and parsing of date strings. Unfortunately, the API for these functions was not amenable to internationalization.

What I am going to discuss below is very important. It will make your understanding of dates whole lot easier. Pay special attention.

When you think of Date, think of milliseconds from epoch (January 1, 1970) . Date in java is just a wrapper around milliseconds. Let me clarify this with an example.
1:  import java.util.Date;  
2:  import java.util.TimeZone;  
3:    
4:  public class DateTest {  
5:    
6:       public static void main(String[] args) {  
7:            TimeZone.setDefault(TimeZone.getTimeZone("GMT-5"));  
8:            Date gmtMinusFiveDate = new Date();  
9:              
10:            TimeZone.setDefault(TimeZone.getTimeZone("GMT+1"));  
11:            Date gmtPlusOneDate = new Date();  
12:              
13:            TimeZone.setDefault(TimeZone.getTimeZone("GMT+7"));  
14:            Date gmtPlusSevenDate = new Date();  
15:              
16:            System.out.println("GMT-5 date milliseconds: "+gmtMinusFiveDate.getTime());  
17:            System.out.println("GMT+1 date milliseconds: "+gmtPlusOneDate.getTime());  
18:            System.out.println("GMT+7 date milliseconds: "+gmtPlusSevenDate.getTime());  
19:     }  
20:  }  

Output
GMT-5 date milliseconds: 1319391112399
GMT+1 date milliseconds: 1319391112400
GMT+7 date milliseconds: 1319391112400

Above we created Date objects in different time zones (GMT-5, GMT+1, and GMT+7) and printed their millisecond value.

Surprised that all the date instances print same value for milliseconds? (the 1 millisecond difference between 1st and 2nd line in the output is because of when in the program the date object is created). Shouldn't they be hours apart? Not really. This is how it works.

Let's say, we created a Date instance in a machine running in Arizona Timezone (GMT-7) on Oct 21, 2011 at 06:00. Think of it as below is what happens (although the machine would always have millis calculated in GMT from Jan 1, 1970 midnight)
  • Convert the time to GMT. So, Oct 21, 2011 06:00 will be converted to Oct 21, 2011 13:00 (remember GMT-7, so just add 7 hours)
  • Calculate number of milliseconds elapsed from Jan 1, 1970 to Oct 21, 2011 13:00 (lets say )
  • Create a Date object that is a wrapper around this milliseconds value.

that is the reason, regardless of where the machine is running (or what the default timezone is), the date object would only represent the number of milliseconds since January 1, 1970, 00:00:00 GMT. That's it. That is the reason all three lines in the output show the same value.

Solid understanding of the above concept is essential when one is dealing with dates in Java. If you did not understand the above, go back again and read one more time. Once you understand the above concept everything else should be easy to understand.

See my post about Parse String to Java Date and vice versa

No comments:

Post a Comment

Followers