Java SimpleDateFormat parse Timezone like America/Los_Angeles

后端 未结 2 512
[愿得一人]
[愿得一人] 2020-12-19 15:07

I want to parse the following string in Java and convert it to a date:

DTSTART;TZID=America/Los_Angeles:20140423T120000

I tried this:

2条回答
  •  借酒劲吻你
    2020-12-19 15:40

    If you look for a solution how to parse the whole given string in one and only one step then Java 8 offers this option (the pattern symbol V is not supported in SimpleDateFormat):

    // V = timezone-id, HH instead of hh for 24-hour-clock, u for proleptic ISO-year
    DateTimeFormatter dtf = 
      DateTimeFormatter.ofPattern("'DTSTART;TZID='VV:uuuuMMdd'T'HHmmss");
    ZonedDateTime zdt = 
      ZonedDateTime.parse("DTSTART;TZID=America/Los_Angeles:20140423T120000", dtf);
    Instant instant = zdt.toInstant();
    
    // if you really need the old class java.util.Date
    Date jdkDate = Date.from(instant);
    

提交回复
热议问题