001: /*
002: * Copyright (C) 2007 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.sourceforge.squirrel_sql.fw.util;
020:
021: import java.text.DateFormat;
022: import java.text.FieldPosition;
023: import java.text.ParseException;
024: import java.text.ParsePosition;
025: import java.util.Date;
026:
027: /**
028: * Maintains a single reference to a DateFormat and synchronizes access to
029: * methods that delegate to it's methods of the same name.
030: */
031: public class ThreadSafeDateFormat {
032:
033: /** internal protected instance of DateFormat */
034: private DateFormat dateFormat;
035:
036: /**
037: * Constructor
038: * @param dateFormat the DateFormat instance to protect.
039: */
040: public ThreadSafeDateFormat(DateFormat dateFormat) {
041: this .dateFormat = dateFormat;
042: }
043:
044: /**
045: * Constructor
046: * @param style the given formatting style. For example,
047: * SHORT for "M/d/yy" in the US locale.
048: */
049: public ThreadSafeDateFormat(int style) {
050: this (style, false);
051: }
052:
053: /**
054: * Constructor
055: * @param style the given formatting style. For example,
056: * SHORT for "M/d/yy" in the US locale if isTime is false,
057: * and SHORT for "h:mm a" in the US locale if isTime is true
058: */
059: public ThreadSafeDateFormat(int style, boolean isTime) {
060: if (isTime) {
061: this .dateFormat = DateFormat.getTimeInstance(style);
062: } else {
063: this .dateFormat = DateFormat.getDateInstance(style);
064: }
065: }
066:
067: /**
068: * Constructor
069: * @param dateStyle the given date formatting style. For example,
070: * SHORT for "M/d/yy" in the US locale.
071: * @param timeStyle the given time formatting style. For example,
072: * SHORT for "h:mm a" in the US locale.
073: */
074: public ThreadSafeDateFormat(int dateSytle, int timeStyle) {
075: this .dateFormat = DateFormat.getDateTimeInstance(dateSytle,
076: timeStyle);
077: }
078:
079: /**
080: * Formats an object to produce a string. This is equivalent to
081: * <blockquote>
082: * {@link #format(Object, StringBuffer, FieldPosition) format}<code>(obj,
083: * new StringBuffer(), new FieldPosition(0)).toString();</code>
084: * </blockquote>
085: *
086: * @param obj The object to format
087: * @return Formatted string.
088: * @exception IllegalArgumentException if the Format cannot format the given
089: * object
090: */
091: public synchronized String format(Object obj) {
092: return dateFormat.format(obj);
093: }
094:
095: /**
096: * Parses text from the beginning of the given string to produce a date.
097: * The method may not use the entire text of the given string.
098: * <p>
099: * See the {@link #parse(String, ParsePosition)} method for more information
100: * on date parsing.
101: *
102: * @param source A <code>String</code> whose beginning should be parsed.
103: * @return A <code>Date</code> parsed from the string.
104: * @exception ParseException if the beginning of the specified string
105: * cannot be parsed.
106: */
107: public synchronized Date parse(String str) throws ParseException {
108: return dateFormat.parse(str);
109: }
110:
111: /**
112: * Specify whether or not date/time parsing is to be lenient. With
113: * lenient parsing, the parser may use heuristics to interpret inputs that
114: * do not precisely match this object's format. With strict parsing,
115: * inputs must match this object's format.
116: * @param lenient when true, parsing is lenient
117: * @see java.util.Calendar#setLenient
118: */
119: public synchronized void setLenient(boolean lenient) {
120: dateFormat.setLenient(lenient);
121: }
122: }
|