问题:字符串/时间戳入参-》后端LocalDateTime
- 单个时间戳入参
public class LocalDateTimeArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter methodParameter) {
return methodParameter.getParameterType().equals(LocalDateTime.class);
}
@Override
public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
String parameterName = methodParameter.getParameterName();
if (Objects.nonNull(parameterName)) {
String parameter = nativeWebRequest.getParameter(parameterName);
if (Objects.nonNull(parameter)) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(parameter)), ZoneId.systemDefault());
}
}
return null;
}
}
@Configuration
public class CustomDateConfig implements WebMvcConfigurer {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new LocalDateTimeArgumentResolver());
// .....
}
}
http://localhost:22222/dateTest3?localDateTime=1671360603000
- 多个参数,里面包含时间戳
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import javax.validation.constraints.NotNull;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
@Configuration
public class DateTimeConfig {
/** 默认日期时间格式 */
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";
/**
* 日期正则表达式
*/
private static final String DATE_REGEX = "[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])";
/**
* 时间正则表达式
*/
private static final String TIME_REGEX = "(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d";
/**
* 日期和时间正则表达式
*/
private static final String DATE_TIME_REGEX = DATE_REGEX + "\\s" + TIME_REGEX;
/**
* 13位时间戳正则表达式
*/
private static final String TIME_STAMP_REGEX = "1\\d{12}";
/**
* 年和月正则表达式
*/
private static final String YEAR_MONTH_REGEX = "[1-9]\\d{3}-(0[1-9]|1[0-2])";
/**
* 年和月格式
*/
private static final String YEAR_MONTH_PATTERN = "yyyy-MM";
/**
* LocalDateTime转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalDateTime> localDateTimeConverter() {
return new Converter<String, LocalDateTime>() {
@Override
public LocalDateTime convert(@NotNull String source) {
if (source.matches(TIME_STAMP_REGEX)) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(source)), ZoneId.systemDefault());
} else if (source.matches(DATE_TIME_REGEX)) {
return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT));
} else {
return null;
}
}
};
}
/**
* LocalDate转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalDate> localDateConverter() {
return new Converter<String, LocalDate>() {
@Override
public LocalDate convert(@NotNull String source) {
if (source.matches(TIME_STAMP_REGEX)) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(source)), ZoneId.systemDefault()).toLocalDate();
} else if (source.matches(DATE_REGEX)) {
return LocalDate.parse(source, DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));
} else {
return null;
}
}
};
}
/**
* LocalTime转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalTime> localTimeConverter() {
return new Converter<String, LocalTime>() {
@Override
public LocalTime convert(@NotNull String source) {
if (source.matches(TIME_STAMP_REGEX)) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(source)), ZoneId.systemDefault()).toLocalTime();
} else if (source.matches(TIME_REGEX)) {
return LocalTime.parse(source, DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT));
} else {
return null;
}
}
};
}
/**
* Date转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, Date> dateConverter() {
return new Converter<String, Date>() {
@Override
public Date convert(@NotNull String source) {
SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
try {
return format.parse(source);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
}
}
public class DateTestDTO implements Serializable {
private Long id;
private LocalDateTime localDateTime;
private LocalDate localDate;
// obmit getter and setter...
}
// TestController..
@PostMapping(value = "/dateTest3")
@ResponseBody
public LocalDateTime dateTest3(DateTestDTO dto) {
System.out.println(dto);
return dto.getLocalDateTime();
}
http://localhost:22222/dateTest3?id=1671360603000&localDateTime=1671360603000
具体参考:springboot中的参数解析器,参数解析的过程