Monday, 14 April 2014

Understanding and handling java.util.Date and time concept in Java.

Date and Time need do definitions and explanations but it is very crucial how they are handled programmatically. It is so because time is different in different countries. To understand these differences in time, it is divided into various time zones. You can synchronize your time based on which timezone your country lies in.

What difference does it make?

Lets consider you have an online calender which stored all the birthdays of all your contacts. Lets say you are from Bangalore(India) and it is past 12 which means it's time to celebrate you best friends birthday. You will expect it to be shown in the app that connect to the online calender. But lets say the server which hosts the online calender is in Washington DC(USA) and there it is still 2 P.M. So your friends birthday is not until next 10 hours? Absolutely not. You want to celebrate it right away :)

So how do we resolve the difference?

To resolve this difference a standard time is defined known as UTC(Coordinated Universal Time). Most of the time zones on land are offset from Coordinated Universal Time (UTC) by a whole number of hours (UTC−12 to UTC+14).

As per wiki

Coordinated Universal Time (French: Temps Universel Coordonné, UTC) is the primary time standard by which the world regulates clocks and time. It is one of several closely related successors to Greenwich Mean Time (GMT). For most purposes, UTC is synonymous with GMT, but GMT is no longer precisely defined by the scientific community.

Even if you see you computers time you will find it is set to some time zone. For example my laptop is configured as follows - 



So simple solution to above problem is let your servers run on UTC time zones. Now for request from each country convert the time from the countries time zone to UTC and then do a normal comparison with servers time(which is already in UTC). So for example if the request is coming from India with time shown above I would deduct 5.30 hours from the time stamp to covert it into UTC and then compare/process. Also it is generally preferred to convert the time to UTC on client itself and send the time stamp in UTC to the server instead of doing the conversion on server.

Operating on Date in Java

In java Date is in java.util package.  Simplest way to print current date is

    public static void main(String args[]){

        System.out.println(new Date()); //or
        System.out.println(new Date(System.currentTimeMillis()));


Now lets say you are given a time stamp and you have to check if it is older than 90 days or not. Also lets consider this comparison happens on server side and that the timestamp received as well as the server time is in UTC. So no need to handle time zones explicitly.

So lets say you have time stamp(String) like
Thu Sep 28 20:29:30 JST 2000

Now you have to check if this date is older than 90 days. We know it is but lets see how it is done programmatically. 

    public static void main(String args[]) throws ParseException {

        String target = "Thu Sep 28 20:29:30 JST 2000";
        DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
        Date result =  df.parse(target);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -90);
        Date dateBefore30Days = cal.getTime();

        if(result.compareTo(dateBefore30Days) <= 0){
            System.out.println("Time stamp is older than 90 days");
        else {
            System.out.println("Time stamp is not older than 90 days");


You know the answer but I hope you get the point. Also note that Date class implements Comparable interface due to which you can use compareTo() method.

Related Links

t> UA-39527780-1 back to top