skumar
skumar

Reputation: 1015

dateTimeFormatter short time-zone name not returned

The short timezone name is not returning for the below code in java8, it returns "-08:00" instead

ZonedDateTime dateTime1 = ZonedDateTime.parse("2020-01-22T08:07:59.179-08:00");
ZoneId.of("America/Los_Angeles");
System.out.println(dateTime1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS z")));

this outputs: 2020-01-22 08:07:59.179 -08:00

Please let know which format input will produce "2020-01-22 08:07:59.179 PST"

Upvotes: 2

Views: 347

Answers (1)

Scratte
Scratte

Reputation: 3166

You may need to add the zoneId to your date:

import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class StackOverflowTest {
  public static void main(String[] args){

    ZonedDateTime dateTime1 =
       ZonedDateTime.parse("2020-01-22T08:07:59.179-08:00")
      .withZoneSameInstant(ZoneId.of("America/Los_Angeles"));

    ZonedDateTime dateTime2 = // I'm at CET
       ZonedDateTime.parse("2020-01-22T08:07:59.179+01:00")
      .withZoneSameInstant(ZoneId.of("Europe/Berlin"));

    System.out.println(dateTime1.format(DateTimeFormatter
                                       .ofPattern("yyyy-MM-dd HH:mm:ss.SSS z")));
    System.out.println(dateTime2.format(DateTimeFormatter
                                       .ofPattern("yyyy-MM-dd HH:mm:ss.SSS z")));

/* prints
2020-01-22 08:07:59.179 GMT-08:00
2020-01-22 08:07:59.179 CET
*/
  }
}

Upvotes: 7

Related Questions