001: // ***************************************************************
002: // * *
003: // * File: DateTimeInfo.java *
004: // * *
005: // * Copyright (c) 2001 Sun Microsystems, Inc. *
006: // * All rights reserved. *
007: // * *
008: // * *
009: // * Date - Dec/11/2001 *
010: // * Author - alejandro.abdelnur@sun.com *
011: // * *
012: // ***************************************************************
013:
014: package com.sun.portal.common.util;
015:
016: import java.util.Map;
017: import java.util.HashMap;
018: import java.util.Locale;
019: import java.util.Date;
020: import java.util.Calendar;
021: import java.util.ResourceBundle;
022:
023: import java.text.DateFormat;
024:
025: /**
026: *
027: * @author <A HREF="mailto:alejandro.abdelnur@sun.com">Alejandro Abdelnur</A>
028: *
029: */
030: public class DateTimeInfo {
031:
032: private static final Map _store = new HashMap();
033:
034: public static DateTimeInfo getInstance() {
035: return getInstance(Locale.getDefault());
036: }
037:
038: public static DateTimeInfo getInstance(Locale locale) {
039: DateTimeInfo dti = (DateTimeInfo) _store.get(locale);
040: if (dti == null) {
041: synchronized (_store) {
042: dti = (DateTimeInfo) _store.get(locale);
043: if (dti == null) {
044: dti = new DateTimeInfo(locale);
045: _store.put(locale, dti);
046: }
047: }
048: }
049: return dti;
050: }
051:
052: private static final String[] DAYS = { "1", "2", "3", "4", "5",
053: "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
054: "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
055: "26", "27", "28", "29", "30", "31" };
056:
057: private static long _lastCalculation = 0;
058: private static String[] YEARS;
059:
060: private static final String[] HOURS_24 = { "0", "1", "2", "3", "4",
061: "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
062: "15", "16", "17", "18", "19", "20", "21", "22", "23" };
063:
064: private static final String[] HOURS_12 = { "1", "2", "3", "4", "5",
065: "6", "7", "8", "9", "10", "11", "12" };
066:
067: private static final String[] MINUTES = { "00", "01", "02", "03",
068: "04", "05", "06", "07", "08", "09", "10", "11", "12", "13",
069: "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
070: "24", "25", "26", "27", "28", "29", "30", "31", "32", "33",
071: "34", "35", "36", "37", "38", "39", "40", "41", "42", "43",
072: "44", "45", "46", "47", "48", "49", "50", "51", "52", "53",
073: "54", "55", "56", "57", "58", "59" };
074:
075: private static final String[] SECONDS = MINUTES;
076:
077: private static final String BUNDLE_NAME = "java.text.resources.LocaleElements";
078:
079: private Locale _locale;
080: private int[] _dateOrder;
081: private String[] _abbrMonths;
082: private String[] _months;
083: private String[] _days;
084: private boolean _24Hours;
085: private String[] _hours;
086: private String[] _minutes;
087: private String[] _seconds;
088: private String[] _amPmMarkers;
089: private int[] _timeOrder;
090: private char _dateElementSeparator;
091:
092: private DateTimeInfo(Locale locale) {
093: _locale = locale;
094:
095: ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME,
096: _locale);
097: _dateOrder = obtainDateOrder(bundle);
098: _abbrMonths = obtainAbbrMonths(bundle);
099: _months = obtainMonths(bundle);
100: _days = DAYS;
101: _24Hours = obtainIs24Hours(bundle);
102: _hours = (_24Hours) ? HOURS_24 : HOURS_12;
103: _minutes = MINUTES;
104: _seconds = SECONDS;
105: _amPmMarkers = obtainAmPmMarkers(bundle);
106: _timeOrder = obtainTimeOrder();
107: _dateElementSeparator = obtainDateElementSeparator(bundle);
108: }
109:
110: public Locale getLocale() {
111: return _locale;
112: }
113:
114: public int[] getDateOrder() {
115: return _dateOrder;
116: }
117:
118: public String[] getAbbrMonths() {
119: return _abbrMonths;
120: }
121:
122: public String[] getMonths() {
123: return _months;
124: }
125:
126: public String[] getDays() {
127: return _days;
128: }
129:
130: public String[] getYears() {
131: if ((System.currentTimeMillis() - _lastCalculation) > (1000L * 60 * 60 * 24)) {
132: recalculateYears();
133: }
134: return YEARS;
135: }
136:
137: public String[] getYears(int begin, int howMany) {
138: String[] years = new String[howMany];
139: for (int i = 0; i < years.length; i++) {
140: years[i] = Integer.toString(begin++);
141: }
142: return years;
143: }
144:
145: public int[] getTimeOrder() {
146: return _timeOrder;
147: }
148:
149: public char getDateElementSeparator() {
150: return _dateElementSeparator;
151: }
152:
153: public boolean is24Hours() {
154: return _24Hours;
155: }
156:
157: public String[] getHours() {
158: return _hours;
159: }
160:
161: public String[] getMinutes() {
162: return _minutes;
163: }
164:
165: public String[] getSeconds() {
166: return _seconds;
167: }
168:
169: public String[] getAmPmMarkers() {
170: return _amPmMarkers;
171: }
172:
173: public int toIndexValue(int section, int value) {
174: switch (section) {
175: case Calendar.DAY_OF_MONTH:
176: case Calendar.HOUR:
177: value--;
178: break;
179: default:
180: break;
181: }
182: return value;
183: }
184:
185: public int fromStringValue(int section, String str) {
186: int value = -1;
187: switch (section) {
188: case Calendar.YEAR:
189: value = Integer.parseInt(str);
190: break;
191: case Calendar.MONTH:
192: /*
193: for (int i=0;value==-1 && i<_abbrMonths.length;i++) {
194: if (_abbrMonths[i].equalsIgnoreCase(str)) {
195: value = i;
196: }
197: }
198: */
199: value = Integer.parseInt(str) - 1;
200: break;
201: case Calendar.DAY_OF_MONTH:
202: value = Integer.parseInt(str);
203: break;
204: case Calendar.HOUR:
205: value = Integer.parseInt(str);
206: break;
207: case Calendar.HOUR_OF_DAY:
208: value = Integer.parseInt(str);
209: break;
210: case Calendar.MINUTE:
211: value = Integer.parseInt(str);
212: break;
213: case Calendar.SECOND:
214: value = Integer.parseInt(str);
215: break;
216: case Calendar.AM_PM:
217: for (int i = 0; value == -1 && i < _amPmMarkers.length; i++) {
218: if (_amPmMarkers[i].equalsIgnoreCase(str)) {
219: value = i;
220: }
221: }
222: break;
223: default:
224: break;
225: }
226: return value;
227: }
228:
229: // --- private initialization stuff
230:
231: private synchronized void recalculateYears() {
232: _lastCalculation = System.currentTimeMillis();
233: Date date = new Date();
234: Calendar calendar = Calendar.getInstance();
235: calendar.setTime(date);
236: int year = calendar.get(calendar.YEAR);
237: YEARS = new String[10];
238: for (int i = 0; i < YEARS.length; i++) {
239: YEARS[i] = Integer.toString(year++);
240: }
241: }
242:
243: private int[] obtainDateOrder(ResourceBundle bundle) {
244: String[] patterns = bundle.getStringArray("DateTimePatterns");
245: char[] datePattern = patterns[7].toCharArray();
246: int[] localeOrder = new int[4];
247: for (int i = 0; i < localeOrder.length; i++) {
248: localeOrder[i] = -1;
249: }
250:
251: int pos = 0;
252: for (int i = 0; pos < localeOrder.length
253: && i < datePattern.length; i++) {
254: switch (datePattern[i]) {
255: case 'M':
256: if (pos == 0 || localeOrder[pos - 1] != Calendar.MONTH) {
257: localeOrder[pos++] = Calendar.MONTH;
258: }
259: break;
260: case 'd':
261: if (pos == 0
262: || localeOrder[pos - 1] != Calendar.DAY_OF_MONTH) {
263: localeOrder[pos++] = Calendar.DAY_OF_MONTH;
264: }
265: break;
266: case 'y':
267: if (pos == 0 || localeOrder[pos - 1] != Calendar.YEAR) {
268: localeOrder[pos++] = Calendar.YEAR;
269: }
270: break;
271: default:
272: break;
273: }
274: }
275: return localeOrder;
276: }
277:
278: private char obtainDateElementSeparator(ResourceBundle bundle) {
279: char separator = '/';
280: String[] patterns = bundle.getStringArray("DateTimePatterns");
281: char[] datePattern = patterns[7].toCharArray();
282: boolean found = false;
283: for (int i = 0; i < datePattern.length && !found; i++) {
284: char c = datePattern[i];
285: if (c != 'd' && c != 'M' && c != 'y') {
286: separator = c;
287: found = true;
288: }
289: }
290: return separator;
291: }
292:
293: private String[] obtainAbbrMonths(ResourceBundle bundle) {
294: String[] months = bundle.getStringArray("MonthAbbreviations");
295: String[] realMonths = new String[12];
296: System.arraycopy(months, 0, realMonths, 0, realMonths.length);
297: return realMonths;
298: }
299:
300: private String[] obtainMonths(ResourceBundle bundle) {
301: String[] months = bundle.getStringArray("MonthNames");
302: String[] realMonths = new String[12];
303: System.arraycopy(months, 0, realMonths, 0, realMonths.length);
304: return realMonths;
305: }
306:
307: private boolean obtainIs24Hours(ResourceBundle bundle) {
308: String hourPattern = bundle.getStringArray("DateTimePatterns")[2];
309: return (hourPattern.indexOf("H") > -1);
310: }
311:
312: private String[] obtainAmPmMarkers(ResourceBundle bundle) {
313: return bundle.getStringArray("AmPmMarkers");
314: }
315:
316: private int[] obtainTimeOrder() {
317: int[] order;
318: if (is24Hours()) {
319: order = new int[3];
320: order[0] = Calendar.HOUR_OF_DAY;
321: order[1] = Calendar.MINUTE;
322: order[2] = Calendar.SECOND;
323: } else {
324: order = new int[4];
325: order[0] = Calendar.HOUR;
326: order[1] = Calendar.MINUTE;
327: order[2] = Calendar.SECOND;
328: order[3] = Calendar.AM_PM;
329: }
330: return order;
331: }
332:
333: }
|