Spring Boot 中java8时间的转换

7 篇文章 0 订阅
2 篇文章 0 订阅

首先说一下,有问题找官方文档

我使用的是springboot 2.0.0.RELEASE版本。

对于这个官方给出了两种解决方案,一种是json的,一种是json的。

首先对于json增加如下配置即可:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * json序列化反序列时间
 *
 * @author Derrick.Zhao
 * @version 1.0.0
 * @sinc 2018/9/18 10:56
 */
@JsonComponent
public class JsonExample {

    public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static class DateTimeSerializer extends JsonSerializer<LocalDateTime> {
        @Override
        public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeString(value.format(DATE_TIME_FORMATTER));
        }
    }
}

 如此便可对时间进行序列化,反序列化继承

JsonDeserializer<LocalDateTime>

如果是在GET形式,可以重写

HttpMessageConverter

代码如下:

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * java8时间日期转换配置类
 *
 * @author Derrick.Zhao
 * @version 1.0.0
 * @sinc 2018/9/18 9:28
 */
@Configuration
public class MappingConverterAdapter {

    public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");

    @Bean
    public HttpMessageConverter httpMessageConverter() {


        return new HttpMessageConverter() {
            @Override
            public boolean canRead(Class clazz, @Nullable MediaType mediaType) {

                return clazz.equals(LocalDateTime.class) && getSupportedMediaTypes().contains(mediaType);
            }

            @Override
            public boolean canWrite(Class clazz, @Nullable MediaType mediaType) {
                return clazz == LocalDateTime.class && getSupportedMediaTypes().contains(mediaType);
            }

            @Override
            public List<MediaType> getSupportedMediaTypes() {
                ArrayList<MediaType> mediaTypes = new ArrayList<>();
                mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
                mediaTypes.add(MediaType.APPLICATION_JSON);
                mediaTypes.add(MediaType.TEXT_PLAIN);
                return mediaTypes;
            }

            @Override
            public Object read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
                MediaType contentType = inputMessage.getHeaders().getContentType();
                Charset charset = (contentType != null && contentType.getCharset() != null ?
                        contentType.getCharset() : CHARSET);
                String body = StreamUtils.copyToString(inputMessage.getBody(), charset);
                Map<String, String> parse = JSON.parse(body, Map.class);
                return LocalDateTime.parse(parse.values().stream().findFirst().get(), DATE_TIME_FORMATTER);
            }

            @Override
            public void write(Object o, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
                LocalDateTime localDateTime = (LocalDateTime) o;
                String format = localDateTime.format(DATE_TIME_FORMATTER);
                Charset charset = (contentType != null && contentType.getCharset() != null ?
                        contentType.getCharset() : CHARSET);
                StreamUtils.copy(format.getBytes(charset), outputMessage.getBody());
            }
        };
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值