-2

Using Java 1.5 or older what would be the best way to convert a time given in 05-Aug-15 11:30 PM BST to 2015-08-06 18:53:50Z.

The code I have is as follows:

    String ts = "05-Aug-15 11:30 PM BST";
    DateFormat df1 = new SimpleDateFormat("dd-MMM-yy HH:mm aa z");
    Date myDate = df1.parse(ts);


    String myd = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(myDate);
    System.out.println(myd);

2015-08-05T11:30:00Z

Is this the correct way to get from what is BST to UTC/Zulu.

Another issue is that the origional time is in 12:00 AM/PM but I would like to have it in 24h. This is what I get:

 05-Aug-15 10:30 PM BST New Date:2015-08-05T10:30:00Z
 05-Aug-15 10:30 AM BST New Date:2015-08-05T10:30:00Z
DevilCode
  • 1,054
  • 3
  • 35
  • 61
  • possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – JFPicard Aug 06 '15 at 18:48
  • 1
    `IS this the correct way to...` why would it not be the correct way? If it works it is not an question if it doesn't explain your issue – Kyborek Aug 06 '15 at 18:50
  • 1
    Kyborek i have amended the question to be more clear. When I convert I don't seem to get accurate 24hr format. – DevilCode Aug 06 '15 at 18:57

1 Answers1

1

Your first DateFormat includes HH which means it's 24 hour as well. Change it to something like

DateFormat df1 = new SimpleDateFormat("dd-MMM-yy hh:mm aa z");

And I also suggest you explicitly set the TimeZone (and create a df2) for output. Something like,

String ts = "05-Aug-15 11:30 PM BST";
DateFormat df1 = new SimpleDateFormat("dd-MMM-yy hh:mm aa z");
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df2.setTimeZone(TimeZone.getTimeZone("UTC"));
Date myDate = df1.parse(ts);
System.out.println(df1.format(myDate)); // <-- this is how I saw it
String myd = df2.format(myDate);
System.out.println(myd);

which outputs

05-Aug-15 11:30 PM BST
2015-08-05T22:30:00Z
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249