Skip to content

Mapping ZonedDateTime or OffsetDateTime to LocalDateTime #4027

@JanecekPetr

Description

@JanecekPetr

Expected behavior

This is very similar to:

However, given:

record Zoned(ZonedDateTime zonedDateTime) {}
record Local(LocalDateTime localDateTime) {}

@Mapper
interface ZonedToLocalMapper {
    @Mapping(target = "localDateTime", source = "zonedDateTime")
    Local toLocal(Zoned zoned);
}

I'd expect the mapper to just do

localDateTime = zonedDateTime.toLocalDateTime();

The reverse fails nicely, that's correct as converting LocalDateTime to ZonedDateTime requires a Zone.

Actual behavior

however, we see this monstrosity

localDateTime = xmlGregorianCalendarToLocalDateTime( zonedDateTimeToXmlGregorianCalendar( zonedDateTime ) );

with a lot of javax.xml.datatype classes. Those should be unnecessary.

Steps to reproduce the problem

record Zoned(ZonedDateTime zonedDateTime) {}
record Local(LocalDateTime localDateTime) {}

@Mapper
interface ZonedToLocalMapper {
    @Mapping(target = "localDateTime", source = "zonedDateTime")
    Local toLocal(Zoned zoned);
}

and the sane for OffsetDateTime on the left side and likely Instant and LocalTime and LocalDate on the right side etc.

MapStruct Version

1.6.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions