001: /*
002: * Created on 7 Nov 2006
003: */
004: package uk.org.ponder.dateutil;
005:
006: import java.text.DateFormat;
007: import java.text.ParseException;
008: import java.text.SimpleDateFormat;
009: import java.util.Date;
010: import java.util.Locale;
011: import java.util.TimeZone;
012:
013: import uk.org.ponder.localeutil.LocaleReceiver;
014:
015: /**
016: * A transit bean parsing Date objects into their Locale-specific forms.
017: *
018: * @author Antranig Basman (antranig@caret.cam.ac.uk)
019: */
020:
021: public class StandardFieldDateTransit extends LocaleReceiver implements
022: FieldDateTransit {
023: private Date date;
024:
025: private SimpleDateFormat shortformat;
026: private DateFormat medformat;
027: private DateFormat longformat;
028: private SimpleDateFormat timeformat;
029: private DateFormat longtimeformat;
030: private DateFormat iso8601tz;
031: private DateFormat iso8601notz;
032: //private DateFormat breakformat;
033: private TimeZone timezone = TimeZone.getDefault();
034:
035: public void setTimeZone(TimeZone timezone) {
036: this .timezone = timezone;
037: }
038:
039: public int getTZOffset() {
040: return timezone.getOffset(date.getTime());
041: }
042:
043: public void init() {
044:
045: Locale locale = getLocale();
046: // TODO: Think about sharing these, see LocalSDF for construction costs
047: shortformat = (SimpleDateFormat) DateFormat.getDateInstance(
048: DateFormat.SHORT, locale);
049: shortformat.setLenient(false);
050: medformat = DateFormat.getDateInstance(DateFormat.MEDIUM,
051: locale);
052: longformat = DateFormat
053: .getDateInstance(DateFormat.LONG, locale);
054: timeformat = (SimpleDateFormat) DateFormat.getTimeInstance(
055: DateFormat.SHORT, locale);
056: timeformat.setTimeZone(timezone);
057: longtimeformat = DateFormat.getTimeInstance(DateFormat.LONG,
058: locale);
059: longtimeformat.setTimeZone(timezone);
060: iso8601tz = new SimpleDateFormat(LocalSDF.W3C_DATE_TZ);
061: iso8601notz = new SimpleDateFormat(LocalSDF.W3C_DATE_NOTZ);
062: iso8601notz.setTimeZone(timezone);
063: //breakformat = new SimpleDateFormat(LocalSDF.BREAKER_DATE, locale);
064: // do not use new Date(0) because of TZ insanity!!!
065: date = LocalSDF.breakformat.parse("01012000000000");
066: }
067:
068: public String getShort() {
069: return shortformat.format(date);
070: }
071:
072: public String getMedium() {
073: return medformat.format(date);
074: }
075:
076: public String getLong() {
077: return longformat.format(date);
078: }
079:
080: public String getTime() {
081: return timeformat.format(date);
082: }
083:
084: public String getLongTime() {
085: return longtimeformat.format(date);
086: }
087:
088: public void setShort(String shortform) throws ParseException {
089: Date ydate = shortformat.parse(shortform);
090: DateUtil.applyFields(date, ydate, DateUtil.DATE_FIELDS);
091: }
092:
093: public void setMedium(String medform) throws ParseException {
094: Date ydate = medformat.parse(medform);
095: DateUtil.applyFields(date, ydate, DateUtil.DATE_FIELDS);
096: }
097:
098: public void setLong(String longform) throws ParseException {
099: Date ydate = longformat.parse(longform);
100: DateUtil.applyFields(date, ydate, DateUtil.DATE_FIELDS);
101: }
102:
103: public void setTime(String time) throws ParseException {
104: Date mdate = timeformat.parse(time);
105: DateUtil.applyFields(date, mdate, DateUtil.TIME_FIELDS);
106: }
107:
108: public Date getDate() {
109: return date;
110: }
111:
112: public void setDate(Date date) {
113: this .date = date;
114: }
115:
116: /** Render an ISO8601-formatted value, including timezone information **/
117: public String getISO8601TZ() {
118: return iso8601tz.format(date);
119: }
120:
121: /** Set an ISO 8601-formatted value for which the timezone is to be firmly
122: * IGNORED.
123: */
124: public void setISO8601TZ(String isoform) throws ParseException {
125: date = iso8601notz.parse(isoform);
126: }
127:
128: public String getShortFormat() {
129: return shortformat.toLocalizedPattern();
130: }
131:
132: public String getTimeFormat() {
133: return timeformat.toLocalizedPattern();
134: }
135:
136: }
|