001: /*
002: * (C) Copyright 2004 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.util.i18n;
022:
023: import java.text.DateFormat;
024: import java.text.SimpleDateFormat;
025: import java.util.HashMap;
026: import java.util.Locale;
027:
028: /**
029: *
030: *
031: * @author Padmanabh Dabke
032: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
033: */
034: public class DateTimeFormatUtil {
035: private static String dfuDefaultDatePattern = "MM/dd/yyyy";
036: private static SimpleDateFormat dfuDefaultDateFormat = new SimpleDateFormat(
037: dfuDefaultDatePattern);
038: private static HashMap dfuDateFormatMap = new HashMap();
039:
040: private static String dfuDefaultDateTimePattern = "MM/dd/yyyy hh:mm a";
041: private static SimpleDateFormat dfuDefaultDateTimeFormat = new SimpleDateFormat(
042: dfuDefaultDateTimePattern);
043: private static HashMap dfuDateTimeFormatMap = new HashMap();
044:
045: // Date related functions
046: public static void setDefaultDateFormatPattern(String pattern) {
047: dfuDefaultDateFormat = new SimpleDateFormat(pattern);
048: dfuDefaultDateFormat.setLenient(true);
049: dfuDefaultDatePattern = pattern;
050: }
051:
052: @SuppressWarnings("unchecked")
053: public static void setDateFormatPattern(Locale l, String pattern) {
054: SimpleDateFormat f = new SimpleDateFormat(pattern);
055: f.setLenient(true);
056: dfuDateFormatMap.put(l, f);
057: }
058:
059: public static DateFormat getDateFormat(Locale l) {
060: if (l == null)
061: return dfuDefaultDateFormat;
062: DateFormat f = (DateFormat) dfuDateFormatMap.get(l);
063: if (f == null) {
064: return dfuDefaultDateFormat;
065: } else {
066: return f;
067: }
068: }
069:
070: public static String getDateFormatPattern(Locale l) {
071: if (l == null)
072: return dfuDefaultDatePattern;
073: SimpleDateFormat f = (SimpleDateFormat) dfuDateFormatMap.get(l);
074: if (f == null) {
075: return dfuDefaultDatePattern;
076: } else {
077: return f.toPattern();
078: }
079: }
080:
081: public static void setDefaultDateTimeFormatPattern(String pattern) {
082: dfuDefaultDateTimeFormat = new SimpleDateFormat(pattern);
083: dfuDefaultDateTimeFormat.setLenient(true);
084: dfuDefaultDateTimePattern = pattern;
085: }
086:
087: @SuppressWarnings("unchecked")
088: public static void setDateTimeFormatPattern(Locale l, String pattern) {
089: SimpleDateFormat f = new SimpleDateFormat(pattern);
090: f.setLenient(true);
091: dfuDateTimeFormatMap.put(l, f);
092: }
093:
094: public static DateFormat getDateTimeFormat(Locale l) {
095: if (l == null)
096: return dfuDefaultDateTimeFormat;
097: DateFormat f = (DateFormat) dfuDateTimeFormatMap.get(l);
098: if (f == null) {
099: return dfuDefaultDateTimeFormat;
100: } else {
101: return f;
102: }
103: }
104:
105: public static String getDateTimeFormatPattern(Locale l) {
106: if (l == null)
107: return dfuDefaultDateTimePattern;
108: SimpleDateFormat f = (SimpleDateFormat) dfuDateTimeFormatMap
109: .get(l);
110: if (f == null) {
111: return dfuDefaultDateTimePattern;
112: } else {
113: return f.toPattern();
114: }
115: }
116:
117: }
|