2012-07-04 45 views
4

是否可以在Spring MVC控制器中處理不同的日期格式?如何在Spring MVC控制器中處理不同的日期格式?

我知道,設定這樣的事情

@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嗎?

+1

強制在客戶端只有一個日期格式也一樣,jQuery UI的日期選擇器等可以很容易 – NimChimpsky

+0

這是相當奇怪 - 我明白,你可以*解析*不同格式的日期與自定義編輯器或甚至更好的類型轉換器 - 但你將如何*顯示*他們?它認爲你應該實現你自己的日期類型來封裝關於它的表示的信息,看起來有點奇怪 –

+0

我認爲它應該以相同的格式表示,否則你會遇到問題。在這裏,你只有一個格式化程序,只有一種格式,如果你有多個格式,它應該採取第一個有效的格式, – EliuX

回答

1

如果在同一時間收到日期只有一種格式,那麼你可以簡單地基於格式創建的DateFormat一個實例

例如

決定根據輸入的格式

DateFormat df = null; 
if(recievedDate.indexOf("//")!=-1){ 
    df = new SimpleDateFormat("dd/MM/yyyy") 
}else{ 
    df = new SimpleDateFormat("yyyyMMddhhmmss") 
} 
3

這個怎麼樣。上述情況很快就會出現重擊。

 public class MostLenientDateParser { 
      private final List<String> supportedFormats; 

      public MostLenientDateParser(List<String> supportedFormats) { 
       this.supportedFormats = supportedFormats; 
      } 

      public Date parse(String dateValue) { 
       for(String candidateFormat: supportedFormats) { 
        Date date = lenientParse(dateValue, candidateFormat); 
        if (date != null) { 
         return date; 
        } 
       } 
       throw new RuntimeException("tried so many formats, non matched"); 
      } 

      private Date lenientParse(String dateCandidate, String dateFormat) { 
       try { 
        return new SimpleDateFormat(dateFormat).parse(dateCandidate); 
       } catch (Exception e) { 
        return null; 
       } 
      } 
     } 

這也可以通過Spring Converters通過CustomDateEditor實現來引用表單數據綁定。

4

靈感來自Skipy

public class LenientDateParser extends PropertyEditorSupport { 

private static final List<String> formats = new ArrayList<String>(); 

private String outputFormat; 

static{ 
    formats.add("dd-MM-yyyy HH:ss"); 
    formats.add("dd/MM/yyyy HH:ss"); 
    formats.add("dd-MM-yyyy"); 
    formats.add("dd/MM/yyyy"); 
    formats.add("dd MMM yyyy"); 
    formats.add("MMM-yyyy HH:ss"); 
    formats.add("MMM-yyyy"); 
    formats.add("MMM yyyy"); 
} 

public LenientDateParser(String outputFormat){ 
    this.outputFormat = outputFormat; 
} 

@Override 
public void setAsText(String text) throws IllegalArgumentException { 
    if(StringUtils.isEmpty(text)) 
     return; 
    DateTime dt = null; 
    for(String format : formats){ 

     try{ 

      dt = DateTime.parse(text, DateTimeFormat.forPattern(format)); 

      break; 

     }catch(Exception e){ 
      if(log.isDebugEnabled()) 
       log.debug(e,e); 
     } 
    } 
    if(dt != null) 
     setValue(dt.toDate()); 
} 

@Override 
public String getAsText() { 
    Date date = (Date) getValue(); 

    if(date == null) 
     return ""; 

    DateTimeFormatter f = DateTimeFormat.forPattern(outputFormat); 

    return f.print(date.getTime()); 

} 
} 
2

對於有同樣的問題,如果你正在使用彈簧3可以在現場使用真棒@DateTimeFormat(模式=「DD-MM-YYYY」)他人的模型。

只需確保您的org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter註冊一個conversionService

你可以有,只要你想在同一個bean @DateTimeFormat之多。

1

在處理多個語言環境時,不是一個好主意,可以使用寬鬆日期格式化程序。像2013年10月11日這樣的日期可以用dd/MM/YYYY和MM/dd/YYYY進行正確解析。

+0

你不應該指定那些不明確的情況。情況並非如此。 – EliuX

3

如果您只在情況下需要它,您可以註冊附加到字段中的自定義編輯器形式:

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", this.getLocale(context)); 
DateFormat dateTimeFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss SSS", this.getLocale(context)); 
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateTimeFormat, true)); 
binder.registerCustomEditor(Date.class, "name.of.input", new CustomDateEditor(dateTimeFormat, true)); 
相關問題