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-
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.
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.
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
How to convert Timestamp object to Calendar object and perform Calendar related functions