Java: date and offset?

I have a web application and if the client side (in the UK, which is in UTC + 0000 timezone), I send the date parameter as a string like this:

date = "2012-03-28 10:00:00 +0000" // this is meant to say "This is the date and time BST which is +0000 offset from UTC"

      

but when I receive this string in my Java REST service and try to parse it into a date object using SimpleDateFormater, it assumes that what I am saying is: "it is 10:00 UTC and im located at UTC +0000 hour zone ", so it is limited to 10:00 UTC and not 09:00 UTC, which is the correct conversation from 10:00 BST (which is +0000).

here is my example Java code:

String dateString = "2012-03-28 10:00:00 +0000";
Timestamp timestamp= null;
try{
DateFormat planningDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
Date date = planningDateFormat.parse(dateString);
timestamp = new Timestamp(date.getTime());
System.out.println("Time stamp value is: " + timestamp.getTime());
System.out.println("Date value after parse: " + date);
System.out.println("Time value passed in was: " + dateString);
}
catch(Exception e){}

      

which outputs:

$ java TimeTest 
Time stamp value is: 1332917100000
Date value after parse: Wed Mar 28 11:00:00 BST 2012
Time value passed in was: 2012-03-28 10:00:00 +0000

      

How can I get around this problem?

+3


source to share


2 answers


it means to say, "This is the BST date and time, which is +0000 offset from UTC"

Then your problem. UTC is 1 hour behind BST. So 11am BST is 10am UTC, hence your exit. You should probably read UTC a bit .

The UK is not in the "UTC + 0000 time zone" in the "Europe / London time zone", which is UTC + 0 in winter and UTC + 1 in summer.



(As Bogdan says, time zones are tight and Joda Time is a much better date and time library than the built-in Java version anyway ... but that will give you the same answer ...)

EDIT: Just to be perfectly clear, this value "2012-03-28 10:00:00 +0000" means 10am UTC on any sane system. It's 11:00 BST as Java shows. If you are trying to make it mean something else, you must stop doing it, as you will be at odds with any system known to man.

+6


source


TimeZone management is one of the most difficult tasks when creating a web application :). But there are some good projects on the internet that offer good support for such cases. One of them will be Joda Time



http://joda-time.sourceforge.net/

+1


source







All Articles