Modelmapper to convert from String to LocalDate

后端 未结 3 737
终归单人心
终归单人心 2021-01-12 00:08

My DTO is having date field in String format. My entity is having date as LocalDate. Currently I am skipping it from map and then later manually explicitly setting it (Strin

相关标签:
3条回答
  • 2021-01-12 00:20

    I also use ModelMapper 2.3.5 for my project and had a similar issue. The use of the ModelMapper related project https://github.com/modelmapper/modelmapper-module-java8/ helped me a lot.

    It provides two modules that one can add to ModelMapper configuration by providing the following maven dependencies to your project :

    <!-- https://mvnrepository.com/artifact/com.github.chhsiao90/modelmapper-module-java8-datatypes -->
    <dependency>
        <groupId>com.github.chhsiao90</groupId>
        <artifactId>modelmapper-module-java8-datatypes</artifactId>
        <version>1.1.0</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/com.github.chhsiao90/modelmapper-module-jsr310 -->
    <dependency>
        <groupId>com.github.chhsiao90</groupId>
        <artifactId>modelmapper-module-jsr310</artifactId>
        <version>1.1.0</version>
    </dependency>
    

    Then the ModelMapper configuration should add

    modelMapper.registerModule(new Jsr310Module());
    modelMapper.registerModule(new Jdk8Module());
    

    Hope this can help

    0 讨论(0)
  • 2021-01-12 00:23

    I am currently using ModelMapper 2.3.5 and the accepted solution does not work with me when I do ModelMapper.validate() for my mapping. See my related question.

    I managed to get also validation working - surprisingly - by removing provider totally so only converter is used. So like:

    modelmapper.addConverter(toStringDate);
    modelmapper.getTypeMap(String.class, LocalDate.class); // no provider, maps ok with me still
    modelmapper.validate();
    
    0 讨论(0)
  • 2021-01-12 00:28

    If you want to convert to LocalDate you need to create a Provider otherwise ModelMappercannot instantiate LocalDate because it doesn't have a public default constructor.

    Use this configuration and it will work:

     ModelMapper modelmapper = new ModelMapper();
    
        Provider<LocalDate> localDateProvider = new AbstractProvider<LocalDate>() {
            @Override
            public LocalDate get() {
                return LocalDate.now();
            }
        };
    
        Converter<String, LocalDate> toStringDate = new AbstractConverter<String, LocalDate>() {
            @Override
            protected LocalDate convert(String source) {
                DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDate localDate = LocalDate.parse(source, format);
                return localDate;
            }
        };
    
    
        modelmapper.createTypeMap(String.class, LocalDate.class);
        modelmapper.addConverter(toStringDate);
        modelmapper.getTypeMap(String.class, LocalDate.class).setProvider(localDateProvider);
    

    Test output:

     String dateTest = "2000-09-27";
     LocalDate dateConverted = modelmapper.map(dateTest, LocalDate.class);
    
     System.out.println(dateConverted.toString()); //Output = 2000-09-27
    
    0 讨论(0)
提交回复
热议问题