Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with dates  RSS feed

 
Greg Abel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a swing application that uses an application server for its business logic. The tomcat server is located in EST time zone and the swing client is used in many time zones. The issue that is occurring is that on the client, dates are created and then sent to the application server to be compared with dates that are created on the application server. The issue is the time zones are not the same and dates are being changed. For example, on the client, the user would enter a date string of 4/29/15 but when a date object is created and compared on the server, it is getting changed to 4/28/15. I was wondering what the correct way would be to address this issue? Would I set the timezone to EST when the client creates a date object? I am using the SimpleDateFormat and formatting the date to MM/dd/yyyy from the client and the formatted date is sent to the server. Any help would be great.

Thanks in advance.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't store dates. Store timestamps instead. Two timestamps created at the exact same time (discounting general relativity ) on different machines with synchronized clocks should have the same value.

If you have access to Java 8, when converting between timestamps and date-like objects, please use the java.time API, and not Calendar/Date.
 
Greg Abel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to go with the structure that is already present.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then all dates must first be normalized to some shared timezone. All of the clients could use EST, but what happens when your server moves? Instead, agree on a common timezone. Usually, this is UTC.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Abel wrote:I have to go with the structure that is already present.

What exactly is the structure that is already present; what can you, and what can you not change?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/new-java/94831-dates-issue.html
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!