Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2024-11-26T20:55:26 of type java.time.format.Parsed

简介: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2024-11-26T20:55:26 of type java.time.format.Parsed

The error message you're encountering suggests that there is an issue with converting a TemporalAccessor to an OffsetDateTime. This typically happens when the TemporalAccessor does not have enough information to create an OffsetDateTime, which requires both date and time components along with an offset from UTC.

Here are some steps to troubleshoot and resolve this issue:

  1. Check the Input String: Ensure that the input string contains all necessary components (date, time, and offset). For example, a valid OffsetDateTime string should look like 2024-11-26T20:55:26+02:00.

  2. Use Proper Formatter: Make sure you are using the correct formatter to parse the input string. The formatter should match the structure of your input string.

  3. Convert to OffsetDateTime: If you have a TemporalAccessor, you need to ensure it can be converted to OffsetDateTime. You might need to use ZonedDateTime or LocalDateTime as intermediaries if the TemporalAccessor lacks the offset information.

Here’s an example in Java that demonstrates how to handle this conversion properly:

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

public class Main {
   
    public static void main(String[] args) {
   
        String input = "2024-11-26T20:55:26"; // Example input without offset
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

        try {
   
            // Parse the input string into a TemporalAccessor
            TemporalAccessor temporalAccessor = formatter.parse(input);

            // Check if the TemporalAccessor has enough information to create an OffsetDateTime
            if (temporalAccessor instanceof LocalDateTime) {
   
                LocalDateTime localDateTime = (LocalDateTime) temporalAccessor;
                // Assuming you want to convert to a specific offset, e.g., UTC+2
                ZoneOffset offset = ZoneOffset.ofHours(2);
                OffsetDateTime offsetDateTime = localDateTime.atOffset(offset);
                System.out.println("OffsetDateTime: " + offsetDateTime);
            } else {
   
                throw new IllegalArgumentException("Input string does not contain enough information to create an OffsetDateTime");
            }
        } catch (Exception e) {
   
            e.printStackTrace();
        }
    }
}

In this example:

  • We first parse the input string into a TemporalAccessor using DateTimeFormatter.ISO_LOCAL_DATE_TIME.
  • We then check if the TemporalAccessor is an instance of LocalDateTime.
  • If it is, we convert it to OffsetDateTime by specifying a ZoneOffset.

If your input string already includes the offset, you can directly parse it into an OffsetDateTime:

import java.time.*;
import java.time.format.DateTimeFormatter;

public class Main {
   
    public static void main(String[] args) {
   
        String input = "2024-11-26T20:55:26+02:00"; // Example input with offset
        DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

        try {
   
            // Directly parse the input string into an OffsetDateTime
            OffsetDateTime offsetDateTime = OffsetDateTime.parse(input, formatter);
            System.out.println("OffsetDateTime: " + offsetDateTime);
        } catch (Exception e) {
   
            e.printStackTrace();
        }
    }
}

This approach ensures that the input string is correctly parsed into an OffsetDateTime object.

目录
相关文章
|
6月前
|
Python
【Error】DeprecationWarning: executable_path has been deprecated, please pass in a Service object
【Error】DeprecationWarning: executable_path has been deprecated, please pass in a Service object
93 2
java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parame
java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parame
|
Java Spring
【Java】Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
【Java】Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
215 0
DeprecationWarning:current URL string parser is deprecated, and will be removed in a future version.
DeprecationWarning:current URL string parser is deprecated, and will be removed in a future version.
|
缓存 Java Maven
Information:java: Errors occurred while compiling module ‘shirodemo‘
Information:java: Errors occurred while compiling module ‘shirodemo‘
188 0
|
Java Spring
Java was started but returned exit code=13
Java was started but returned exit code=13
99 0
Dx unsupported class file version 52.0 Conversion to Dalvik format failed with error 1
Dx unsupported class file version 52.0 Conversion to Dalvik format failed with error 1
151 0
java中出现Syntax error, annotations are only available if source level is 1.5 or greater
java中出现Syntax error, annotations are only available if source level is 1.5 or greater
java中出现Syntax error, annotations are only available if source level is 1.5 or greater
|
Kotlin
Program type already present: org.intellij.lang.annotations.Flow\Program type already present: org.i
Program type already present: org.intellij.lang.annotations.Flow\Program type already present: org.i
126 0
Program type already present: org.intellij.lang.annotations.Flow\Program type already present: org.i
|
Java 数据库连接
Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';
Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';
791 0