Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
OffsetDateTime formatting and parsing

This code

String formattedDate = OffsetDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE);
OffsetDateTime.parse(formattedDate, DateTimeFormatter.ISO_OFFSET_DATE);

leads to

java.time.format.DateTimeParseException: Text '2020-11-27+01:00' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {OffsetSeconds=3600},ISO resolved to 2020-11-27 of type java.time.format.Parsed

Shouldn't this work?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

As the name suggests, OffsetDateTime needs time components (hour, minute etc.) as well. DateTimeFormatter.ISO_OFFSET_DATE does not have pattern for time components and therefore you should not use it to parse a date string into OffsetDateTime. You can build a formatter with default time components.

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String formattedDate = OffsetDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE);
        System.out.println(formattedDate);

        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                                .append(DateTimeFormatter.ISO_OFFSET_DATE)
                                .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
                                .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
                                .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
                                .toFormatter(Locale.ENGLISH);

        OffsetDateTime odt = OffsetDateTime.parse(formattedDate, dtf);
        System.out.println(odt);
        System.out.println(DateTimeFormatter.ISO_OFFSET_DATE.format(odt));
    }
}

Output:

2020-11-27Z
2020-11-27T00:00Z
2020-11-27Z
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!