How to resolve Struts2 Date type conversion _java

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.