Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java simple date format british time

Tags:

java

datetime

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.

like image 217
DD. Avatar asked Jun 03 '10 11:06

DD.


People also ask

What is the date format with t?

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).

What date is yyyy mm DDThh mm SSZ?

"YYYY-MM-DDThh:mm:ss-hh:mm" is ISO 8601 format.

What is Z in SimpleDateFormat?

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.

How do you show TimeZone in formatted date in Java?

You can display timezone easily in Java using SimpleDateFormat(“z”).


1 Answers

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
like image 164
b.roth Avatar answered Oct 26 '22 23:10

b.roth