Skip to content

Allow specifying custom DateTimeFormatter for OffsetDateTime ser/deser (new constructors?) #376

Closed

Description

@ZIRAKrezovic

Trying to make OffsetDateTime deserialize from ISO_LOCAL_DATE_TIME pattern by appending default offset of "0". This is easily doable with

    public static final DateTimeFormatter OFFSET_DATE_TIME_PARSER =
            new DateTimeFormatterBuilder()
                    .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
                    .optionalStart()
                    .parseLenient()
                    .appendOffsetId()
                    .parseStrict()
                    .optionalEnd()
                    .parseDefaulting(ChronoField.OFFSET_SECONDS, 0)
                    .toFormatter();

        var parsed = OffsetDateTime.parse("2025-01-01T22:01:05", OFFSET_DATE_TIME_PARSER);

However, there is no easy way to override this parser, especially since InstantDeserializer.ISO_OFFSET_DATE_TIME is created with many parameters and calls methods that are private to the InstantDeserializer.

When looking at LocalDateTimeDeserializer it as a public constructor that accepts DateTimeFormatter as an arg.

Similar situation happens in OffsetDateTimeSerializer. I wish to only display 3 digits of nano-second instead of 9, but there's no right way to override DateTimeFormatter in OffsetDateTimeSerializer like there's in LocalDateTimeSerializer

Would you kindly support this use-case?

Activity

  1. added
    pr-welcomeIssue for which progress most likely if someone submits a Pull Request
    on Oct 23, 2025
  2. cowtowncoder commented on Oct 23, 2025

    @cowtowncoder
    Member

    Adding accessible constructor sounds like a reasonable idea.

    PRs welcome!

  3. changed the title [-]Allow specifying OffsetDateTime parser for Serialization/Deserialization[/-] [+]Allow specifying custom `DateTimeFormatter` for `OffsetDateTime` ser/deser (new constructors?)[/+] on Oct 23, 2025
  4. cowtowncoder commented on Oct 23, 2025

    @cowtowncoder
    Member

    NOTE: depending on implementation, pr either here (2.x) or jackson-databind (3.x) -- since functionality embedded in databind for Jackson 3.

  5. added a commit that references this issue on Nov 17, 2025
    e010dfb
  6. self-assigned this
    on Nov 17, 2025
  7. cowtowncoder commented on Nov 17, 2025

    @cowtowncoder
    Member

    Ok: fixing via #379 by:

    • Changing InstantDeserializer.withDateFormat(DTF) to be public
    • Adding public OffsetDateTimeSerializer.withFormatter(DTF)

    so that it is possible to construct customized instances: from unit tests:

    InstantDeserializer<OffsetDateTime> customDeserializer =
        InstantDeserializer.OFFSET_DATE_TIME.withDateFormat(customFormatter);
    
    OffsetDateTimeSerializer customSerializer =
        OffsetDateTimeSerializer.INSTANCE .withFormatter(dtf);
    

    (note: did not change existing method name withDateFormat())

  8. added this to the 2.21.0 milestone on Nov 17, 2025
  9. cowtowncoder commented on Nov 17, 2025

    @cowtowncoder
    Member

    Fixed by #379.

  10. removed
    pr-welcomeIssue for which progress most likely if someone submits a Pull Request
    on Nov 17, 2025
  11. added a commit that references this issue on Nov 18, 2025
    ec2fa91
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions