Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java LocalDateTime: Why is Unsupported field: OffsetSeconds produced?

Tags:

java

I'm trying to parse the below processingdate so that I can eventually end up with a date of format 2020-11-10T10:43:07+00:00. How can I amend the below to produce this?

 String processingDate = "2020-11-24";
    LocalDateTime dt =
        LocalDateTime.of(
            LocalDate.parse(
                processingDate, DateTimeFormatter.ISO_DATE),
            LocalTime.now());

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

java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds
like image 593
IveGotCodeButImNotACoder Avatar asked Dec 30 '25 00:12

IveGotCodeButImNotACoder


1 Answers

A local datetime doesn't contain any concept of zones or offsets. You are trying to print the offset to a string by using XXX at the end of the pattern.

But anyway, there is no need to go from a LocalDateTime -> String -> OffsetDateTime.

Just do

dt.atOffset(ZoneOffset.UTC)

or whatever other offset you want

like image 71
Michael Avatar answered Dec 31 '25 18:12

Michael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!