Net.sf.json Date-time formatting methods

Source: Internet
Author: User
Tags dateformat rfc

Writing tool Classes

[Java]View Plaincopy
  1. Import  Java.text.DateFormat;
  2. Import  Java.text.SimpleDateFormat;
  3. Import  java.util.Date;
  4. Import  Net.sf.json.JsonConfig;
  5. Import  Net.sf.json.processors.JsonValueProcessor;
  6. Public class datejsonvalueprocessor implements Jsonvalueprocessor {
  7. /**
  8. * Letter Date or time element representation example <br>
  9. * G Era marker Text AD <br>
  10. * y year 1996; <br>
  11. * The month of the year in M July; Jul; <br>
  12. * Number of weeks in W year <br>
  13. * weeks in W month number 2 <br>
  14. * Days in D year number 189 <br>
  15. * Number of days in the D month <br>
  16. * Week number 2 in the F month <br>
  17. * E Week of days Text Tuesday; Tue<br>
  18. * A am/pm tagged Text pm <br>
  19. * H hours in the day (0-23) number 0 <br>
  20. * k hours in the day (1-24) number 24<br>
  21. * Hours in K am/pm (0-11) number 0 <br>
  22. * Hours in H am/pm (1-12) number <br>
  23. * minutes in M-hour number <br>
  24. * seconds in S minutes number <br>
  25. * S ms number 978 <br>
  26. * Z TimeZone general time zone Pacific PST; gmt-08:00 <br>
  27. * Z timezone RFC 822 time zone-0800 <br>
  28.      */   
  29. Public static final String default_date_pattern = "YYYY-MM-DD"  ;
  30. Private  DateFormat DateFormat;
  31. Public datejsonvalueprocessor (String datepattern) {
  32. Try  {  
  33. DateFormat = new SimpleDateFormat (Datepattern);
  34. } catch (Exception e) {
  35. DateFormat = new SimpleDateFormat (Default_date_pattern);
  36. }
  37. }
  38. Public Object Processarrayvalue (object value, Jsonconfig jsonconfig) {
  39. return  process (value);
  40. }
  41. Public Object Processobjectvalue (String key, object value, Jsonconfig jsonconfig) {
  42. return  process (value);
  43. }
  44. Private Object Process (object value) {
  45. return  Dateformat.format ((Date) value);
  46. }
  47. }

To test

[Java]View Plaincopy
  1. Import  java.util.Date;
  2. Import  Java.util.HashMap;
  3. Import  Java.util.Map;
  4. Import  Net.sf.json.JSONObject;
  5. Import  Net.sf.json.JsonConfig;
  6. Public class Test {
  7. Public static void main (string[] args) {
  8. map<string, object> map = new hashmap<string, object> ();
  9. Map.put ("Time", new Date ());
  10. Map.put ("name", "yy");
  11. Map.put ("age", +);
  12. Jsonconfig config = new jsonconfig ();
  13. /**
  14. * Letter Date or time element representation example <br>
  15. * G era marker Text AD <br>
  16. * y year 1996; <br>
  17. * The month of the year in M July; Jul; <br>
  18. * Number of weeks in W year <br>
  19. * weeks in W month number 2 <br>
  20. * Days in D year number 189 <br>
  21. * Number of days in the D month <br>
  22. * Week number 2 in the F month <br>
  23. * E Week of days Text Tuesday; Tue<br>
  24. * A am/pm tagged Text pm <br>
  25. * H hours in the day (0-23) number 0 <br>
  26. * k hours in the day (1-24) number 24<br>
  27. * Hours in K am/pm (0-11) number 0 <br>
  28. * Hours in H am/pm (1-12) number <br>
  29. * minutes in M-hour number <br>
  30. * seconds in S minutes number <br>
  31. * S ms number 978 <br>
  32. * Z TimeZone general time zone Pacific PST; gmt-08:00 <br>
  33. * Z timezone RFC 822 time zone-0800 <br>
  34.      */   
  35. Config.registerjsonvalueprocessor (Date. Class, new datejsonvalueprocessor ("G yyyy-mm-dd hh:mm:ss.  SS zzz zzz w DDD FF EE "));
  36. Jsonobject OBJ = jsonobject.fromobject (map, config);
  37. System.out.println (OBJ);
  38. }
  39. }

Source: http://blog.csdn.net/heardy/article/details/6760722

Net.sf.json Date-time formatting methods

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.