Java Date Api Explained

This is one area where I have seen even seasoned developers go back to documentation and basic every time they need to deal with dates. Java Date API has evolved since beginning and with new Date API in Java 8 things seem to settle down. But given that there are still applications using older versions of Java or not migrated fully, there has always been struggle working with dates. This is more for the older applications which are not using API like Joda time which is adopted in Java 8 version. 

Before we get into details and see the new Java 8 api, lets refresh our memories with the old java util date related classes and how those work-

Important classes 

1. java.util.Date – It represents specific instant or point in time. It doesn’t matter which location or timezone you are, that point in time remains same for all locations. The only difference is that local date and time could be different in different regions. Specific instant in time is calculated as milliseconds from “epoch” time which is “January 1, 1970, 00:00:00 GMT“.

Some key points about Date object – 

  • It doesn’t contain methods to represent dates as Year, Month, Day etc. All the methods related to these fields are deprecated because of internationalization issue or local calendar systems. Individual fields in date classes can be accessing via Calendar object which we will discuss below. 
  • Two Date objects can be compared together using “after” and “before” methods. This method generally compares the milliseconds present in date object to find which date is bigger than the other. 
  • Date object can be created using below two constructor methods- 

           Date() – Creates date object representing current time.

           Date(long milliseconds)  – Creates date object representing milliseconds from “epoch” time.

 1  2  3  4  5  6  7  8  9 10
import java.util.Date;  public class DateUsage {     public static void main(String[] args) {         Date date = new Date();         Date date1 = new Date(date.getTime()+1000);         System.out.println(date);         System.out.println(date1);     } } 
Output: Thu Jan 19 23:12:33 EST 2017 Thu Jan 19 23:12:34 EST 2017 

 

How to convert certain fields on the date to come up with another date? 

One way is to add or subtract milliseconds from the time object in the date object and come up with another date object (as shown in above example). This is not a convenient way and definitely wouldn’t work in lot of scenarios like how would be convert date in certain timezone to some other timezone. To make this possible, Calendar class hierarchy is available which provides methods for converting dates. 

 

2. java.util.Calendar – It’s an abstract class which provides methods for changing the date fields like day, month, year, hour, minutes etc. Java provides an implementation of Gregorian Calendar which is internationally accepted Calendar system but there are protected methods available which can be used to create a custom Calendar system. We will give an example to define our Custom Calendar later in this article. 

Note – toString method on Calendar object, prints date in timezone the jvm is running. 

How to create Calendar object 

Using default constructor – This creates a new calendar object representing current time and default time zone.

1 2 3 4 5 6 7 8 9
import java.util.Calendar; import java.util.GregorianCalendar;  public class DateUsage {     public static void main(String[] args) {         Calendar calendar = GregorianCalendar.getInstance();         System.out.println(calendar.getTime());     } } 

 
Output: Fri Feb 10 21:19:05 EST 2017 

 

3. java.sql.Timestamp – It extends java.util.Date class that allows date related fields integration with database. It’s similar to Date object in terms of holding the value of point in time  but differs in the precision. Date object holds value in the precision of milliseconds whereas Timestamp holds till the precision of nano seconds. Once the timestamp is retrieved from the database it can be converted into Gregorian Calendar and perform calendar related functions. 

 

How to convert Date object to Calendar object and perform Calendar related functions

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar;  public class DateToCalendar {     public static void main(String[] args) {          Date date = new Date();         date.setTime(date.getTime()+1000);         System.out.println(date);          // method 1         Calendar calendar = GregorianCalendar.getInstance();         calendar.setTime(date);         System.out.println(calendar.getTime());          // method 2         calendar = GregorianCalendar.getInstance();         calendar.setTimeInMillis(date.getTime());         System.out.println(calendar.getTime());      } } 
Output:
Thu Mar 23 20:44:04 EDT 2017 Thu Mar 23 20:44:04 EDT 2017 Thu Mar 23 20:44:04 EDT 2017

 

How to convert Timestamp object to Calendar object and perform Calendar related functions

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
import java.sql.Timestamp; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar;  public class TimestampToCalendar {     public static void main(String[] args) {          Date date = new Date();         Timestamp timestamp = new Timestamp(date.getTime());           date.setTime(date.getTime()+1000);         System.out.println(date);          // method         Calendar calendar = GregorianCalendar.getInstance();         calendar.setTimeInMillis(timestamp.getTime());         System.out.println(calendar.getTime());      } } 
Output: Thu Mar 23 20:47:17 EDT 2017 Thu Mar 23 20:47:16 EDT 2017 

 


Leave a Reply

Your email address will not be published. Required fields are marked *