2014-02-12 55 views
0
public class DateCreation { 
    public static void main(String[] args) { 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
      Date date = new Date(); 
      String format = sdf.format(date); 
      System.out.print("date is:"+format); 
    }  
} 

這是工作的罰款,並輸出爲:日期:2014年2月11日日期格式不適用於spring的服務類?

但是,當我在春天MVC的服務層使用相同的語法比它的結果: 日期爲:???? - ?? - ??

這是春天的代碼。有一個@Override函數,我已經使用它。

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
    Date date = new Date(); 
    String format = sdf.format(date); 
    logger.info("current date is:"+format); 

什麼可能是錯誤?

+0

這聽起來沒有什麼彈簧對我來說。可能你在模式變量中有一個錯誤 – gerrytan

+0

請告訴我們你是如何在Spring中使用它的。 –

+0

@SotiriosDelimanolis我已經更新了 –

回答

0
@InitBinder 
    public void initBinder(WebDataBinder webDataBinder) { 
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
     dateFormat.setLenient(false); 
     webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); 
    } 
+0

在服務層類中使用上面的代碼。 –

+0

這是webDataBinder –

+0

Ankit,你可以編輯你的答案,如果你需要的話。另外,這個改變是如何解釋打印記錄器聲明中的前一個'??'? –