I have a web application, and if the client side (in the UK which is in timezone UTC+0000)I send the date parameter as a string like so:
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 in to a date object using SimpleDateFormater it assumes that what im saying is: "this is 10:00 o'clock UTC and im located in a UTC+0000 timezone" so it stors it as 10:00 AM UTC time instead of 09:00 AM UTC time which is the correct conversation from 10:00 AM BST (which is +0000).
here is My sample 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 issue?
this is meant to say "This is the date and time BST which is +0000 offset from UTC"
That's your problem then. BST is one hour ahead of UTC. So 11am BST is 10am UTC, hence your output. You should probably read up a bit more on UTC.
The UK is not in "timezone UTC+0000" it's in "timezone Europe/London" which is UTC+0 in Winter and UTC+1 in Summer.
(As Bogdan says, time zones are hard and Joda Time is a much better date/time library than the built-in Java one anyway... but it would give you the same answer...)
EDIT: Just to make it perfectly clear, this value "2012-03-28 10:00:00 +0000" indicates 10am UTC in any sane system. That's 11am BST, just as Java is showing you. If you're trying to make it mean something else, you should stop doing that, as you'll be at odds with just about every system known to man.
TimeZone management is one the most challenging part when building a web application :). But there are several good projects on the web that offer good support for such cases. One of them would be Joda Time
http://joda-time.sourceforge.net/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With