Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: gov/lanl/Utility/DateTime.java


1   //DateTime.java
2   
3   
4   /*************************************
5    *Copyright Notice
6    *Copyright (c) 1999, Regents of the University of California. All rights reserved.
7   
8    * DISCLAIMER
9    * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
10   * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
11   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
12   * SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
14   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
15   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
16   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
17   * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
18   * DAMAGE.
19   ***************************************/
20  
21  package gov.lanl.Utility;
22  
23  import java.util.Calendar;
24  import java.util.Locale;
25  import java.util.TimeZone;
26  
27  /**Provides a date/time class to support the data in the TeleMed server
28   *Date and time are saved together as a double, with the integer
29   *part the Julian date, and the fractional part the time in hours
30   *(24 hour standard) divided by 24, down to seconds. ALWAYS GMT!
31   *Thus every time stamp in the TeleMed data base has date and time.
32   *<p>The julian conversions are modelled after "Numerical Recipes.."
33   *by Press, Teukolsky, Vettering, and Flannery; Cambridge University
34   *Press
35   *<p>Modified for PIDS time stamp
36   *@author Jim George
37   *@version 12/21/98
38   */
39  
40  public class DateTime implements Cloneable {
41  
42      /**The month, 1-12*/
43      int month;
44      /**The day of the month, 1-31*/
45      int day;
46      /**The year, all digits*/
47      int year;
48      /**The hour of the day, 0-23*/
49      int hour;
50      /**The minutes of the hour, 0-59*/
51      int min;
52      /**The seconds of the minute, 0-59*/
53      int sec;
54      private static org.apache.log4j.Logger cat = org.apache.log4j.Logger.getLogger(DateTime.class.getName());
55  
56      /**Create a DateTime object with the current date time
57       *return DateTime initialized to the current date and time
58       */
59  
60      public DateTime() {
61  
62          TimeZone tz = TimeZone.getTimeZone("GMT");
63          Calendar rightNow = Calendar.getInstance(tz);
64  
65          year = rightNow.get(Calendar.YEAR);
66          month = rightNow.get(Calendar.MONTH) + 1;
67          day = rightNow.get(Calendar.DATE);
68          hour = rightNow.get(Calendar.HOUR_OF_DAY);
69          min = rightNow.get(Calendar.MINUTE);
70          sec = rightNow.get(Calendar.SECOND);
71  
72  
73          /**replaced for y2k and gmt
74           Date today = new Date();
75           year  = today.getYear() + 1900;
76           month = today.getMonth() + 1;
77           day   = today.getDate();
78           hour  = today.getHours();
79           min   = today.getMinutes();
80           sec   = today.getSeconds();
81           */
82      }
83  
84      /**Create a DateTime object with the requested values
85       *@param yr year (full year, e.g., 1997)
86       *@param mo month ( 1-12)
87       *@param da date (1-31)
88       *@param hr hour of day (0-23)
89       *@param mn minute of hour (0-59)
90       *@param sc second of minute (0-59)
91       *@exception IllegalArgumentException if something wrong in date
92       */
93  
94      public DateTime(int yr, int mo, int da, int hr, int mn, int sc) {
95  
96          year = yr;
97          month = mo;
98          day = da;
99          hour = hr;
100         min = mn;
101         sec = sc;
102 
103         if (!isValid()) throw new IllegalArgumentException();
104     }
105 
106     /**Create a DateTime object with the requested values, 0 for rest
107      *@param yr year (full year, e.g., 1997)
108      *@param mo month ( 1-12)
109      *@param da date (1-31)
110      *@exception IllegalArgumentException if something wrong in date
111      */
112 
113     public DateTime(int yr, int mo, int da) {
114         this(yr, mo, da, 0, 0, 0);
115     }
116 
117     /**Create a DateTime object from a Calendar object
118      *@param inCalendar is the incomming calendar object
119      */
120     public DateTime(Calendar inCalendar) {
121         //initialize to gmt from incomming calendar time
122         year = inCalendar.get(Calendar.YEAR);
123         month = inCalendar.get(Calendar.MONTH) + 1;
124         day = inCalendar.get(Calendar.DATE);
125         hour = inCalendar.get(Calendar.HOUR_OF_DAY);
126         min = inCalendar.get(Calendar.MINUTE);
127         sec = inCalendar.get(Calendar.SECOND);
128         //adjust to gmt
129         TimeZone tz = inCalendar.getTimeZone();
130         int off = tz.getRawOffset();
131         cat.debug("Offset= " + off + " hour= " + hour + " day = " + day);
132         advanceSecs(-off / 1000);
133         cat.debug("  new hour= " + hour + " new day = " + day);
134 
135 
136     }
137 
138     /**Create a DateTime object with the requested julian date
139      * @param julian the date time in our julian expanded format, i.e. date/time
140      * @exception IllegalArgumentException if something wrong
141      */
142 
143     public DateTime(int julian) {
144 
145         fromJulian(julian);
146         if (!isValid()) throw new IllegalArgumentException();
147     }
148 
149     /**
150      * Advances this DateTime by n days
151      * @param n the number of days by which to change this day, + or -
152      * @deprecated Replaced by advanceDays
153      */
154 
155     public void advance(int n) {
156         advanceDays(n);
157     }
158 
159 
160     /**
161      * Advances this DateTime by n days
162      * @param n the number of days by which to change this day, + or -
163      */
164 
165     public void advanceDays(int n) {
166         fromJulian(toJulian() + n);
167     }
168 
169 
170     /**
171      * Advances this DateTime by hr hours
172      * @param hr is the number of hours to add
173      */
174 
175     public void advanceHours(int hr) {
176         double addval = hr;
177         addval = addval / 24;
178         fromJulian(toJulian() + addval);
179     }
180 
181 
182     /**
183      * Advances this DateTime by min minutes
184      * @param min is the number of minutes to add
185      */
186 
187     public void advanceMins(int min) {
188         double addval = min;
189         addval = addval / 60 / 24;
190         fromJulian(toJulian() + addval);
191     }
192 
193 
194     /**
195      * Advances this DateTime by min minutes
196      * @param sec is the number of minutes to add
197      */
198 
199     public void advanceSecs(int sec) {
200         double addval = sec;
201         addval = addval / 60 / 60 / 24;
202         fromJulian(toJulian() + addval);
203     }
204 
205 
206     /**@return day of the month as an int, 1-31*/
207 
208     public int getDay() {
209         return day;
210     }
211 
212 
213     /**@return month as an int, 1-12*/
214 
215     public int getMonth() {
216         return month;
217     }
218 
219 
220     /**@return year as an int, all digits*/
221 
222     public int getYear() {
223         return year;
224     }
225 
226 
227     /**@return hour as an int*/
228 
229     public int getHour() {
230         return hour;
231     }
232 
233 
234     /**@return minutes as an int*/
235 
236     public int getMin() {
237         return min;
238     }
239 
240 
241     /**@return seconds as an int*/
242 
243     public int getSecs() {
244         return sec;
245     }
246 
247 
248     /**@return the weekday (0-6, Sunday-Saturday)*/
249 
250     public int weekday() {
251         //double dj = toJulian();
252         //int ij = (int) dj;
253         //int rj = (ij - 2440000) % 7;
254         //System.out.println(dj + " " + ij + " " + rj);
255 
256 
257         return (((int) toJulian() + 1) % 7);
258     }
259 
260 
261     /**
262      * positive if this is later than the parameter
263      * @param other any date
264      * @return the number of days between this and the parameter
265      */
266 
267     public int daysBetween(DateTime other) {
268         int ithis, iother;
269 
270         ithis = (int) toJulian();
271         iother = (int) other.toJulian();
272         return (ithis - iother);
273     }
274 
275     /**@return a copy of this object*/
276 
277     public Object clone() {
278 
279         try {
280             return super.clone();
281         } catch (CloneNotSupportedException e) {
282             return null;
283         }
284     }
285 
286 
287     /**@return true if this is a valid DateTime*/
288 
289     public boolean isValid() {
290 
291         DateTime dt = new DateTime();
292 
293         dt.fromJulian(this.toJulian());
294 
295         return dt.day == day && dt.month == month && dt.year == year
296                 && dt.hour == hour && dt.min == min && dt.sec == sec;
297     }
298 
299 
300     /**
301      *@return the julian day as the integer part and the 24hr time as fractional
302      *<p><i>2440000 was 5/23/68 a Thursday</i>
303      */
304 
305     public double toJulian() {
306 
307         int y = year;
308         int m = month;
309         double timeofday;
310         int ijulian;
311         int IGREG = 15 + 31 * (10 + 12 * 1582); // Greg.Calendar 10/15/1582
312         int adj;
313 
314         if (y < 0) y = y + 1;
315         if (m > 2)
316             m = m + 1;
317         else {
318             y = y - 1;
319             m = m + 13;
320         }
321 
322         ijulian = (int) (365.25 * y) + (int) (30.6001 * m) + day + 1720995;
323 
324         if (day + 31 * (m + 12 * y) >= IGREG) { // change for Gregorian calendar
325             adj = y / 100;
326             ijulian = ijulian + 2 - adj + adj / 4;
327         }
328 
329         timeofday = hour / 24.0 + min / 1440.0 + sec / 86400.0;
330         return ijulian + timeofday;
331     }
332 
333 
334     /**Converts a TeleMed julian (both date & time) to a DateTime
335      *The DataTime object exists and its values are changed
336      *@param injulian the TeleMed julian daytime number
337      */
338 
339     public void fromJulian(double injulian) {
340 
341         int ja,jalpha,jb,jc,jd,je;
342         int JGREG = 2299161; //  julian date of Geg calendar
343         double halfsecond = 0.5;
344 
345         double julian = injulian + halfsecond / 86400.0;
346 
347         ja = (int) julian;
348 
349         if (ja >= JGREG) {    //adjust
350             jalpha = (int) (((ja - 1867216) - 0.25) / 36524.25);
351             ja = ja + 1 + jalpha - jalpha / 4;
352         }
353 
354         jb = ja + 1524;
355         jc = (int) (6680.0 + ((jb - 2439870) - 122.1) / 365.25);
356         jd = 365 * jc + jc / 4;
357         je = (int) ((jb - jd) / 30.6001);
358         day = jb - jd - (int) (30.6001 * je);
359         month = je - 1;
360         if (month > 12) month = month - 12;
361         year = jc - 4715;
362         if (month > 2) year = year - 1;
363         if (year <= 0) year = year - 1;
364 
365         // now for the time
366 
367         double thetime = julian - (int) julian;
368         thetime = thetime * 24;
369         hour = (int) thetime;
370         thetime = (thetime - hour) * 60;
371         min = (int) thetime;
372         thetime = (thetime - min) * 60;
373         sec = (int) thetime;
374 
375     }
376 
377     /**Converts from PID time to the DateTime class
378      *@param thepidtime the pid time as a long, has both date and time
379      */
380 
381     public void fromPIDTime(long thepidtime) {
382         double jul;
383         jul = (double) thepidtime;
384         jul = jul / 10000000 / 60 / 60 / 24;
385         fromJulian(jul);
386     }
387 
388 
389     /**Converts the DateTime to PID time
390      *@return the PID time as a long, both date and time
391      */
392 
393     public long toPIDTime() {
394         double jul = toJulian();
395         long pidtime = (long) (jul * 24 * 60 * 60 * 10000000);
396         return pidtime;
397     }
398 
399 
400     /**Creates a calendar object using GMT timezone
401      *@return a Calendar with GMT time zone set properly
402      */
403     public Calendar makeGMTCalendar() {
404         Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
405         gmtCal.set(year, month - 1, day, hour, min, sec);
406         return gmtCal;
407     }
408 
409 
410     /**Creates a calendar object using Local timezone
411      *@return a Calendar with Local time zone set properly
412      */
413     public Calendar makeLocalCalendar() {
414         Calendar locCal = Calendar.getInstance();
415         TimeZone tz = locCal.getTimeZone();
416         int off = tz.getRawOffset() / 1000;
417         advanceSecs(off);
418         locCal.set(year, month - 1, day, hour, min, sec);
419         advanceSecs(-off);
420         return locCal;
421     }
422 
423 
424 }