1. 程式人生 > 實用技巧 >@Valid Date 日期全域性的格式化轉換

@Valid Date 日期全域性的格式化轉換

通過InitBinder註解,做到全域性的格式化轉換

首先自定義一個格式轉換類(我們以Date格式為例)DateFormatEditor繼承自PropertiesEditor:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.springframework.beans.propertyeditors.PropertiesEditor;
 
public class DateFormatEditor extends PropertiesEditor {
    // 日期是否允許為空
    private
boolean isAllowEmpty; // 日期格式 private String pattern; public DateFormatEditor() { super(); this.isAllowEmpty = true; this.pattern = "yyyy-MM-dd"; } public DateFormatEditor(String pattern, boolean isAllowEmpty) { super(); this.isAllowEmpty = isAllowEmpty;
this.pattern = pattern; } public boolean isAllowEmpty() { return isAllowEmpty; } public void setAllowEmpty(boolean isAllowEmpty) { this.isAllowEmpty = isAllowEmpty; } public String getPattern() { return pattern; } public void setPattern(String pattern) {
this.pattern = pattern; } @Override public void setAsText(String text) throws IllegalArgumentException { try { if (text != null && !text.equals("")) { SimpleDateFormat format = new SimpleDateFormat(pattern); setValue(format.parse(text)); } else { if (isAllowEmpty) { setValue(null); } } } catch (ParseException e) { System.out.println("格式轉換失敗!"); setValue(null); } } }

定義完了還是不能用,這裡需要我們註冊:

由於需要全域性的格式化轉換所以我們定義一個BaseController類:

import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import spring.util.DateFormatEditor;
 
@Controller
public class BaseController {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected HttpServletResponse response;
    @Autowired
    protected HttpSession session;
    @Autowired
    protected ServletContext application;
 
    @InitBinder
    protected void initDate(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new DateFormatEditor("yyyy-MM-dd hh:mm:ss", true));
    }
}

首先自定義一個格式轉換類(我們以Date格式為例)DateFormatEditor繼承自PropertiesEditor:

  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import org.springframework.beans.propertyeditors.PropertiesEditor;
  4. public class DateFormatEditor extends PropertiesEditor {
  5. // 日期是否允許為空
  6. private boolean isAllowEmpty;
  7. // 日期格式
  8. private String pattern;
  9. public DateFormatEditor() {
  10. super();
  11. this.isAllowEmpty = true;
  12. this.pattern = "yyyy-MM-dd";
  13. }
  14. public DateFormatEditor(String pattern, boolean isAllowEmpty) {
  15. super();
  16. this.isAllowEmpty = isAllowEmpty;
  17. this.pattern = pattern;
  18. }
  19. public boolean isAllowEmpty() {
  20. return isAllowEmpty;
  21. }
  22. public void setAllowEmpty(boolean isAllowEmpty) {
  23. this.isAllowEmpty = isAllowEmpty;
  24. }
  25. public String getPattern() {
  26. return pattern;
  27. }
  28. public void setPattern(String pattern) {
  29. this.pattern = pattern;
  30. }
  31. @Override
  32. public void setAsText(String text) throws IllegalArgumentException {
  33. try {
  34. if (text != null && !text.equals("")) {
  35. SimpleDateFormat format = new SimpleDateFormat(pattern);
  36. setValue(format.parse(text));
  37. } else {
  38. if (isAllowEmpty) {
  39. setValue(null);
  40. }
  41. }
  42. } catch (ParseException e) {
  43. System.out.println("格式轉換失敗!");
  44. setValue(null);
  45. }
  46. }
  47. }

定義完了還是不能用,這裡需要我們註冊:

由於需要全域性的格式化轉換所以我們定義一個BaseController類:

  1. import java.util.Date;
  2. import javax.servlet.ServletContext;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import javax.servlet.http.HttpSession;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.WebDataBinder;
  9. import org.springframework.web.bind.annotation.InitBinder;
  10. import spring.util.DateFormatEditor;
  11. @Controller
  12. public class BaseController {
  13. @Autowired
  14. protected HttpServletRequest request;
  15. @Autowired
  16. protected HttpServletResponse response;
  17. @Autowired
  18. protected HttpSession session;
  19. @Autowired
  20. protected ServletContext application;
  21. @InitBinder
  22. protected void initDate(WebDataBinder binder) {
  23. binder.registerCustomEditor(Date.class, new DateFormatEditor("yyyy-MM-dd hh:mm:ss", true));
  24. }
  25. }

這樣就把所有Date型別的引數全部攔截下來進行判斷和格式化。