Parameswar
Parameswar

Reputation: 2069

Print ZonedDateTime exactly as it is parsed

I have a date string in a format:

String date = 2014-05-05T05:05:00.000 ZonedDateTime zoneDateTime = LocalDateTime.parse(date).atZone(ZoneId.of("UTC");

the above prints:

2014-05-05T05:05Z[UTC]

Is there any way we can print it in the following format ?

2014-05-05T05:05:00.000Z

In joda time, i can easily do this:

DateTime datetime= org.joda.time.LocalDateTime.parse(date).toDateTime(UTC)

Upvotes: 1

Views: 333

Answers (1)

NullPointer
NullPointer

Reputation: 7368

Is there any way we can print it in the following format ?

String date = 2014-05-05T05:05:00.000

You can use java.time.format.DateTimeFormatter.ofPattern().

        String date = "2014-05-05T05:05:00.000";
        ZonedDateTime zoneDateTime = LocalDateTime.parse(date).atZone(ZoneId.of("UTC"));
        System.out.println(zoneDateTime);
        System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX").format(zoneDateTime));

Refrence: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Upvotes: 1

Related Questions