Parse date with AEDT and AEST time zone in java

时间:2016-04-07 10:48:43

标签: java date timezone

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 ?

1 个答案:

答案 0 :(得分:2)

使用java.time框架(JSR 310),您可以执行以下操作:

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

...打印:

  

2014-02-01T15:00:19 + 11:00 [澳大利亚/悉尼]

虽然为什么选择悉尼而不是墨尔本我不确定。