KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > datetime > TimeUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.datetime;
4
5 /**
6  * Date time calculations and utilities. <code>TimeUtil</code> is used by
7  * {@link JDateTime} and it contains few utilities that may be used
8  * elswhere, although {@link JDateTime} is recomended for all time
9  * manipulation.
10  */

11 public class TimeUtil {
12
13     public static final long MILLIS_IN_DAY = 1000L * 60 * 60 * 24;
14
15     // ---------------------------------------------------------------- misc calc
16

17     /**
18      * Calculates day of year from given time stamp.
19      * It may not work for some dates in 1582.
20      *
21      * @return day of year in range: [1-366]
22      */

23     public static int dayOfYear(int year, int month, int day) {
24         int day_of_year;
25         if (isLeapYear(year)) {
26             day_of_year = ((275 * month) / 9) - ((month + 9) / 12) + day - 30;
27         } else {
28             day_of_year = ((275 * month) / 9) - (((month + 9) / 12) << 1) + day - 30;
29         }
30         return day_of_year;
31     }
32
33     
34     /**
35      * Check if the given year is leap year.
36      *
37      * @return <code>true</code> if the year is a leap year
38      */

39     public static boolean isLeapYear(int y) {
40         boolean result = false;
41     
42         if (((y % 4) == 0) && // must be divisible by 4...
43
((y < 1582) || // and either before reform year...
44
((y % 100) != 0) || // or not a century...
45
((y % 400) == 0))) { // or a multiple of 400...
46
result = true; // for leap year.
47
}
48         return result;
49     }
50
51     private static final int[] MONTH_LENGTH = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
52     private static final int[] LEAP_MONTH_LENGTH = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
53
54     /**
55      * Returns the length of the specified month in days. Month is 1 for January
56      * and 12 for December.
57      *
58      * @return length of the specified month in days
59      */

60     public static int getMonthLength(int year, int m) {
61         if ((m < 1) || (m > 12)) {
62             return -1;
63         }
64         if (isLeapYear(year)) {
65             return LEAP_MONTH_LENGTH[m];
66         }
67         return MONTH_LENGTH[m];
68     }
69
70
71     // ---------------------------------------------------------------- valid
72

73     /**
74      * Checks if date is valid.
75      *
76      * @return <code>true</code> if date is valid, otherwise <code>false</code>
77      */

78     public static boolean isValidDate(int year, int month, int day) {
79         if ((month < 1) || (month > 12)) {
80             return false;
81         }
82         int ml = getMonthLength(year, month);
83         //noinspection RedundantIfStatement
84
if ((day < 1) || (day > ml)) {
85             return false;
86         }
87         return true;
88     }
89
90     /**
91      * Checks if time is valid.
92      *
93      * @param hour hour to check
94      * @param minute minute to check
95      * @param second second to check
96      *
97      * @return <code>true</code> if time is valid, otherwise <code>false</code>
98      */

99     public static boolean isValidTime(int hour, int minute, double second) {
100         if ((hour < 0) || (hour >= 24)) {
101             return false;
102         }
103         if ((minute < 0) || (minute >= 60)) {
104             return false;
105         }
106         //noinspection RedundantIfStatement
107
if ((second < 0) || (second >= 60)) {
108             return false;
109         }
110         return true;
111     }
112
113     /**
114      * Checks if date and time are valid.
115      *
116      * @param year year to check
117      * @param month month to check
118      * @param day day to check
119      * @param hour hour to check
120      * @param minute minute to check
121      * @param second second to check
122      *
123      * @return <code>true</code> if date and time are valid, otherwise <code>false</code>
124      */

125     public static boolean isValidDateTime(int year, int month, int day, int hour, int minute, double second) {
126         return (isValidDate(year, month, day) && isValidTime(hour, minute, second));
127     }
128
129     /**
130      * Checks if date and time are valid.
131      *
132      * @param dts date/time stamp
133      *
134      * @return <code>true</code> if date and time are valid, otherwise <code>false</code>
135      */

136     public static boolean isValidDateTime(DateTimeStamp dts) {
137         return (isValidDate(dts.year, dts.month, dts.day) && isValidTime(dts.hour, dts.minute, dts.second));
138     }
139
140
141     // ---------------------------------------------------------------- julian date
142

143     /**
144      * Calculates Astronomical Julian Date from given time stamp.
145      *
146      * @return Julian Date stamp
147      */

148     public static JulianDateStamp toJulianDate(DateTimeStamp time) {
149         return toJulianDate(time.year, time.month, time.day, time.hour, time.minute, time.second);
150     }
151
152     /**
153      * Calculates Astronomical Julian Date from given time.<p>
154      *
155      * Astronomical Julian Dates are counting from noon of the January 1st, -4712
156      * (julian date 0 is -4712/01/01 12:00:00). Zero year exist. Julian Date
157      * is always GMT, there are no timezones.
158      * <p>
159      *
160      * Algorithm based on Numerical Recipesin C, 2nd ed., Cambridge University
161      * Press 1992, modified and enhanced by Igor Spasic.
162      *
163      * @param year year
164      * @param month month
165      * @param day day
166      * @param hour hour
167      * @param minute minute
168      * @param second second
169      *
170      * @return julian time stamp
171      */

172     public static JulianDateStamp toJulianDate(int year, int month, int day, int hour, int minute, double second) {
173         //double JD;
174
JulianDateStamp jds = new JulianDateStamp();
175
176         // month range fix
177
if ((month > 12) || (month < -12)) {
178             month--;
179             int delta = month / 12;
180             year += delta;
181             month -= delta * 12;
182             month++;
183         }
184         if (month < 0) {
185             year--;
186             month += 12;
187         }
188
189         // decimal day fraction
190
double frac = (hour / 24.0) + (minute / 1440.0) + (second / 86400.0);
191         if (frac < 0) { // negative time fix
192
int delta = ((int)(-frac)) + 1;
193             frac += delta;
194             day -= delta;
195         }
196         double gyr = year + (0.01 * month) + (0.0001 * day) + (0.0001 * frac) + 1.0e-9;
197
198         // conversion factors
199
int iy0;
200         int im0;
201         if (month <= 2) {
202             iy0 = year - 1;
203             im0 = month + 12;
204         } else {
205             iy0 = year;
206             im0 = month;
207         }
208         int ia = iy0 / 100;
209         int ib = 2 - ia + (ia >> 2);
210     
211         // calculate julian date
212
int jd;
213         if (year <= 0) {
214             jd = (int)((365.25 * iy0) - 0.75) + (int)(30.6001 * (im0 + 1)) + day + 1720994;
215         } else {
216             jd = (int)(365.25 * iy0) + (int)(30.6001 * (im0 + 1)) + day + 1720994;
217         }
218         if (gyr >= 1582.1015) { // on or after 15 October 1582
219
jd += ib;
220         }
221         //JD = jd + frac + 0.5;
222
// return JD
223

224         jds.integer = jd;
225         jds.fraction = frac + 0.5;
226         return jds;
227     }
228
229
230     /**
231      * Calculates time stamp from Astronomical Julian Date.
232      *
233      * @param JD julian date
234      *
235      * @return time stamp
236      */

237     public static DateTimeStamp fromJulianDate(double JD) {
238         return fromJulianDate(new JulianDateStamp(JD));
239     }
240
241
242     /**
243      * Calculates time stamp from Astronomical Julian Date.
244      * Algorithm based on Numerical Recipesin C, 2nd ed., Cambridge University
245      * Press 1992.
246      *
247      * @param jds julian date stamp
248      *
249      * @return time stamp
250      */

251     public static DateTimeStamp fromJulianDate(JulianDateStamp jds) {
252         DateTimeStamp time = new DateTimeStamp();
253         int year, month, day;
254         double frac;
255         int jd, ka, kb, kc, kd, ke, ialp;
256         double d_hour, d_minute;
257         
258         //double JD = jds.doubleValue();//jdate;
259
//jd = (int)(JD + 0.5); // integer julian date
260
//frac = JD + 0.5 - (double)jd + 1.0e-10; // day fraction
261

262         ka = (int)(jds.fraction + 0.5);
263         jd = jds.integer + ka;
264         frac = jds.fraction + 0.5 - ka + 1e-10;
265
266         ka = jd;
267         if (jd >= 2299161) {
268             ialp = (int)(((double)jd - 1867216.25) / (36524.25));
269             ka = jd + 1 + ialp - (ialp >> 2);
270         }
271         kb = ka + 1524;
272         kc = (int)(((double)kb - 122.1) / 365.25);
273         kd = (int)((double)kc * 365.25);
274         ke = (int)((double)(kb - kd) / 30.6001);
275         day = kb - kd - ((int)((double)ke * 30.6001));
276         if (ke > 13) {
277             month = ke - 13;
278         } else {
279             month = ke - 1;
280         }
281         if ((month == 2) && (day > 28)){
282             day = 29;
283         }
284         if ((month == 2) && (day == 29) && (ke == 3)) {
285             year = kc - 4716;
286         } else if (month > 2) {
287             year = kc - 4716;
288         } else {
289             year = kc - 4715;
290         }
291         time.year = year;
292         time.month = month;
293         time.day = day;
294
295         // hour with minute and second included as fraction
296
d_hour = frac * 24.0;
297         time.hour = (int)d_hour; // integer hour
298

299         // minute with second included as a fraction
300
d_minute = (d_hour - (double)time.hour) * 60.0;
301         time.minute = (int)d_minute; // integer minute
302

303         time.second = (d_minute - (double)time.minute) * 60.0;
304
305         // fix calculation errors
306
time.second = ((int) (time.second * 10000) + 0.5) / 10000.0;
307         return time;
308     }
309
310
311 }
312
Popular Tags