6be57e87d3d1fc914df90f9bee1d8567a43cbf79.svn-base 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package com.yihu.utils;
  2. import java.text.SimpleDateFormat;
  3. import net.sf.json.JsonConfig;
  4. import net.sf.json.processors.JsonValueProcessor;
  5. public class DateJsonValueProcessor implements JsonValueProcessor {
  6. private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
  7. private SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  8. public DateJsonValueProcessor() {
  9. }
  10. public Object processArrayValue(Object value, JsonConfig jsonConfig) {
  11. String[] obj = {};
  12. if (value instanceof java.sql.Timestamp[]) {
  13. java.sql.Timestamp[] dates = (java.sql.Timestamp[]) value;
  14. obj = new String[dates.length];
  15. for (int i = 0; i < dates.length; i++) {
  16. obj[i] = sf2.format(new java.util.Date(dates[i].getTime()));
  17. }
  18. }else if (value instanceof java.util.Date[]) {
  19. java.util.Date[] dates = (java.util.Date[]) value;
  20. obj = new String[dates.length];
  21. for (int i = 0; i < dates.length; i++) {
  22. obj[i] = sf.format(dates[i]);
  23. }
  24. }else if (value instanceof java.sql.Date[]) {
  25. java.sql.Date[] dates = (java.sql.Date[]) value;
  26. obj = new String[dates.length];
  27. for (int i = 0; i < dates.length; i++) {
  28. obj[i] = sf.format(new java.util.Date(dates[i].getTime()));
  29. }
  30. }else if (value instanceof Integer[]) {
  31. Integer[] vals = (Integer[]) value;
  32. obj = new String[vals.length];
  33. for (int i = 0; i < vals.length; i++) {
  34. obj[i] =(vals[i]==null?null:vals[i]+"") ;
  35. }
  36. }else if (value instanceof Double[]) {
  37. Double[] vals = (Double[]) value;
  38. obj = new String[vals.length];
  39. for (int i = 0; i < vals.length; i++) {
  40. obj[i] =(vals[i]==null?null:vals[i]+"") ;
  41. }
  42. }
  43. return obj;
  44. }
  45. public Object processObjectValue(String key, Object value,
  46. JsonConfig jsonConfig) {
  47. if (value instanceof java.sql.Timestamp) {
  48. String str =sf2.format(new java.util.Date(((java.sql.Timestamp) value).getTime()));
  49. return str;
  50. }else if (value instanceof java.util.Date) {
  51. String str = sf.format((java.util.Date) value);
  52. return str;
  53. }else if (value instanceof java.sql.Date) {
  54. String str =sf.format(new java.util.Date(((java.sql.Date) value).getTime()));
  55. return str;
  56. }else if(value!=null){
  57. return value.toString();
  58. }else
  59. return "";
  60. }
  61. }