java - How do you set an attribute to the current OffsetDateTime (now) using MapStruct? - Stack Overflow

admin2025-04-29  2

I’m trying to use MapStruct to map attributes on a class and among those attributes is a OffetDateTime variable that I need to set to the current time. However, when the MapperImpl class is generated, it cannot recognize the OffsetDateTime and ZoneOffset classes. So, for e.g.

public class MapThisClass {
    private OffsetDateTime time;

    // other attributes
}

I’m trying to set (in the mapping, the time to the current OffsetTime like below:

public class MapThisClassMapper {

    //. Map other attributes

    Mapping(target = “time”, expression = "java(OffsetDateTime.now(ZoneOffset.UTC))")
    public abstract MapThisClass cloneMapThisClass(MapThisClass mapThisObject, Clazz someOtherClassToMapFrom);
    

}

When I try to build, I get the following error.

ERROR] MapThisMapperImpl.java:[233,61] cannot find symbol
[ERROR]   symbol:   variable ZoneOffset
[ERROR]   location: class MapThisMapperImpl
[ERROR] MapThisMapperImpl.java:[233,42] cannot find symbol
[ERROR]   symbol:   variable OffsetDateTime
[ERROR]   location: class MapThisMapperImpl

I’m new to MapStruct. Is there a way for me to ensure that the MapperImpl class recognizes (automatically imports) the OffsetDateTime and ZoneOffset classes? Or maybe there is another way to do this and I need to go about this some other way?

Any insights/help would be greatly appreciated.

thanks!

I’m trying to use MapStruct to map attributes on a class and among those attributes is a OffetDateTime variable that I need to set to the current time. However, when the MapperImpl class is generated, it cannot recognize the OffsetDateTime and ZoneOffset classes. So, for e.g.

public class MapThisClass {
    private OffsetDateTime time;

    // other attributes
}

I’m trying to set (in the mapping, the time to the current OffsetTime like below:

public class MapThisClassMapper {

    //. Map other attributes

    Mapping(target = “time”, expression = "java(OffsetDateTime.now(ZoneOffset.UTC))")
    public abstract MapThisClass cloneMapThisClass(MapThisClass mapThisObject, Clazz someOtherClassToMapFrom);
    

}

When I try to build, I get the following error.

ERROR] MapThisMapperImpl.java:[233,61] cannot find symbol
[ERROR]   symbol:   variable ZoneOffset
[ERROR]   location: class MapThisMapperImpl
[ERROR] MapThisMapperImpl.java:[233,42] cannot find symbol
[ERROR]   symbol:   variable OffsetDateTime
[ERROR]   location: class MapThisMapperImpl

I’m new to MapStruct. Is there a way for me to ensure that the MapperImpl class recognizes (automatically imports) the OffsetDateTime and ZoneOffset classes? Or maybe there is another way to do this and I need to go about this some other way?

Any insights/help would be greatly appreciated.

thanks!

Share Improve this question asked Jan 6 at 23:01 Sandeep BarnabasSandeep Barnabas 632 bronze badges 1
  • Just a warning you are probably aware of using ZoneOffset.UTC. Only offset 0, Greenwich Mean Time, UTC, has meaning. For other times use ZonedDateTime specific to some countries regulations should be used. Better immediately use ZonedDateTime to avoid the pure theoretical OffsetDateTime. – Joop Eggen Commented Jan 7 at 9:45
Add a comment  | 

1 Answer 1

Reset to default 1

Just add the imports to the interface or give the fully-qualified name in the expression:

@Mapper(imports = {OffsetDateTime.class, ZoneOffset.class})
public class MapThisClassMapper {

    //. Map other attributes

    Mapping(target = “time”, expression = "java(OffsetDateTime.now(ZoneOffset.UTC))")
    public abstract MapThisClass cloneMapThisClass(MapThisClass mapThisObject, Clazz someOtherClassToMapFrom);
    

}

or

public class MapThisClassMapper {

    //. Map other attributes

    Mapping(target = “time”, expression = "java(java.time.OffsetDateTime.now(java.time.ZoneOffset.UTC))")
    public abstract MapThisClass cloneMapThisClass(MapThisClass mapThisObject, Clazz someOtherClassToMapFrom);
    

}
转载请注明原文地址:http://anycun.com/QandA/1745940164a91409.html