001: /*
002: * @(#)DateTimeFormat.java 1.2 04/12/06
003: *
004: * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.lib;
010:
011: import java.util.Date;
012: import java.util.Calendar;
013: import java.text.DateFormat;
014: import pnuts.lang.Context;
015:
016: class DateTimeFormat {
017: final static String DATEFORMAT_CACHE = "pnuts$lib$dateformat_cache"
018: .intern();
019: final static String DATETIME_FORMAT = "pnuts$lib$datetime_format"
020: .intern();
021: final static String DATE_FORMAT = "pnuts$lib$date_format".intern();
022: final static String TIME_FORMAT = "pnuts$lib$time_format".intern();
023: final static String DEFAULT = "DEFAULT";
024: final static String defaultDatePattern = "MM/dd/yyyy";
025:
026: static void reset(Context context) {
027: context.set(DATEFORMAT_CACHE, null);
028: context.set(DATETIME_FORMAT, null);
029: context.set(DATE_FORMAT, null);
030: context.set(TIME_FORMAT, null);
031: }
032:
033: static DateFormat getDateFormat(String pattern, Context context) {
034: DateFormatCache cache;
035: synchronized (context) {
036: cache = (DateFormatCache) context.get(DATEFORMAT_CACHE);
037: if (cache == null) {
038: cache = new DateFormatCache(context);
039: context.set(DATEFORMAT_CACHE, cache);
040: }
041: }
042: return cache.get(pattern);
043: }
044:
045: static Date parse(String expr, Context context) {
046: return parse(expr, defaultDatePattern, context);
047: }
048:
049: static Date parse(String expr, String pattern, Context context) {
050: try {
051: return getDateFormat(pattern, context).parse(expr);
052: } catch (Exception e) {
053: return null;
054: }
055: }
056:
057: static DateFormat getDateTimeFormat(String type, Context context) {
058: synchronized (context) {
059: DateFormat df = (DateFormat) context.get(type);
060: if (df == null) {
061: df = DateFormat.getDateTimeInstance();
062: df.setTimeZone(date.getTimeZone(context));
063: context.set(type, df);
064: }
065: return df;
066: }
067: }
068:
069: static String formatDateTime(Date date, Context context) {
070: return getDateTimeFormat(DATETIME_FORMAT, context).format(date);
071: }
072:
073: static String formatDateTime(Date date, String pattern,
074: Context context) {
075: return getDateFormat(pattern, context).format(date);
076: }
077:
078: static String formatDateTime(Date d, String style1, String style2,
079: Context context) {
080: DateFormat dateFormat = DateFormat.getDateTimeInstance(
081: dateStyle(style1), dateStyle(style2), locale
082: .getLocale(context));
083: dateFormat.setTimeZone(date.getTimeZone(context));
084: return dateFormat.format(d);
085: }
086:
087: static String formatDate(Date date, Context context) {
088: return getDateTimeFormat(DATE_FORMAT, context).format(date);
089: }
090:
091: static String formatDate(Date d, String style, Context context) {
092: DateFormat dateFormat = DateFormat.getDateInstance(
093: dateStyle(style), locale.getLocale(context));
094: dateFormat.setTimeZone(date.getTimeZone(context));
095: return dateFormat.format(d);
096: }
097:
098: static String formatTime(Date date, Context context) {
099: return getDateTimeFormat(TIME_FORMAT, context).format(date);
100: }
101:
102: static String formatTime(Date d, String style, Context context) {
103: DateFormat dateFormat = DateFormat.getTimeInstance(
104: dateStyle(style), locale.getLocale(context));
105: dateFormat.setTimeZone(date.getTimeZone(context));
106: return dateFormat.format(d);
107: }
108:
109: private static int dateStyle(String s) {
110: s = s.toLowerCase();
111:
112: if ("full".equals(s)) {
113: return DateFormat.FULL;
114: }
115: if ("long".equals(s)) {
116: return DateFormat.LONG;
117: }
118: if ("medium".equals(s)) {
119: return DateFormat.MEDIUM;
120: }
121: if ("short".equals(s)) {
122: return DateFormat.SHORT;
123: }
124: return DateFormat.DEFAULT;
125: }
126: }
|