I am using simple date format to allow users to specify which time zone they are sending data in:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,z");
This works fine: e.g.
df.parse("2009-05-16 11:07:41,GMT");
However, if someone is always sending time in London time (i.e. taking into account daylight savings), what would be the approriate time zone String to add? e.g. this doesnt work:
df.parse("2009-05-16 11:07:41,Western European Time");
System.out.println(date);
Sat May 16 12:07:41 BST 2009
I want to match the time to british time taking into daylight savings.
Thanks.
What is T between date and time? The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC). If your strings always have a “Z” you can use: SimpleDateFormat format = new SimpleDateFormat( “yyyy-MM-dd'T'HH:mm:ss).
"YYYY-MM-DDThh:mm:ss-hh:mm" is ISO 8601 format.
The Z means "zero hour offset", also known as "Zulu time" (UTC) in the ISO 8601 time representation. However, ACP 121 standard defines the list of military time zones and derives the "Zulu time" from the Greenwich Mean Time (GMT). TimeZone can be formatted in z, Z or zzzz formats.
You can display timezone easily in Java using SimpleDateFormat(“z”).
In daylight saving time, it's BST
. In the rest of the year it's GMT
.
I suggest that you use the generic name (for the whole year), which is Europe/London
. You can use something like this:
String userInput = "2009-05-16 11:07:41,Europe/London";
String[] tokens = userInput.split(",");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone(tokens[1]));
System.out.println(df.parse(tokens[0]));
The output in this case is:
Sat May 16 11:07:41 GMT+01:00 2009
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