如何在Spring MVC控制器中处理不同的日期格式? [英] How to handle different date formats in Spring MVC controller?
本文介绍了如何在Spring MVC控制器中处理不同的日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在Spring MVC控制器中处理不同的日期格式?
Is it possible to handle different date format in a Spring MVC controller?
我知道设置类似这样的东西
I know that setting something like this
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, false));
}
我可以处理 dd / MM / yyyy
格式,但如果我想解析 yyyyMMddhhmmss
格式的日期呢?我应该在我的控制器中添加多个 CustomDateEditor
吗?
I can handle dd/MM/yyyy
format, but what if i want to parse also dates in yyyyMMddhhmmss
format? Should I add multiple CustomDateEditor
s in my controller?
推荐答案
如果在如果您只收到一种日期格式,那么您可以根据格式创建一个 DateFormat
的实例
If at a time you receive only one format of date, then you could simply create one instance of DateFormat
based on format
例如
根据输入确定格式
DateFormat df = null;
if(recievedDate.indexOf("//")!=-1){
df = new SimpleDateFormat("dd/MM/yyyy")
}else{
df = new SimpleDateFormat("yyyyMMddhhmmss")
}
这篇关于如何在Spring MVC控制器中处理不同的日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文