Writing tool Classes
[Java]View Plaincopy
- Import Java.text.DateFormat;
- Import Java.text.SimpleDateFormat;
- Import java.util.Date;
- Import Net.sf.json.JsonConfig;
- Import Net.sf.json.processors.JsonValueProcessor;
- Public class datejsonvalueprocessor implements Jsonvalueprocessor {
- /**
- * Letter Date or time element representation example <br>
- * G Era marker Text AD <br>
- * y year 1996; <br>
- * The month of the year in M July; Jul; <br>
- * Number of weeks in W year <br>
- * weeks in W month number 2 <br>
- * Days in D year number 189 <br>
- * Number of days in the D month <br>
- * Week number 2 in the F month <br>
- * E Week of days Text Tuesday; Tue<br>
- * A am/pm tagged Text pm <br>
- * H hours in the day (0-23) number 0 <br>
- * k hours in the day (1-24) number 24<br>
- * Hours in K am/pm (0-11) number 0 <br>
- * Hours in H am/pm (1-12) number <br>
- * minutes in M-hour number <br>
- * seconds in S minutes number <br>
- * S ms number 978 <br>
- * Z TimeZone general time zone Pacific PST; gmt-08:00 <br>
- * Z timezone RFC 822 time zone-0800 <br>
- */
- Public static final String default_date_pattern = "YYYY-MM-DD" ;
- Private DateFormat DateFormat;
- Public datejsonvalueprocessor (String datepattern) {
- Try {
- DateFormat = new SimpleDateFormat (Datepattern);
- } catch (Exception e) {
- DateFormat = new SimpleDateFormat (Default_date_pattern);
- }
- }
- Public Object Processarrayvalue (object value, Jsonconfig jsonconfig) {
- return process (value);
- }
- Public Object Processobjectvalue (String key, object value, Jsonconfig jsonconfig) {
- return process (value);
- }
- Private Object Process (object value) {
- return Dateformat.format ((Date) value);
- }
- }
To test
[Java]View Plaincopy
- Import java.util.Date;
- Import Java.util.HashMap;
- Import Java.util.Map;
- Import Net.sf.json.JSONObject;
- Import Net.sf.json.JsonConfig;
- Public class Test {
- Public static void main (string[] args) {
- map<string, object> map = new hashmap<string, object> ();
- Map.put ("Time", new Date ());
- Map.put ("name", "yy");
- Map.put ("age", +);
- Jsonconfig config = new jsonconfig ();
- /**
- * Letter Date or time element representation example <br>
- * G era marker Text AD <br>
- * y year 1996; <br>
- * The month of the year in M July; Jul; <br>
- * Number of weeks in W year <br>
- * weeks in W month number 2 <br>
- * Days in D year number 189 <br>
- * Number of days in the D month <br>
- * Week number 2 in the F month <br>
- * E Week of days Text Tuesday; Tue<br>
- * A am/pm tagged Text pm <br>
- * H hours in the day (0-23) number 0 <br>
- * k hours in the day (1-24) number 24<br>
- * Hours in K am/pm (0-11) number 0 <br>
- * Hours in H am/pm (1-12) number <br>
- * minutes in M-hour number <br>
- * seconds in S minutes number <br>
- * S ms number 978 <br>
- * Z TimeZone general time zone Pacific PST; gmt-08:00 <br>
- * Z timezone RFC 822 time zone-0800 <br>
- */
- Config.registerjsonvalueprocessor (Date. Class, new datejsonvalueprocessor ("G yyyy-mm-dd hh:mm:ss. SS zzz zzz w DDD FF EE "));
- Jsonobject OBJ = jsonobject.fromobject (map, config);
- System.out.println (OBJ);
- }
- }
Source: http://blog.csdn.net/heardy/article/details/6760722
Net.sf.json Date-time formatting methods