001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util;
011:
012: import java.util.*;
013: import java.text.*;
014:
015: import org.mmbase.util.logging.Logger;
016: import org.mmbase.util.logging.Logging;
017:
018: /**
019: * Utility function to create DateFormat instances.
020: *
021: * @author Michiel Meeuwissen
022: * @since MMBase-1.7.1
023: * @version $Id: DateFormats.java,v 1.5 2007/06/20 14:00:10 michiel Exp $
024: */
025: public class DateFormats {
026:
027: private static final Logger log = Logging
028: .getLoggerInstance(DateFormats.class);
029:
030: /**
031: * Creates a DateFormat instance, based on a String.
032: *
033: * @param format The format defining the DateFormat. This can be constants like :FULL, :FULL.FULL, :LONG, :MEDIUM or :SHORT.
034: * It can also be 'e' for weekday. Also 'RFC822' or 'rfc822' is possible then the
035: * quite complicated http://www.faqs.org/rfcs/rfc822.html compliant date and time
036: * is made, which comes in handy when you need to create a rss feed f.e.
037: * Or none of those, then a SimpleDateFormat is instantiated.
038: * @param timeZone A String describing the timeZone (see DateFormat#setTimeZone)
039: * @param locale Most DateFormat's need a Locale too.
040: * @throws IllegalArgumentException
041: */
042: public static DateFormat getInstance(String format,
043: String timeZone, Locale locale) {
044: DateFormat df;
045: if (format.length() > 0 && format.charAt(0) == ':') {
046: log.debug("found symbolic format");
047: if (format.charAt(1) == '.') {
048: df = DateFormat
049: .getTimeInstance(getDateFormatStyle(format
050: .substring(2)), locale);
051: } else if (format.indexOf('.') == -1) {
052: df = DateFormat
053: .getDateInstance(getDateFormatStyle(format
054: .substring(1)), locale);
055: } else {
056: int i = format.indexOf('.');
057: df = DateFormat.getDateTimeInstance(
058: getDateFormatStyle(format.substring(1, i)),
059: getDateFormatStyle(format.substring(i + 1)),
060: locale);
061: }
062: } else if (format.equals("e")) {
063: df = new DayOfWeekDateFormat();
064: } else if (format.equals("RFC822") || format.equals("rfc822")) {
065: df = new SimpleDateFormat("EE, dd MMM yyyy hh:mm:ss Z",
066: Locale.US);
067: timeZone = "UTC";
068: } else {
069: df = new SimpleDateFormat(format, locale);
070: }
071: if (!(timeZone == null || timeZone.equals(""))) {
072: df.setTimeZone(TimeZone.getTimeZone(timeZone));
073: } else {
074: df
075: .setTimeZone(org.mmbase.util.dateparser.DateParser.defaultTimeZone);
076: }
077: return df;
078:
079: }
080:
081: /**
082: * Converts a string to a DateFormat constant.
083: *
084: * @param style A string describing the dateformat style (FULL, LONG, MEDIUM, SHORT)
085: * @return A DateFormat style constant.
086: * @see java.text.DateFormat
087: */
088: private static int getDateFormatStyle(String style) {
089: if ("FULL".equals(style)) {
090: return DateFormat.FULL;
091: } else if ("LONG".equals(style)) {
092: return DateFormat.LONG;
093: } else if ("MEDIUM".equals(style)) {
094: return DateFormat.MEDIUM;
095: } else if ("SHORT".equals(style)) {
096: return DateFormat.SHORT;
097: } else {
098: throw new IllegalArgumentException(
099: "Unknown DateFormat Style " + style);
100: }
101: }
102:
103: /**
104: * There is no DateFormat which can return the day of the week as a number available in
105: * java.text package. This provides one.
106: */
107:
108: protected static class DayOfWeekDateFormat extends DateFormat {
109: private TimeZone zone = null;
110:
111: public Date parse(String source, ParsePosition pos) {
112: Calendar calendar = Calendar
113: .getInstance(zone != null ? zone
114: : org.mmbase.util.dateparser.DateParser.defaultTimeZone);
115: int day = source.charAt(0) - '0';
116: pos.setIndex(pos.getIndex() + 1);
117: calendar.set(Calendar.DAY_OF_WEEK, day);
118: return calendar.getTime();
119: }
120:
121: public StringBuffer format(Date date, StringBuffer toAppendTo,
122: FieldPosition pos) {
123: Calendar calendar = Calendar
124: .getInstance(zone != null ? zone
125: : org.mmbase.util.dateparser.DateParser.defaultTimeZone);
126: calendar.setTime(date);
127: // pos.setBeginIndex(0); pos.setEndIndex(1);
128: toAppendTo.append(calendar.get(Calendar.DAY_OF_WEEK));
129: return toAppendTo;
130: }
131:
132: public void setTimeZone(TimeZone value) {
133: zone = value;
134: }
135:
136: }
137: }
|