1 /*
2 * $Id: TimeScheme.java 1419 2010-11-01 14:12:17Z hoheisel $
3 *
4 * Copyright (c) 2007
5 * Fraunhofer Institute for Computer Architecture and Software Technology
6 * See http://www.first.fraunhofer.de for more details.
7 */
8
9 package net.kwfgrid.gwes.jaxen;
10
11 import java.util.Date;
12 import java.util.Calendar;
13 import java.util.GregorianCalendar;
14 import java.util.TimeZone;
15 import java.text.SimpleDateFormat;
16 import java.text.ParseException;
17
18 /**
19 * TimeScheme is a class for storing the time in different formats.
20 * @author Andreas Hoheisel
21 * (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
22 * @version $Id: TimeScheme.java 1419 2010-11-01 14:12:17Z hoheisel $
23 */
24 public class TimeScheme implements Comparable, Cloneable {
25
26 /** time format: Gregorian Calendar */
27 public Calendar cal;
28 /** time format: Date */
29 public Date dat;
30 /** time format: number of milliseconds since January 1, 1970, 00:00:00 GMT */
31 public long ms;
32 /** xsd.dateTime */
33 static final SimpleDateFormat dateAndTime=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
34 /** time zone */
35 static final TimeZone timeZone = TimeZone.getTimeZone("GMT");
36
37 /**
38 * Constructs a default TimeScheme using the current time in the default
39 * time zone with the default locale.
40 */
41 public TimeScheme() {
42 dateAndTime.setTimeZone(timeZone);
43 this.cal = new GregorianCalendar();
44 this.ms = this.cal.getTime().getTime();
45 this.dat = new Date(this.ms);
46 }
47
48 /**
49 * Constructs a TimeScheme with the given date set in the default time zone
50 * with the default locale.
51 * @param msSince1970 the value in ms since 1970 used to set the date
52 */
53 public TimeScheme(long msSince1970) {
54 dateAndTime.setTimeZone(timeZone);
55 this.ms = msSince1970;
56 this.dat = new Date(msSince1970);
57 this.cal = new GregorianCalendar();
58 this.cal.setTime(this.dat);
59 }
60
61 public TimeScheme(String dateTime) throws ParseException {
62 dateAndTime.setLenient(false);
63 dateAndTime.setTimeZone(timeZone);
64 this.dat = dateAndTime.parse(dateTime);
65 this.ms = dat.getTime();
66 this.cal = new GregorianCalendar();
67 this.cal.setTime(this.dat);
68 }
69
70 /**
71 * Constructs a TimeScheme with the given date set in the default time zone
72 * with the default locale.
73 * @param year the value used to set the YEAR time field in the calendar.
74 * @param month the value used to set the MONTH time field in the calendar.
75 * Month value is 0-based. e.g., 0 for January.
76 * @param date the value used to set the DATE time field in the calendar.
77 */
78 public TimeScheme(int year, int month, int date) {
79 dateAndTime.setTimeZone(timeZone);
80 this.cal = new GregorianCalendar(year, month, date);
81 this.ms = this.cal.getTime().getTime();
82 this.dat = new Date(this.ms);
83 }
84
85 /**
86 * Constructs a TimeScheme with the given date and time set in the
87 * default time zone with the default locale.
88 * @param year the value used to set the YEAR time field in the
89 * calendar.
90 * @param month the value used to set the MONTH time field in the
91 calendar. Month value is 0-based. e.g., 0 for January.
92 * @param date the value used to set the DATE time field in the
93 * calendar.
94 * @param hour the value used to set the HOUR_OF_DAY time field in
95 * the calendar.
96 * @param minute the value used to set the MINUTE time field in the
97 * calendar.
98 */
99 public TimeScheme(int year, int month, int date, int hour, int minute) {
100 dateAndTime.setTimeZone(timeZone);
101 this.cal = new GregorianCalendar(year, month, date, hour, minute);
102 this.ms = this.cal.getTime().getTime();
103 this.dat = new Date(this.ms);
104 }
105
106 /**
107 * Constructs a TimeScheme with the given date and time set in the
108 * default time zone with the default locale.
109 * @param year the value used to set the YEAR time field in the
110 * calendar.
111 * @param month the value used to set the MONTH time field in the
112 calendar. Month value is 0-based. e.g., 0 for January.
113 * @param date the value used to set the DATE time field in the
114 * calendar.
115 * @param hour the value used to set the HOUR_OF_DAY time field in
116 * the calendar.
117 * @param minute the value used to set the MINUTE time field in the
118 * calendar.
119 * @param second the value used to set the SECOND time field in the
120 * calendar.
121 */
122 public TimeScheme(int year, int month, int date, int hour, int minute, int second) {
123 dateAndTime.setTimeZone(timeZone);
124 this.cal = new GregorianCalendar(year, month, date, hour, minute, second);
125 this.ms = this.cal.getTime().getTime();
126 this.dat = new Date(this.ms);
127 }
128
129 /**
130 * update cal and dat when ms is given.
131 */
132 public void msToCal() {
133 dat.setTime(ms);
134 cal.setTime(dat);
135 }
136
137 /**
138 * update ms and dat when cal is given.
139 */
140 public void calToMs() {
141 dat = cal.getTime();
142 ms = dat.getTime();
143 }
144
145 public String getDateTime() {
146 return dateAndTime.format(dat);
147 }
148
149 public Calendar getCal() {
150 return cal;
151 }
152
153 public void setCal(Calendar cal) {
154 this.cal = cal;
155 calToMs();
156 }
157
158 public Date getDat() {
159 return dat;
160 }
161
162 public void setDat(Date dat) {
163 this.dat = dat;
164 ms = dat.getTime();
165 cal.setTime(dat);
166 }
167
168 public long getMs() {
169 return ms;
170 }
171
172 public void setMs(long ms) {
173 this.ms = ms;
174 msToCal();
175 }
176
177 /**
178 * Compares the TimeScheme to another object. Throws
179 * ClassCastException if the specified object's type prevents
180 * it from being compared to this TimeScheme.
181 */
182 public int compareTo(Object o) {
183 int ret = 0;
184 TimeScheme t = (TimeScheme) o;
185 if (this.ms > t.ms) {
186 ret = 1;
187 } else if (this.ms < t.ms) {
188 ret = -1;
189 }
190 return ret;
191 }
192
193 /**
194 * Creates a deep copy of the TimeScheme
195 * @return the copy
196 */
197 protected Object clone() {
198 TimeScheme t = new TimeScheme();
199 t.ms = this.ms;
200 t.msToCal();
201 return t;
202 }
203
204 public String toString() {
205 return getDateTime();
206 }
207
208 }