本文主要是介绍LocalDate与 LocalDateTime 介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
@ApiOperation("分页查询文档")@GetMapping("page")public Response<PageInfo<DocumentInfoQueryDto>> documentList(@RequestParam Integer pageNum,@RequestParam Integer size, @RequestParam(required = false) String documentTitle,@RequestParam(required = false) Integer documentType, @RequestParam(required = false) String creatorName,@RequestParam(required = false) LocalDate createTimeStart,@RequestParam(required = false) LocalDate createTimeEnd) {}
@ApiOperation("分页查询文档")@GetMapping("pages")public Response<Void> documentLisst(@RequestParam Integer pageNum,@RequestParam Integer size, @RequestParam(required = false) String documentTitle,@RequestParam(required = false) Integer documentType, @RequestParam(required = false) String creatorName,@RequestParam(required = false) LocalDateTime createTimeStart,@RequestParam(required = false) LocalDateTime createTimeEnd) {return Response.success();}
@ApiOperation("分页查询文档")@GetMapping("pagess")public Response<Void> documentLisst(@RequestParam Integer pageNum,@RequestParam Integer size, @RequestParam(required = false) String documentTitle,@RequestParam(required = false) Integer documentType, @RequestParam(required = false) String creatorName,@RequestParam(required = false) LocalTime createTimeStart,@RequestParam(required = false) LocalTime createTimeEnd) {return Response.success();}
配置类
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;import javax.annotation.Nonnull;import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;@Configuration
public class DateTimeConfiguration {/*** 默认日期时间格式*/public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";/*** 默认日期格式*/public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";/*** 默认时间格式*/public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";/*** LocalDate转换器,用于转换RequestParam和PathVariable参数*/@Beanpublic Converter<String, LocalDate> localDateConverter() {return new StringToLocalDateConverter();}/*** LocalDateTime转换器,用于转换RequestParam和PathVariable参数*/@Beanpublic Converter<String, LocalDateTime> localDateTimeConverter() {return new StringToLocalDateTimeConverter();}/*** LocalTime转换器,用于转换RequestParam和PathVariable参数*/@Beanpublic Converter<String, LocalTime> localTimeConverter() {return new StringToLocalTimeConverter();}/*** Date转换器,用于转换RequestParam和PathVariable参数*/@Beanpublic Converter<String, Date> dateConverter() {return new StringToDateConverter();}/*** Json序列化和反序列化转换器,用于转换Post请求体中的json以及将我们的对象序列化为返回响应的json*/@Beanpublic Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {return builder -> {builder.simpleDateFormat(DEFAULT_DATE_TIME_FORMAT);builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));builder.serializers(new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));builder.deserializers(new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));builder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));builder.deserializers(new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));};}public static class StringToLocalDateConverter implements Converter<String, LocalDate> {@Overridepublic LocalDate convert(@Nonnull String source) {return StringUtils.isEmpty(source) ? null : LocalDate.parse(source,DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));// DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT));}}public static class StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {@Overridepublic LocalDateTime convert(@Nonnull String source) {return StringUtils.isEmpty(source) ? null : LocalDateTime.parse(source,DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT));}}public static class StringToLocalTimeConverter implements Converter<String, LocalTime> {@Overridepublic LocalTime convert(@Nonnull String source) {return StringUtils.isEmpty(source) ? null : LocalTime.parse(source,DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT));}}public static class StringToDateConverter implements Converter<String, Date> {@Overridepublic Date convert(@Nonnull String source) {SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);try {return format.parse(source);} catch (ParseException e) {throw new RuntimeException(e);}}}
}
值的注意的是,不管是LocalDate LocalDateTime还是LocalTime类型
传入的都是String类型。
参考链接
这辈子坚持与不坚持都不可怕,怕的是独自走在坚持的道路上!!!
这篇关于LocalDate与 LocalDateTime 介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!