KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > datetime > formatter > DefaultFormatter


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

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 /**
11  * Default {@link JdtFormatter} uses <b>ISO 8601</b> specification, enhanced by some
12  * custom patterns. For more info see:
13  * <a HREF="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601 on Wikipedia</a>
14  *
15  * <p>
16  * Patterns list:
17  *
18  * <ul>
19  * <li>YYYY + year</li>
20  * <li>MM + month</li>
21  * <li>DD + day of month</li>
22  * <li>D - day of week</li>
23  * <li>MML - month name (addon)</li>
24  * <li>MMS - month abbreviation (addon)</li>
25  * <li>DL - day of week name (addon)</li>
26  * <li>DS - day of week abbreviation (addon)</li>
27  * <li>hh + hour</li>
28  * <li>mm + minute</li>
29  * <li>ss + seconds (no milliseconds)</li>
30  * <li>mss + milliseconds (addon)</li>
31  * <li>ss.mss + seconds.milliseconds (addon)</li>
32  * <li>DDD - day of year</li>
33  * <li>WW - week of year</li>
34  * <li>WWW - week of year with 'W' prefix</li>
35  * <li>W - week of month (addon)</li>
36  * </ul>
37  *
38  * <p>
39  * Patterns notted with + sign are used both for conversion and parsing,
40  * but all patterns are used for conversion.
41  * <p>
42  * todo Add more patterns for timezone etc
43  * todo Add new formater for SimpleDateFormatter-alike patterns
44  */

45 public class DefaultFormatter extends AbstractFormatter {
46
47     public DefaultFormatter() {
48         preparePatterns(
49                 new String JavaDoc[] {
50                         "YYYY", // 0 + year
51
"MM", // 1 + month
52
"DD", // 2 + day of month
53
"D", // 3 - day of week
54
"MML", // 4 - month long name
55
"MMS", // 5 - month short name
56
"DL", // 6 - day of week long name
57
"DS", // 7 - day of week short name
58
"hh", // 8 + hour
59
"mm", // 9 + minute
60
"ss", // 10 + seconds (no milliseconds)
61
"mss", // 11 + milliseconds
62
"ss.mss", // 12 + seconds.milliseconds
63
"DDD", // 13 - day of year
64
"WW", // 14 - week of year
65
"WWW", // 15 - week of year with 'W' prefix
66
"W", // 16 - week of month
67
}
68         );
69     }
70
71
72     protected String JavaDoc 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 JavaDoc[] 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 JavaDoc(patterns[patternIndex]);
114         }
115     }
116
117
118     protected void parseValue(int i, String JavaDoc value, DateTimeStamp time) {
119         int v = 0;
120         double vd = 0;
121         if (i != 12) { // not double
122
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 { // double
130
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