编写转换器转换为TimeStamp
@Configuration
public class StringToTimestampConverter implements Converter<String,Timestamp> {
private final SimpleDateFormat defaultDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public Timestamp convert(String text) {
Timestamp timestamp=null;
if(StringUtils.hasText(text)){
text = text.trim();
boolean isLong = true;
try {
long millisecond = Long.parseLong(text);
timestamp=new Timestamp(millisecond);
}catch(Exception e){
isLong = false;
}
if(!isLong) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(FormatUtils.getFormatter(text.length()));
String format = defaultDateFormat.format(sdf.parse(text));
System.out.println(format);
} catch (ParseException var3) {
throw new IllegalArgumentException("Could not parse date: " + var3.getMessage(), var3);
}
}
}
return timestamp;
}
}
public class FormatUtils {
public static String getFormatter(int length) {
String formatter = "yyyy-MM-dd";
if (length == 16) {
formatter = "yyyy-MM-dd HH:mm";
} else if (length == 19) {
formatter = "yyyy-MM-dd HH:mm:ss";
} else if (length == 20) {
formatter = "yyyy-MM-dd HH:mm:ss.S";
} else if (length == 21) {
formatter = "yyyy-MM-dd HH:mm:ss.SS";
} else if (length == 22) {
formatter = "yyyy-MM-dd HH:mm:ss.SSS";
}
return formatter;
}
}
拦截器
@Configuration
public class WebConfigBeans {
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;
/**
* 增加字符串转timStamp的功能
*/
@PostConstruct
public void initEditableValidation() {
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter
.getWebBindingInitializer();
if (initializer.getConversionService() != null) {
GenericConversionService genericConversionService = (GenericConversionService) initializer
.getConversionService();
genericConversionService.addConverter(new StringToTimestampConverter());
}
}
}
@PostConstruct
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
@StringToDate
@Configuration
public class StringToTimestampConverter implements Converter<String,Date> {
private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
private static final String shortDateFormat = "yyyy-MM-dd";
private static final String dateFormat2 = "yyyy/MM/dd HH:mm:ss";
private static final String shortDateFormat2 = "yyyy/MM/dd";
@Override
public Date convert(String source) {
if (StringUtils.isBlank(source)) {
return null;
}
source = source.trim();
try {
SimpleDateFormat formatter;
if (source.contains("-")) {
if (source.contains(":")) {
formatter = new SimpleDateFormat(dateFormat);
} else {
formatter = new SimpleDateFormat(shortDateFormat);
}
Date dtDate = formatter.parse(source);
return dtDate;
} else if (source.contains("/")) {
if (source.contains(":")) {
formatter = new SimpleDateFormat(dateFormat2);
} else {
formatter = new SimpleDateFormat(shortDateFormat2);
}
Date dtDate = formatter.parse(source);
return dtDate;
}
} catch (Exception e) {
throw new RuntimeException(String.format("parser %s to Date fail", source));
}
throw new RuntimeException(String.format("parser %s to Date fail", source));
}
}
设置默认页面
@Configuration
public class DefaultView extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/hello.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
super.addViewControllers(registry);
}
}