001: // TimeFormatter.java
002: // $Id: TimeFormatter.java,v 1.5 2000/08/16 21:37:58 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.util;
007:
008: import java.util.Date;
009:
010: /**
011: * This class does date formatting using the same format strings accepted by the
012: * strftime(3) UNIX call. This class has static methods only.
013: * @author <a href="mail:anto@w3.org">Antonio Ramírez</a>
014: */
015:
016: public class TimeFormatter {
017:
018: private static String[] fullWeekDays = { "Sunday", "Monday",
019: "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
020:
021: private static String[] abrWeekDays = { "Sun", "Mon", "Tue", "Wed",
022: "Thu", "Fri", "Sat" };
023:
024: private static String[] fullMonths = { "January", "February",
025: "March", "April", "May", "June", "July", "August",
026: "September", "October", "November", "December" };
027:
028: private static String[] abrMonths = { "Jan", "Feb", "Mar", "Apr",
029: "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
030:
031: /**
032: * Format the given date as a string, according to the given
033: * format string.
034: * The format string is of the form used by the strftime(3) UNIX
035: * call.
036: * @param date The date to format
037: * @param format The formatting string
038: * @return the String with the formatted date. */
039: public static String format(Date date, String format) {
040: StringBuffer buf = new StringBuffer(50);
041: char ch;
042: for (int i = 0; i < format.length(); i++) {
043: ch = format.charAt(i);
044: if (ch == '%') {
045: ++i;
046: if (i == format.length())
047: break;
048: ch = format.charAt(i);
049: if (ch == 'E') {
050: // Alternate Era
051: ++i;
052: } else if (ch == 'Q') {
053: // Alternate numeric symbols
054: ++i;
055: }
056: if (i == format.length())
057: break;
058: ch = format.charAt(i);
059: switch (ch) {
060: case 'A':
061: buf.append(fullWeekDays[date.getDay()]);
062: break;
063: case 'a':
064: buf.append(abrWeekDays[date.getDay()]);
065: break;
066:
067: case 'B':
068: buf.append(fullMonths[date.getMonth()]);
069: break;
070:
071: case 'b':
072: case 'h':
073: buf.append(abrMonths[date.getMonth()]);
074: break;
075:
076: case 'C':
077: appendPadded(buf, (date.getYear() + 1900) / 100, 2);
078: break;
079:
080: case 'c':
081: buf.append(date.toLocaleString());
082: break;
083:
084: case 'D':
085: buf.append(TimeFormatter.format(date, "%m/%d/%y"));
086: break;
087:
088: case 'd':
089: appendPadded(buf, date.getDate(), 2);
090: break;
091:
092: case 'e':
093: appendPadded(buf, date.getMonth() + 1, 2, ' ');
094: break;
095:
096: case 'H':
097: appendPadded(buf, date.getHours(), 2);
098: break;
099:
100: case 'I':
101: case 'l':
102: int a = date.getHours() % 12;
103: if (a == 0)
104: a = 12;
105: appendPadded(buf, a, 2, ch == 'I' ? '0' : ' ');
106: break;
107:
108: case 'j':
109: buf.append("[?]");
110: // No simple way to get this as of now
111: break;
112:
113: case 'k':
114: appendPadded(buf, date.getHours(), 2, ' ');
115: break;
116:
117: case 'M':
118: appendPadded(buf, date.getMinutes(), 2);
119: break;
120:
121: case 'm':
122: appendPadded(buf, date.getMonth() + 1, 2);
123: break;
124:
125: case 'n':
126: buf.append('\n');
127: break;
128:
129: case 'p':
130: buf.append(date.getHours() < 12 ? "am" : "pm");
131: break;
132:
133: case 'R':
134: buf.append(TimeFormatter.format(date, "%H:%M"));
135: break;
136:
137: case 'r':
138: buf.append(TimeFormatter.format(date, "%l:%M%p"));
139: break;
140:
141: case 'S':
142: appendPadded(buf, date.getSeconds(), 2);
143: break;
144:
145: case 'T':
146: buf.append(TimeFormatter.format(date, "%H:%M:%S"));
147: break;
148:
149: case 't':
150: buf.append('\t');
151: break;
152:
153: case 'U':
154: case 'u':
155: case 'V':
156: case 'W':
157: buf.append("[?]");
158: // Weekdays are a pain, especially
159: // without day of year (0-365) ;
160: break;
161:
162: case 'w':
163: buf.append(date.getDay());
164: break;
165:
166: case 'X':
167: buf.append(TimeFormatter.format(date, "%H:%M:%S"));
168: break;
169:
170: case 'x':
171: buf.append(TimeFormatter.format(date, "%B %e, %Y"));
172: break;
173:
174: case 'y':
175: appendPadded(buf, (date.getYear() + 1900) % 100, 2);
176: break;
177:
178: case 'Y':
179: appendPadded(buf, (date.getYear() + 1900), 4);
180: break;
181:
182: case 'Z':
183: String strdate = date.toString();
184: buf.append(strdate.substring(20, 23));
185: // (!)
186: // There should be a better way
187: // to do this...
188: break;
189: case '%':
190: buf.append('%');
191: break;
192:
193: }
194: } else {
195: buf.append(ch);
196: }
197: }
198: return buf.toString();
199: }
200:
201: private static void appendPadded(StringBuffer buf, int n,
202: int digits, char pad) {
203: String foo = String.valueOf(n).trim();
204: for (int i = 0; i < digits - foo.length(); i++)
205: buf.append(pad);
206: buf.append(foo);
207: }
208:
209: private static final void appendPadded(StringBuffer buf, int n,
210: int digits) {
211: appendPadded(buf, n, digits, '0');
212: }
213:
214: /**
215: * For testing purposes
216: */
217: public static void main(String[] args) {
218: try {
219: System.out.println(TimeFormatter
220: .format(new Date(), args[0]));
221: } catch (Exception ex) {
222: ex.printStackTrace();
223: }
224: }
225: }
|