Date Conversion Class Dateconverter.java

Source: Internet
Author: User

  1. Package com.util;
  2. Import Java.text.DateFormat;
  3. Import Java.text.SimpleDateFormat;
  4. Import Java.util.Date;
  5. Import Java.util.Map;
  6. Import Com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
  7. /**
  8. * Date Conversion class
  9. *
  10. */
  11. Public class DateConverter extends Defaulttypeconverter {
  12. private Static final dateformat[] accept_date_formats = {
  13. New SimpleDateFormat ("dd/mm/yyyy"),
  14. New SimpleDateFormat ("Yyyy-mm-dd"),
  15. New SimpleDateFormat ("Yyyy/mm/dd")}; //Date format to support conversion
  16. @Override
  17. Public Object Convertvalue (Map context, object value, Class totype) {
  18. if (ToType = = Date. Class) { //The conversion of string to date when the browser submits to the server
  19. Date date = null;
  20. String datestring = null;
  21. String[] params = (string[]) value;
  22. datestring = params[0]; Gets the date of the string
  23. For (DateFormat format:accept_date_formats) {
  24. try {
  25. return Format.parse (datestring); //Traversal date support format for conversion
  26. } catch (Exception e) {
  27. continue;
  28. }
  29. }
  30. return null;
  31. }
  32. Else if (ToType = = String. Class) { //the type conversion of date to string when the server outputs to the browser
  33. Date date = (date) value;
  34. return new SimpleDateFormat ("YYYY-MM-DD"). Format (date); The format of the output is Yyyy-mm-dd
  35. }
  36. return null;
  37. }
  38. }

Date Conversion Class Dateconverter.java

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.