1 34 35 36 package com.micronova.util.codec; 37 38 import java.util.*; 39 import java.text.*; 40 41 import com.micronova.util.*; 42 43 public class CodecFormat extends Codec 44 { 45 public static Object date(Object object, Object patternSpec, Object localeSpec, Object timeZoneSpec) throws Exception 46 { 47 if (object != null) 48 { 49 String pattern = TypeUtil.isString(patternSpec); 50 51 Locale locale = TypeUtil.isLocale(localeSpec); 52 TimeZone timeZone = TypeUtil.isTimeZone(timeZoneSpec); 53 54 Date date = TypeUtil.isDate(object, pattern, locale, timeZone); 55 56 if (date != null) 57 { 58 SimpleDateFormat simpleDateFormat; 59 60 if (pattern == null) 61 { 62 simpleDateFormat = new SimpleDateFormat(); 63 } 64 else 65 { 66 simpleDateFormat = new SimpleDateFormat(pattern, locale); 67 } 68 69 if (timeZone != null) 70 { 71 simpleDateFormat.setTimeZone(timeZone); 72 } 73 74 StringBuffer buffer = new StringBuffer (); 75 FieldPosition fieldPosition = new FieldPosition(0); 76 77 buffer = simpleDateFormat.format(date, buffer, fieldPosition); 78 79 object = buffer.toString(); 80 } 81 else 82 { 83 object = null; 84 } 85 } 86 87 return object; 88 } 89 90 public static Object date(Object object, Object patternSpec, Object localeSpec) throws Exception 91 { 92 return date(object, patternSpec, localeSpec, null); 93 } 94 95 public static Object date(Object object, Object patternSpec) throws Exception 96 { 97 return date(object, patternSpec, null); 98 } 99 100 public static Object date(Object object) throws Exception 101 { 102 return date(object, null, null); 103 } 104 105 public static Object number(Object object, Object patternSpec, Object localeSpec) throws Exception 106 { 107 if (object != null) 108 { 109 String pattern = TypeUtil.isString(patternSpec); 110 111 Locale locale = TypeUtil.isLocale(localeSpec); 112 113 Number number = TypeUtil.isNumber(object, pattern, locale); 114 115 if (number != null) 116 { 117 DecimalFormat decimalFormat; 118 119 if (pattern == null) 120 { 121 decimalFormat = new DecimalFormat(); 122 } 123 else 124 { 125 decimalFormat = new DecimalFormat(pattern, new DecimalFormatSymbols(locale)); 126 } 127 128 StringBuffer buffer = new StringBuffer (); 129 FieldPosition fieldPosition = new FieldPosition(0); 130 131 buffer = decimalFormat.format(number, buffer, fieldPosition); 132 133 object = buffer.toString(); 134 } 135 else 136 { 137 object = null; 138 } 139 } 140 141 return object; 142 } 143 144 145 public static Object number(Object object, Object patternSpec) throws Exception 146 { 147 return number(object, patternSpec, null); 148 } 149 150 public static Object number(Object object) throws Exception 151 { 152 return number(object, null, null); 153 } 154 } 155 | Popular Tags |