美文网首页Hello Java
LocalDateTime时间格式化

LocalDateTime时间格式化

作者: Aldeo | 来源:发表于2019-01-09 15:17 被阅读10次

    1.引入jar
    //防止dto改成locakdatetime报错
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-typehandlers-jsr310</artifactId>
    <version>1.0.2</version>
    </dependency>
    //格式化数据
    <dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.9.2</version>
    </dependency>

    2.(1)加注解
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")

    (2)或者
    @Bean
    public ObjectMapper serializingObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
    objectMapper.registerModule(javaTimeModule);
    return objectMapper;
    }

    相关文章

      网友评论

        本文标题:LocalDateTime时间格式化

        本文链接:https://www.haomeiwen.com/subject/ffkgrqtx.html