001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003: package jodd.datetime.formatter;
004:
005: import jodd.datetime.DateTimeStamp;
006: import jodd.datetime.JDateTime;
007: import jodd.format.Printf;
008: import jodd.util.LocaleUtil;
009:
010: /**
011: * Default {@link JdtFormatter} uses <b>ISO 8601</b> specification, enhanced by some
012: * custom patterns. For more info see:
013: * <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601 on Wikipedia</a>
014: *
015: * <p>
016: * Patterns list:
017: *
018: * <ul>
019: * <li>YYYY + year</li>
020: * <li>MM + month</li>
021: * <li>DD + day of month</li>
022: * <li>D - day of week</li>
023: * <li>MML - month name (addon)</li>
024: * <li>MMS - month abbreviation (addon)</li>
025: * <li>DL - day of week name (addon)</li>
026: * <li>DS - day of week abbreviation (addon)</li>
027: * <li>hh + hour</li>
028: * <li>mm + minute</li>
029: * <li>ss + seconds (no milliseconds)</li>
030: * <li>mss + milliseconds (addon)</li>
031: * <li>ss.mss + seconds.milliseconds (addon)</li>
032: * <li>DDD - day of year</li>
033: * <li>WW - week of year</li>
034: * <li>WWW - week of year with 'W' prefix</li>
035: * <li>W - week of month (addon)</li>
036: * </ul>
037: *
038: * <p>
039: * Patterns notted with + sign are used both for conversion and parsing.
040: * All patterns are used for conversion.
041: * <p>
042: * todo Add more patterns for timezone etc
043: * todo Add new formater for SimpleDateFormatter-alike patterns
044: */
045: public class DefaultFormatter extends AbstractFormatter {
046:
047: public DefaultFormatter() {
048: preparePatterns(new String[] { "YYYY", // 0 + year
049: "MM", // 1 + month
050: "DD", // 2 + day of month
051: "D", // 3 - day of week
052: "MML", // 4 - month long name
053: "MMS", // 5 - month short name
054: "DL", // 6 - day of week long name
055: "DS", // 7 - day of week short name
056: "hh", // 8 + hour
057: "mm", // 9 + minute
058: "ss", // 10 + seconds (no milliseconds)
059: "mss", // 11 + milliseconds
060: "ss.mss", // 12 + seconds.milliseconds
061: "DDD", // 13 - day of year
062: "WW", // 14 - week of year
063: "WWW", // 15 - week of year with 'W' prefix
064: "W", // 16 - week of month
065: });
066: }
067:
068: @Override
069: protected String convertPattern(int patternIndex, JDateTime jdt) {
070: switch (patternIndex) {
071: case 0:
072: return Printf.str("%~04i", jdt.getYear());
073: case 1:
074: return Printf.str("%02i", jdt.getMonth());
075: case 2:
076: return Printf.str("%02i", jdt.getDay());
077: case 3:
078: return Integer.toString(jdt.getDayOfWeek());
079: case 4:
080: String[] data = LocaleUtil.getMonthNames(jdt.getLocale());
081: return data[jdt.getMonth() - 1];
082: case 5:
083: data = LocaleUtil.getMonthAbbreviations(jdt.getLocale());
084: return data[jdt.getMonth() - 1];
085: case 6:
086: data = LocaleUtil.getDayNames(jdt.getLocale());
087: return data[jdt.getDayOfWeek() % 7];
088: case 7:
089: data = LocaleUtil.getDayAbbreviations(jdt.getLocale());
090: return data[jdt.getDayOfWeek() % 7];
091: case 8:
092: return Printf.str("%02d", jdt.getHour());
093: case 9:
094: return Printf.str("%02d", jdt.getMinute());
095: case 10:
096: return Printf.str("%02d", (int) jdt.getSecond());
097: case 11:
098: return Printf.str("%02d", jdt.getMillisecond());
099: case 12:
100: return Printf.str("%06.3f", jdt.getSecond());
101: case 13:
102: return Printf.str("%03d", jdt.getDayOfYear());
103: case 14:
104: return Printf.str("%02d", jdt.getWeekOfYear());
105: case 15:
106: return Printf.str("W%02d", jdt.getWeekOfYear());
107: case 16:
108: return Integer.toString(jdt.getWeekOfMonth());
109: default:
110: return new String(patterns[patternIndex]);
111: }
112: }
113:
114: @Override
115: protected void parseValue(int i, String value, DateTimeStamp time) {
116: int v = 0;
117: double vd = 0;
118: if (i != 12) { // not double
119: v = Integer.parseInt(value);
120: if (i == 11) {
121: vd = 1.0;
122: for (int u = 0; u < value.length(); u++) {
123: vd *= 10.0;
124: }
125: }
126: } else { // double
127: vd = Double.parseDouble(value);
128: vd += 1e-9;
129: }
130: switch (i) {
131: case 0:
132: time.year = v;
133: break;
134: case 1:
135: time.month = v;
136: break;
137: case 2:
138: time.day = v;
139: break;
140: case 8:
141: time.hour = v;
142: break;
143: case 9:
144: time.minute = v;
145: break;
146: case 10:
147: time.second += v;
148: break;
149: case 11:
150: time.second += v / vd;
151: break;
152: case 12:
153: time.second = vd;
154: break;
155: }
156: }
157: }
|