So to make sure that the system has to convert the date type correctly, write a global type conversion class that makes the type conversion between date and string.
Copy Code code as follows:
Package com.great.util;
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.Map;
Import Com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
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 that supports conversion
@Override
Public Object Convertvalue (Map context, Object value, Class totype) {
if (ToType = = Date.class) {//browser when submitting to the server, a string to Date conversion
Date date = null;
String datestring = null;
String[] params = (string[]) value;
datestring = params[0];//string to get date
for (DateFormat format:accept_date_formats) {
try {
Return Format.parse (datestring);//Traversal date support format, convert
catch (Exception e) {
Continue
}
}
return null;
}
If else if (ToType = = String.class) {//server exports to browser, type conversion of date to String
Date date = (date) value;
return new SimpleDateFormat ("Yyyy-mm-dd"). Format (date);//output is in the form of YYYY-MM-DD
}
return null;
}
}
Create the Xwork-conversion.properties file in the root directory and add the following statement inside, registering the type converter
Java.util.date=com.great.util.dateconverter
The com.great.util.DateConverter is that the date conversion class contains the full name of the namespace.
And then a lot of people are done.
And I have not succeeded, system error
"Error (COMMONSLOGGER.JAVA:27)-Conversion Registration Error"
"Java.lang.ClassNotFoundException:com.great.util.DateConverter"
Registration type converter not successful?
A careful examination found that "java.util.date=com.great.util.dateconverter" behind a space! The truth. Remove space, run again, success!