001: /*
002: * Created on Jan 20, 2006
003: */
004: package uk.org.ponder.dateutil;
005:
006: import java.text.SimpleDateFormat;
007: import java.util.Calendar;
008: import java.util.Date;
009: import java.util.GregorianCalendar;
010:
011: import uk.org.ponder.errorutil.PropertyException;
012: import uk.org.ponder.util.UniversalRuntimeException;
013:
014: /**
015: * A "transit bean" which will accept and collect separate String values for
016: * broken up date field values (day, month, year, etc.)
017: * into a java.util.Date format.
018: *
019: * @author Antranig Basman (antranig@caret.cam.ac.uk)
020: *
021: */
022:
023: public class BrokenDateTransit {
024: public static final String DMY_MODE = "DMY";
025: public static final String FULL_MODE = "Full";
026: public static final String HMS_MODE = "HMS";
027:
028: public Calendar calendar = new GregorianCalendar();
029: // UI-exposed fields of transit beans need to be read/write since they may
030: // be re-fetched by fixup before rendering.
031: public String month = "01";
032: public String year;
033: public String day = "01";
034:
035: public String hour;
036: public String minute = "0";
037: public String second = "0";
038: public String ampm; // "0" for am, "1" for pm
039:
040: private String mode = "DMY";
041:
042: public void setMode(String mode) {
043: this .mode = mode;
044: }
045:
046: public void setCalendar(Calendar calendar) {
047: this .calendar = calendar;
048: }
049:
050: private Date date = new Date();
051:
052: public BrokenDateTransit() {
053: render();
054: }
055:
056: public void setDate(Date date) {
057: this .date = date;
058: render();
059: }
060:
061: public static int parseToZero(String value) {
062: int togo = 0;
063: try {
064: togo = Integer.parseInt(value);
065: } catch (Exception e) {
066: }
067: return togo;
068: }
069:
070: public void parse() {
071: try {
072: if (mode.equals(DMY_MODE)) {
073: int yearint = Integer.parseInt(year);
074: int monthint = Integer.parseInt(month);
075: int dayint = Integer.parseInt(day);
076: calendar.set(yearint, monthint - 1, dayint);
077: } else if (mode.equals(FULL_MODE)) {
078: int yearint = Integer.parseInt(year);
079: int monthint = Integer.parseInt(month);
080: int dayint = Integer.parseInt(day);
081: int hourint = Integer.parseInt(hour);
082: int minuteint = parseToZero(minute);
083: int secondint = parseToZero(second);
084: if (ampm != null) {
085: hourint += Integer.parseInt(ampm) == 1 ? 12 : 0;
086: }
087: calendar.set(yearint, monthint - 1, dayint, hourint,
088: minuteint, secondint);
089: }
090: } catch (Exception e) {
091: throw UniversalRuntimeException.accumulate(e,
092: PropertyException.class, "Invalid date format");
093: }
094: date = calendar.getTime();
095: }
096:
097: public void render() {
098: if (mode.equals(DMY_MODE)) {
099: SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
100: String formatted = sdf.format(date);
101: day = formatted.substring(0, 2);
102: month = formatted.substring(2, 4);
103: year = formatted.substring(4);
104: } else if (mode.equals(FULL_MODE)) {
105: SimpleDateFormat sdf = LocalSDF.breakformat.get();
106: String formatted = sdf.format(date);
107: day = formatted.substring(0, 2);
108: month = formatted.substring(2, 4);
109: year = formatted.substring(4, 8);
110: hour = formatted.substring(8, 10);
111: minute = formatted.substring(10, 12);
112: second = formatted.substring(12, 14);
113: }
114: }
115:
116: public Date getDate() {
117: parse();
118: return date;
119: }
120:
121: }
|