Spring Boot 中java8时间的转换

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

我使用的是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 {
        @Override
        public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeString(value.format(DATE_TIME_FORMATTER));
        }
    }
}

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

JsonDeserializer

如果是在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 getSupportedMediaTypes() {
                ArrayList 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 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());
            }
        };
    }
}

 

你可能感兴趣的:(WEB,springboot,JAVA,spring)