JAVA的LocalDateTime与时间格式化与序列化、反序列化

格式化

1
2
3
4
String d="08-12月-17 05.38.07.812000 下午";
Locale locale=Locale.CHINA;
Date dd=new SimpleDateFormat("dd-M月-y hh.mm.ss.S a",locale).parse(d);
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dd));

下面测试输出类型,以及可输出的类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@Test
public void LocalDateTest(){
// region LocalDate
log.debug("LocalDate => ISO_LOCAL_DATE 结果为 {}",LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
log.debug("LocalDate => ISO_DATE 结果为 {}",LocalDate.now().format(DateTimeFormatter.ISO_DATE));
log.debug("LocalDate => ISO_ORDINAL_DATE 结果为 {}",LocalDate.now().format(DateTimeFormatter.ISO_ORDINAL_DATE));
log.debug("LocalDate => ISO_WEEK_DATE 结果为 {}",LocalDate.now().format(DateTimeFormatter.ISO_WEEK_DATE));
log.debug("LocalDate => BASIC_ISO_DATE 结果为 {}",LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE));
// endregion
}


@Test
public void LocalTimeTest(){
// region LocalTime
log.debug("LocalTime => ISO_LOCAL_TIME 结果为 {}",LocalTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME));
log.debug("LocalTime => ISO_TIME 结果为 {}",LocalTime.now().format(DateTimeFormatter.ISO_TIME));
// endregion
}

@Test
public void LocalDateTimeTest(){
// region LocalDateTime
log.debug("LocalDateTime => ISO_LOCAL_DATE 结果为 {}",LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
log.debug("LocalDateTime => ISO_DATE 结果为 {}",LocalDateTime.now().format(DateTimeFormatter.ISO_DATE));
log.debug("LocalDateTime => ISO_LOCAL_TIME 结果为 {}",LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME));
log.debug("LocalDateTime => ISO_TIME 结果为 {}",LocalDateTime.now().format(DateTimeFormatter.ISO_TIME));
log.debug("LocalDateTime => ISO_LOCAL_DATE_TIME 结果为 {}",LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
log.debug("LocalDateTime => ISO_DATE_TIME 结果为 {}",LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
log.debug("LocalDateTime => ISO_ORDINAL_DATE 结果为 {}",LocalDateTime.now().format(DateTimeFormatter.ISO_ORDINAL_DATE));
log.debug("LocalDateTime => ISO_WEEK_DATE 结果为 {}",LocalDateTime.now().format(DateTimeFormatter.ISO_WEEK_DATE));
log.debug("LocalDateTime => BASIC_ISO_DATE 结果为 {}",LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE));
log.debug("LocalDateTime.parse(\"2019-01-01\", DateTimeFormatter.ISO_DATE_TIME) {}",LocalDateTime.parse("2019-01-01",DateTimeFormatter.ISO_LOCAL_DATE));
// endregion
}

后端接收

直接上代码,三种序列化方式

第一种方式:反序列化(用于json

1
2
3
4
5
6
7
8
9
@Bean
public JsonSerializer<LocalDateTime> basicJsonSerializer(){
return new JsonSerializer<LocalDateTime>(){
@Override
public void serialize(LocalDateTime value,JsonGenerator gen,SerializerProvider serializers)throws IOException{
gen.writeString(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
};
}

第二种方式:Converter转换(只支持parameter

这个只能用于 URL 参数

1
2
3
4
5
6
7
8
9
10
@Bean
public Converter<String, LocalDateTime> localDateTimeConverter(){
return new Converter<String, LocalDateTime>(){
@Override
public LocalDateTime convert(@NotNull String oldValue){
/*处理String*/
return null;
}
};
}

第三种方式:实现WebMvcConfigureraddFormatters

这个也是和第二种方式一样,只不过这里自定义程度高!可以添加FormattersConverter 以及他们的工厂。

下面是部分代码。

1
2
3
4
5
6
7
8
9

@Component
public class BootWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
/*常用类型转换数组*/
BootConverters.getConverterToRegister().forEach(registry::addConverter);
}
}

后端发出:反序列化

一般单个序列化

1
2
3
4
5
6
7
8
9
10
11
@Bean
public JsonDeserializer<LocalDateTime> basicJsonDeserializer(){
return new JsonDeserializer<LocalDateTime>(){
@Override
public LocalDateTime deserialize(JsonParser p,DeserializationContext ctxt)throws IOException,JsonProcessingException{
String oldValue=p.getText();
/*处理String*/
return null;
}
};
}

下面是引入了JSON包:

1
2
3
4
5

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* Json序列化和反序列化转换器,用于转换Post请求体中的json以及将我们的对象序列化为返回响应的json
*/
@Bean
public ObjectMapper objectMapper(){
ObjectMapper objectMapper=new ObjectMapper();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);

/*LocalDateTime系列序列化和反序列化模块,继承自jsr310,我们在这里修改了日期格式
* 在这里修改之后,就不用再去全局序列化了*/
JavaTimeModule javaTimeModule=new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class
,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DateBootUtil.DEFAULT_DATETIME_FORMAT)));
javaTimeModule.addSerializer(LocalDate.class
,new LocalDateSerializer(DateTimeFormatter.ofPattern(DateBootUtil.DEFAULT_DATE_FORMAT)));
javaTimeModule.addSerializer(LocalTime.class
,new LocalTimeSerializer(DateTimeFormatter.ofPattern(DateBootUtil.DEFAULT_TIME_FORMAT)));
javaTimeModule.addDeserializer(LocalDateTime.class
,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DateBootUtil.DEFAULT_DATETIME_FORMAT)));
javaTimeModule.addDeserializer(LocalDate.class
,new LocalDateDeserializer(DateTimeFormatter.ofPattern(DateBootUtil.DEFAULT_DATE_FORMAT)));
javaTimeModule.addDeserializer(LocalTime.class
,new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DateBootUtil.DEFAULT_TIME_FORMAT)));
/*Date序列化和反序列化 jackson-datatype-jsr310 包中已经存在 这里就不用再次写了*/
objectMapper.registerModule(javaTimeModule);
return objectMapper;
}

本文地址: https://github.com/maxzhao-it/blog/post/17748/