Date gives one day less than actual date while converting from date to calendar

13,834

This happens because JVM on server side and JVM on client side use different time zones by default Java TimeZone:

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.

As we can see, Pacific Time Zone on server has UTC−8:00 and Indian Standard Time on client has UTC+05:30. They differ by 13.30 and Indian date X converts to US as X-13.30 what may yield a day before on server side for certain X.

Several workarounds are possible depending on how you can influence/modify your server and client application. For example, you may work with dates in UTC+00:00 time zone on both server and client sides. If you need to show a date to the user you may convert it to Indian time zone when needed.

// Set default GMT+0:00 time zone
TimeZone timeZone;
timeZone = TimeZone.getTimeZone("GMT+0:00");
TimeZone.setDefault(timeZone);

Instead of simply using Calendar cal = Calendar.getInstance(); you may create "clear" calendar which you will user later on to set day, month and year

public static Calendar createClearedCalendar() {
    Calendar cal = Calendar.getInstance();

    cal.setTimeZone(timeZone);

    cal.set(1970, 0, 1, 0, 0, 0);
    cal.set(Calendar.HOUR_OF_DAY, 0);

    cal.clear(Calendar.MILLISECOND);

    return cal;
}

By the way, if you manipulate date-time in Java you may consider Joda Time which has more extended options and optimized performance.

Share:
13,834
bharath kumar
Author by

bharath kumar

Updated on June 04, 2022

Comments

  • bharath kumar
    bharath kumar almost 2 years

    I am passing date from front end which is IST(date of indian timezone). And in java code i am converting date to calendar using the following code(This is happening in the server which is there in US PST timezone).

    Calendar cal = Calendar.getInstance();
    int offset = date.getTimezoneOffset();
    logger.info("Calendar Instance - " + cal);
    cal.setTime(date);
    logger.info("Calendar Instance after setting date - " + cal);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    logger.info("Calendar Instance after setting zeros - " + cal);
    return cal;
    

    so when i see the last log the day of the month will be one day less than what i passed.eg. if i pass 22/06/2015 IST it shifts to 21/06/2015. so after processing finally it displays 21/06/2015 in the list of data which is in another UI page.

  • gurghet
    gurghet almost 9 years
    Dont’use Joda Time, it’s not portable, use java.time instead.
  • Antonio
    Antonio almost 9 years
    You may decide whether to use Joda Time in your own project referring to this discussion as an example
  • gurghet
    gurghet almost 9 years
    that’s pretty old, java.time is much better
  • Antonio
    Antonio almost 9 years
    Please note, java.time is available since JDK1.8
  • Basil Bourque
    Basil Bourque almost 9 years
    Joda-Time inspired java.time, with the same people designing and building both. Each has features the other lacks, such as Interval in Joda-Time but not (yet?) in java.time. Joda-Time continues as an active project, running in multiple versions of Java and in Android too. So, Yes, one should start with java.time where possible but otherwise use Joda-Time. Also, look at ThreeTen-Extra project for adding features to java.time.
  • bharath kumar
    bharath kumar almost 9 years
    The timezone changes dynamically i cant take particularly like "asia/kolkata". user might access from any time zone. i tried getting timezone from date using dateobject.getTimeZoneOffset() dynamically and add it to the calendar but this is deprecated more over for marshalling they have used some jars which takes server timezone only for converting calendar to xmlgreagoriancalendar i cannot set the timezone for it.
  • Basil Bourque
    Basil Bourque almost 9 years
    Effectively, a java.util.Date has no time zone. Basically it is just a wrapper around a count of milliseconds from epoch of 1970 in UTC. There actually is a time zone deep in the source code, used internally for some things but of no use to you. The j.u.Date object gives you the key piece you need: a date-time value in UTC. Simply specify your desired/expected time zone as I showed.
  • bharath kumar
    bharath kumar almost 9 years
    There is no specific timezone i can stick. i need to get the client zone dynamically and set it. but they have used GWT for front end i am not sure how to get client zone dynamically.
  • Basil Bourque
    Basil Bourque almost 9 years
    Search StackOverflow for that issue, getting time zone from web client. Ultimately, the only sure way is to ask the user, have the user select a proper time zone name.
  • bharath kumar
    bharath kumar almost 9 years
    There is no option for user to select timezone. They can select only date from GWT date picker.