- Package com.util;
- Import Java.text.DateFormat;
- Import Java.text.SimpleDateFormat;
- Import Java.util.Date;
- Import Java.util.Map;
- Import Com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
- /**
- * Date Conversion class
- *
- */
- Public class DateConverter extends Defaulttypeconverter {
- private Static final dateformat[] accept_date_formats = {
- New SimpleDateFormat ("dd/mm/yyyy"),
- New SimpleDateFormat ("Yyyy-mm-dd"),
- New SimpleDateFormat ("Yyyy/mm/dd")}; //Date format to support conversion
- @Override
- Public Object Convertvalue (Map context, object value, Class totype) {
- if (ToType = = Date. Class) { //The conversion of string to date when the browser submits to the server
- Date date = null;
- String datestring = null;
- String[] params = (string[]) value;
- datestring = params[0]; Gets the date of the string
- For (DateFormat format:accept_date_formats) {
- try {
- return Format.parse (datestring); //Traversal date support format for conversion
- } catch (Exception e) {
- continue;
- }
- }
- return null;
- }
- Else if (ToType = = String. Class) { //the type conversion of date to string when the server outputs to the browser
- Date date = (date) value;
- return new SimpleDateFormat ("YYYY-MM-DD"). Format (date); The format of the output is Yyyy-mm-dd
- }
- return null;
- }
- }
Date Conversion Class Dateconverter.java