Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse date with AEDT and AEST time zone in java

I am trying to parse a string of format

Thu Apr 07 11:45:28 AEST 2016

into date object. My code looks like following:

SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
try{
    Date time = parserSDF.parse("Sat Feb 01 15:00:19 AEDT 2014");
}catch(Exception e){
    e.printStackTrace();
}

But I am getting a 'parse error'. I cannot change the input format of the date and I also cannot set my timezone to a static value as this code is to be run on andorid device. How can I parse this string into date ?

like image 994
Rahul Avatar asked Apr 16 '26 11:04

Rahul


1 Answers

Using the java.time framework (JSR 310), you can do:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss zzz yyyy");
ZonedDateTime zdt = ZonedDateTime.parse("Sat Feb 01 15:00:19 AEDT 2014", dtf);
System.out.println(zdt);

…which prints:

2014-02-01T15:00:19+11:00[Australia/Sydney]

Though why it picks Sydney instead of Melbourne I am not sure.

like image 192
Peter Lawrey Avatar answered Apr 18 '26 01:04

Peter Lawrey