Ryuzaki L
Ryuzaki L

Reputation: 40048

OffsetDateTime toString() return different format date string

I have date in this format yyyy-MM-dd'T'HH:mm:ss'Z', but while i'm parsing this using OffsetDateTime.parse(date); it returns the string by elimination seconds

Logic : Get the day from date, if it is Saturday or Sunday change day to monday and return the date String

String date = "2018-12-30T06:00:00Z";

System.out.println(date);

try {
    OffsetDateTime dateTime = OffsetDateTime.parse(date);

    System.out.println(dateTime);           //2018-12-30T06:00Z

    DayOfWeek day = dateTime.getDayOfWeek();
    // check if price change  date is Sunday or Saturday and change it to Monday
    if (day.equals(DayOfWeek.SATURDAY) || day.equals(DayOfWeek.SUNDAY)) {

        String finalDateTime = dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).toString();
         System.out.println(finalDateTime);      //2018-12-31T06:00Z
    }else {
        System.out.println(date);
    }
    }catch(Exception ex) {
        System.out.println(ex);
        System.out.println(date);
    }   

I need to return string as same input format yyyy-MM-dd'T'HH:mm:ss'Z'

Upvotes: 15

Views: 62199

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79055

'Z' is not the same as Z

'Z' is just a character literal whereas Z is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (having the timezone offset of +00:00 hours).

Therefore, do not use 'Z' in pattern for parsing or formatting.

The reason why you see seconds missing from the default format lies in the implementation of OffsetDateTime#toString. Below is an excerpt from its documentation:

The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.

You can get the desired output using an appropriate DateTimeFormatter e.g. DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssX").

Demo:

class Main {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(
            "uuuu-MM-dd'T'HH:mm:ssX", Locale.ENGLISH);

    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.parse("2018-12-30T06:00:00Z");

        // Default format
        System.out.println(odt);

        // The custom (desired) format
        String formatted = odt.format(FORMATTER);
        System.out.println(formatted);
    }
}

Output:

2018-12-30T06:00Z
2018-12-30T06:00:00Z

Online Demo

Learn more about the modern Date-Time API from Trail: Date Time.

Upvotes: 2

Karol Dowbecki
Karol Dowbecki

Reputation: 44952

As per OffsetDateTime.toString() method javadoc the shortest possible format for the value is used while omitted parts are implied to be zero. The shortest possible format for 2018-12-30T06:00:00Z is uuuu-MM-dd'T'HH:mmXXXXX so the seconds and nanos are skipped:

The output will be one of the following ISO-8601 formats:

  • uuuu-MM-dd'T'HH:mmXXXXX
  • uuuu-MM-dd'T'HH:mm:ssXXXXX
  • uuuu-MM-dd'T'HH:mm:ss.SSSXXXXX
  • uuuu-MM-dd'T'HH:mm:ss.SSSSSSXXXXX
  • uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSSXXXXX

The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.

If you need a precise format use a DateTimeFormatter instance with specific pattern to output the date:

String date = "2018-12-30T06:00:00Z";
OffsetDateTime dt = OffsetDateTime.parse(date);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
System.out.println(fmt.format(dt));

Upvotes: 32

Related Questions