web中Date類型的處理方法

來源:互聯網
上載者:User
 

一:Struts Form中Date類型之“argument type mismatch”錯誤解決方案現象:
    當輸入頁面中含有日期如2008-01-01時,如果Form中對於的欄位為Date類型,則會出現“argument type mismatch”錯誤。

原因:
    Struts的ActionServlet在接受到頁面的請求後,會調用RequestUtils.populate方法對Form進行填值,而此方法又調用到BeanUtils.populate方法,而該方法進行填值時對不同類型的會進行轉換,而預設的ActionServlet並沒有註冊相應的轉換函式,所以會導致轉換出現錯誤。

解決方案:
    1.實現自訂的ActionServlet方法並在裡面註冊轉換函式。
    2.Form中的日期類型使用String類型,把轉換動作放在Action中處理(即在Action中對Form->POJO轉換時處理),可以在Action基類中註冊類型轉換類。二:String類型和Date類型的互相轉換//STRING到日期    publicstatic java.sql.Date stringToDate(String dateStr)     {     return java.sql.Date.valueOf(dateStr);     }     //日期到STRING     publicstatic String dateToString(java.sql.Date datee)     {     return datee.toString();     }注意:在轉換的代碼中定義java.util.Date類型的變數!因為java.sql.Date類型是java.util.Date類型的子類。子類可以自動賦值給父類。 三:擷取目前時間import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.GregorianCalendar; publicclass DateTime {     public static Date getDatetime(){             SimpleDateFormat dateFormatter =new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");         GregorianCalendar gc=new GregorianCalendar();          Date date=new Date();;                        try {                   date =dateFormatter.parse((dateFormatter.format(gc.getTime())));                                           } catch (ParseException e) {                  // TODO Auto-generated catch block                  e.printStackTrace();              }                    return date;    }           }  

四:驗證日期類型格式 import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern;   public class DateV {             public static boolean isValidDate(String sDate) {     String datePattern1 = "//d{4}-//d{2}-//d{2}";     String datePattern2 = "^((//d{2}(([02468][048])|([13579][26]))"    + "[//-/////s]?((((0?[13578])|(1[02]))[//-/////s]?((0?[1-9])|([1-2][0-9])|"    + "(3[01])))|(((0?[469])|(11))[//-/////s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[//-/////s]?"    + "((0?[1-9])|([1-2][0-9])))))|(//d{2}(([02468][1235679])|([13579][01345789]))[//-/////s]?("    + "(((0?[13578])|(1[02]))[//-/////s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[//-/////s]?"    + "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[//-/////s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";     if ((sDate != null)) {     Pattern pattern = Pattern. compile(datePattern1);     Matcher match = pattern.matcher(sDate);     if (match.matches()) {     pattern = Pattern. compile(datePattern2);     match = pattern.matcher(sDate);     return match.matches();    }     else {     return false;    }     }     return false;    }  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.