001: package org.swingml.util;
002:
003: import java.text.*;
004: import java.util.*;
005:
006: /**
007: * @author CrossLogic
008: */
009: public class DateConverter {
010:
011: private static Map timeZoneCache = new HashMap();
012: private String displayPattern;
013: private String pattern = "mm/dd/yyyy";
014: private SimpleDateFormat sdf = new SimpleDateFormat();
015:
016: public DateConverter(String ptrn) {
017: // see if pattern has a display format value
018: int i = ptrn.indexOf('@');
019: if (i < 0) {
020: setPattern(ptrn);
021: } else {
022: // strip format pattern
023: setDisplayPattern(ptrn.substring(i + 1));
024: setPattern(ptrn.substring(0, i));
025: }
026: }
027:
028: public int compare(String value1, String value2)
029: throws java.text.ParseException {
030: if (value1.trim().length() == 0 && value2.trim().length() > 0) {
031: return -1;
032: }
033: if (value1.trim().length() == 0 && value2.trim().length() == 0) {
034: return 0;
035: }
036: if (value1.trim().length() > 0 && value2.trim().length() == 0) {
037: return 1;
038: }
039: Date d1 = convert(value1);
040: Date d2 = convert(value2);
041: return d1.compareTo(d2);
042: }
043:
044: /**
045: * Parse the passed in value for value and time zone. Use those to create an
046: * appropriate date object.
047: *
048: * @param value
049: * @return
050: * @throws java.text.ParseException
051: */
052: public Date convert(String value) throws java.text.ParseException {
053: String parsedValue = getValue(value);
054: TimeZone zone = getTimeZone(value);
055: sdf.setTimeZone(zone);
056: sdf.applyPattern(getPattern());
057: return sdf.parse(parsedValue);
058: }
059:
060: public String displayValueString(String value)
061: throws ParseException {
062: String result = value;
063: String dispPattern = getDisplayPattern();
064: if (dispPattern != null && dispPattern.length() > 0) {
065: // convert to the dispaly pattern
066: Date date = convert(value);
067: sdf.setTimeZone(getTimeZone(value));
068: sdf.applyPattern(dispPattern);
069: result = sdf.format(date);
070: }
071: return result;
072: }
073:
074: public String getDisplayPattern() {
075: return displayPattern;
076: }
077:
078: public String getPattern() {
079: return pattern;
080: }
081:
082: /**
083: * Looks for a timezone code in the value as the data after a ; I.E.
084: * 10/31/2005 12:00:00.0000;EST would result in EST
085: *
086: * @param value
087: * @return
088: */
089: private TimeZone getTimeZone(String value) {
090: TimeZone result = TimeZone.getDefault();
091: if (value.indexOf(";") != -1) {
092: // Get TZ code
093: String theCode = value.substring(value.indexOf(";") + 1)
094: .trim();
095: result = (TimeZone) timeZoneCache.get(theCode);
096: if (result == null) {
097: // Look for raw offset amount.
098: if (theCode.startsWith("GMT")) {
099: String hours = theCode.substring(3).trim();
100: int minutes = 0;
101: try {
102: if (theCode.indexOf(":") >= 0) {
103: // split to hours/minutes
104: minutes = Integer.parseInt(hours
105: .substring(hours.indexOf(":") + 1));
106: hours = hours.substring(0, hours
107: .indexOf(":"));
108: }
109: int offset = Integer.parseInt(hours) * 1000 * 60 * 60;
110: offset += minutes * 60 * 1000;
111: String ids[] = TimeZone.getAvailableIDs(offset);
112: result = TimeZone.getTimeZone(ids[0]);
113: } catch (NumberFormatException nfe) {
114: }
115: } else {
116: result = TimeZone.getTimeZone(theCode);
117: }
118: timeZoneCache.put(theCode, result);
119: }
120: }
121: return result;
122: }
123:
124: /**
125: * Strip off the TimeZone code, if it is there.
126: *
127: * @param value
128: * @return
129: */
130: private String getValue(String value) {
131: String result = value;
132: if (result.indexOf(";") != -1) {
133: result = result.substring(0, result.indexOf(";"));
134: }
135: return result;
136: }
137:
138: public void setDisplayPattern(String s) {
139: displayPattern = s;
140: }
141:
142: public void setPattern(String pattern) {
143: this .pattern = pattern;
144: }
145:
146: public String toString() {
147: return getPattern();
148: }
149: }
|