B2Z
B2Z

Reputation: 43

Java Date Time as ISO_DATE_TIME

I want to add a Z at the end of DateTimeFormatter ISO_DATE_TIME in Java not hard coded

String sample = "2018-05-11T13:35:11Z";

DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSS][XXX][X]");

DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");

LocalDateTime newLocalDateTime = LocalDateTime.parse(sample, df1);

System.out.println(newLocalDateTime.toString());

Output is:

2018-05-11T13:35:11

I want the output to be 2018-05-11T13:35:11Z

Upvotes: 4

Views: 16733

Answers (3)

Anonymous
Anonymous

Reputation: 86389

You shouldn’t use LocalDateTime. Use OffsetDateTime. And you may need no formatter.

    String sample = "2018-05-11T13:35:11Z";

    OffsetDateTime dateTime = OffsetDateTime.parse(sample)
            .withOffsetSameInstant(ZoneOffset.UTC);

    System.out.println(dateTime.toString());

Output from this snippet is the dsesired:

2018-05-11T13:35:11Z

The call to withOffsetSameInstant() makes sure that the date and time are in UTC even if the input had not been. I am using OffsetDateTime.toString() to produce the output string. I am exploiting the fact that both your sample string and your desired output are in ISO 8601 format. OffsetDateTime and the other classes from java.time parse ISO 8601 format as their default, that is, without any explicit formatter, and produce ISO 8601 format from their toString methods.

OffsetDateTime.toString() on one hand will leave out the seconds if they are 0, will on the other hand include a fraction of second if it is non-zero (all of this agress with ISO 8601). If you don’t want this, you do need a formatter. For example:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssX");
    System.out.println(dateTime.format(formatter));

You are correct, you should not hardcode Z as a literal in the format pattern string. Z signifies an offset of zero from UTC. Use format pattern letter X to output the offset so you are sure the offset is always correct. This will print an offset of zero as Z.

A LocalDateTime doesn’t keep a UTC offset, so when parsing into one your are losing information. Don’t do this. Parse into an OffsetDateTime to pick up all the information from the string.

Link: Wikipedia article: ISO 8601

Upvotes: 0

Andreas
Andreas

Reputation: 159260

If you want the output to have a time zone offset like Z, you should use OffsetDateTime or ZonedDateTime.

LocalDateTime ldt = LocalDateTime.parse("2018-05-11T13:35:11");

OffsetDateTime odt = ldt.atOffset(ZoneOffset.UTC);
System.out.println(odt); // prints: 2018-05-11T13:35:11Z

ZonedDateTime zdt = ldt.atZone(ZoneOffset.UTC);
System.out.println(zdt); // prints: 2018-05-11T13:35:11Z

As you can see, the toString() method will return the date in the format you requested.

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201537

You are calling toString() of your LocalDateTime, you should be calling format. Change

System.out.println(newLocalDateTime.toString());

to

System.out.println(newLocalDateTime.format(df1));

Outputs

2018-05-11T13:35:11Z

Upvotes: 2

Related Questions