1 3 package jodd.datetime.formatter; 4 5 import jodd.datetime.DateTimeStamp; 6 import jodd.datetime.JDateTime; 7 import jodd.format.Printf; 8 import jodd.util.LocaleUtil; 9 10 45 public class DefaultFormatter extends AbstractFormatter { 46 47 public DefaultFormatter() { 48 preparePatterns( 49 new String [] { 50 "YYYY", "MM", "DD", "D", "MML", "MMS", "DL", "DS", "hh", "mm", "ss", "mss", "ss.mss", "DDD", "WW", "WWW", "W", } 68 ); 69 } 70 71 72 protected String convertPattern(int patternIndex, JDateTime jdt) { 73 switch (patternIndex) { 74 case 0: 75 return Printf.str("%~04i", jdt.getYear()); 76 case 1: 77 return Printf.str("%02i", jdt.getMonth()); 78 case 2: 79 return Printf.str("%02i", jdt.getDay()); 80 case 3: 81 return Integer.toString(jdt.getDayOfWeek()); 82 case 4: 83 String [] data = LocaleUtil.getMonthNames(jdt.getLocale()); 84 return data[jdt.getMonth() - 1]; 85 case 5: 86 data = LocaleUtil.getMonthAbbreviations(jdt.getLocale()); 87 return data[jdt.getMonth() - 1]; 88 case 6: 89 data = LocaleUtil.getDayNames(jdt.getLocale()); 90 return data[jdt.getDayOfWeek() % 7]; 91 case 7: 92 data = LocaleUtil.getDayAbbreviations(jdt.getLocale()); 93 return data[jdt.getDayOfWeek() % 7]; 94 case 8: 95 return Printf.str("%02d", jdt.getHour()); 96 case 9: 97 return Printf.str("%02d", jdt.getMinute()); 98 case 10: 99 return Printf.str("%02d", (int)jdt.getSecond()); 100 case 11: 101 return Printf.str("%02d", jdt.getMillisecond()); 102 case 12: 103 return Printf.str("%06.3f", jdt.getSecond()); 104 case 13: 105 return Printf.str("%03d", jdt.getDayOfYear()); 106 case 14: 107 return Printf.str("%02d", jdt.getWeekOfYear()); 108 case 15: 109 return Printf.str("W%02d", jdt.getWeekOfYear()); 110 case 16: 111 return Integer.toString(jdt.getWeekOfMonth()); 112 default: 113 return new String (patterns[patternIndex]); 114 } 115 } 116 117 118 protected void parseValue(int i, String value, DateTimeStamp time) { 119 int v = 0; 120 double vd = 0; 121 if (i != 12) { v = Integer.parseInt(value); 123 if (i == 11) { 124 vd = 1.0; 125 for (int u = 0; u < value.length(); u++) { 126 vd *= 10.0; 127 } 128 } 129 } else { vd = Double.parseDouble(value); 131 vd += 1e-9; 132 } 133 switch (i) { 134 case 0: time.year = v; break; 135 case 1: time.month = v; break; 136 case 2: time.day = v; break; 137 case 8: time.hour = v; break; 138 case 9: time.minute = v; break; 139 case 10: time.second += v; break; 140 case 11: time.second += v/vd; break; 141 case 12: time.second = vd; break; 142 } 143 } 144 } 145 | Popular Tags |