http://www.iteye.com/problems/675
1. package test;
2.
3. import java.util.Date;
4. import net.sf.json.JSONObject;
5.
6. public class Person {
7. private Date birthday;
8.
9. public Date getBirthday() {
10. return birthday;
11. }
12.
13. public void setBirthday(Date birthday) {
14. this.birthday = birthday;
15. }
16.
17. public static Person getInstance(String jsonValue) {
18. JSONObject obj=JSONObject.fromObject(jsonValue);
19.
20. //将birthday属性获取到
21. String strDate=(String)obj.get("birthday");
22. Date newDate=null;
23.
24. //设置日期转换的格式
25. java.text.DateFormat formate = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
26. try {
27. newDate = formate.parse(strDate);
28. }catch (Exception e) {
29. e.printStackTrace();
30. }
31. //移除原有的birthday属性
32. obj.remove("birthday");
33. //将日期类型的birthday放到obj中
34. obj.put("birthday", newDate);
35. return (Person)JSONObject.toBean(obj,Person.class);
36. }
37.
38. public static void main(String[] args) {
39. String personJson = "{birthday:\"2008-06-28 17:00:00\"}";
40.
41. Person p = getInstance(personJson);
42.
43. System.out.println("Person's brithday: "+p.getBirthday());
44. }
45. }