0

I am using a gwt application. The server in which my application is deployed and the database it is using is all in paris (france). In the application I have a provision to enter any date which will be saved to db.

When my application is launched from paris, everything is working fine. But when the same application is launched from India, date entered on the screen is stored as 1 day less than what was entered. For example, in India I enter 16/07/2014 theb the date is stored as 15/07/2014.

I suspect some geographical problem. Is there any code changes I need to do to save the date accommodation to the timezone? Or any to do with the system date on the computer the user is using the application?

codeguru
  • 163
  • 2
  • 13

2 Answers2

1

My favorite approach is to save dates as Long values adjusted to midnight in GMT:

Long dateInMills = new Date().getTime();
dateInMills -= dateInMills % (24 * 60 * 60 * 1000));

// to show this date you can use, for example
DateTimeFormat.getFormat(PredefinedFormat.DATE_MEDIUM).format(new Date(dateInMills));

When you show this date in GWT, it will use the default time zone of a browser to display it.

If you want to have more control over it, for example to allow a user with a one time zone in a browser to see dates with a different time zone, you will have to pass the desired TimeZone object to DateTimeFormat#format method.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • While many people do this (track time as count from epoch), I have to disagree. Makes debugging and sanity-checking difficult if not impossible. Problematic values are no longer obvious. Can be confused as different tools/environments use different epochs and different count resolution (whole seconds, milliseconds, microseconds, and nanoseconds). Java libraries, JDBC drivers, and databases are all built to handle the nitty-gritty detail of tracking the date-time, so let them. Do you perform bit-level arithmetic? Work with text by octets? We have classes and libraries to do that work for us. – Basil Bourque Oct 22 '14 at 07:08
  • Every problem can be over-engineered if you have enough time on your hands. For most web-based apps, however, storing dates as Long values and displaying them using standard GWT DateTimeFormat object works perfectly fine. – Andrei Volgin Oct 22 '14 at 07:28
-1

Study Up

You should do a bit of study on how tracking date-time on computers works. Start with this question, Daylight saving time and time zone best practices. Do some other searching on StackOverflow to find many helpful discussions and code examples.

Basically your question is a duplicate of many others.

Unclear Question

Your question does not give enough information to help us help you. Exactly how does your GWT app capture the date-time from the user, in exactly what data type? From exactly where does the server store the date-time value and using exactly what data type?

General Tips

  • Never trust the date-time on a client machine. Always use your own server for current moment, if at all possible.

  • Servers should be set to a time zone of UTC, or if not physically possible in your OS, then set to a time zone of Reykjavík, Iceland.

  • Keep all your business logic and data storage in UTC. Convert to a time zone only as needed for presentation to the user.

  • Never rely on the implicit use of the JVM’s current default time zone. Always specify the desired/expected time zone.

  • Never use the java.util.Date/.Calendar classes. They are notoriously troublesome, confusing, and flawed. Instead use either Joda-Time or the java.time package built into Java 8 (inspired by Joda-Time).

  • If serializing the date-time value to text, use the ISO 8601 standard format such as 2014-10-21T07:11:31.028Z.

Example code in Joda-Time 2.5.

DateTimeZone zoneParis = DateTimeZone.forID( "Europe/Paris" );
DateTimeZone zoneKolkata = DateTimeZone.forID( "Asia/Kolkata" );

DateTime nowUtc = DateTime.now( DateTimeZone.UTC );
DateTime nowParis = now.withZone( zoneParis );
DateTime nowKolkata = now.withZone( zoneKolkata );

Joda-Time and java.time both parse and generate strings in ISO 8601 format by default.

String output = nowUtc.toString();

If required, convert to java.util.Date.

java.util.Date date = newUtc.toDate();
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    GWT does not support neither Joda nor Java 8 at the moment. – Andrei Volgin Oct 22 '14 at 07:24
  • @AndreiVolgin (A) The author did not really identify the source and handling of the data, as noted in the top of my answer. (B) The question carries a `java` tag. (C) There are ways to communicate Java values including Joda-Time/java.time with GWT, such as the [Vaadin](http://www.Vaadin.com/) framework. – Basil Bourque Oct 22 '14 at 07:28
  • 2
    I think it's important to consider all relevant tags when offering an answer. – Andrei Volgin Oct 22 '14 at 07:32