KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > datetime > JDateTimeTest


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

3 package jodd.datetime;
4
5 import java.util.Calendar JavaDoc;
6 import java.util.Date JavaDoc;
7 import java.util.GregorianCalendar JavaDoc;
8 import java.util.TimeZone JavaDoc;
9
10 import junit.framework.TestCase;
11
12 public class JDateTimeTest extends TestCase {
13
14     public void testSetGetMillis() {
15         JDateTime jdt = new JDateTime(2003, 2, 28, 23, 59, 59.000);
16         
17         for (int i = 0; i < 1000; i++) {
18             jdt.setMillisecond(i);
19             assertEquals(i, jdt.getMillisecond());
20         }
21     }
22
23
24     public void testSet999Millis() {
25
26         JDateTime jdt = new JDateTime();
27
28         jdt.set(2003, 2, 28, 23, 59, 59.999);
29         assertEquals("2003-02-28 23:59:59.999", jdt.toString());
30
31         jdt.set(2003, 2, 28, 23, 59, 60.0);
32         assertEquals("2003-03-01 00:00:00.000", jdt.toString());
33
34         // this used to be a problem
35
jdt.set(2003, 2, 28, 23, 59, 59.999999); // 6 fraction digits
36
assertEquals("2003-02-28 23:59:59.999", jdt.toString());
37
38         // this used to be a problem
39
jdt.set(2003, 2, 28, 23, 59, 59.999999999999); // 12 fraction digits - last working
40
assertEquals("2003-02-28 23:59:59.999", jdt.toString());
41
42         jdt.set(2003, 2, 28, 23, 59, 59.9999999999999); // 13 fraction digits
43
assertEquals("2003-02-28 23:59:59.999", jdt.toString());
44
45         jdt.set(2003, 2, 28, 23, 59, 59.99999999999999); // 14 fraction digits (max)
46
assertEquals("2003-02-28 23:59:59.999", jdt.toString());
47
48         jdt.set(2003, 2, 28, 23, 59, 59.999999999999999); // 15 decimals -> 59.9....9 = 60.0
49
assertEquals("2003-03-01 00:00:00.000", jdt.toString());
50     }
51
52
53     public void testDaysInMonth() {
54         JDateTime jdt = new JDateTime(2003, 1, 1);
55         assertEquals(31, jdt.getMonthLength());
56         assertEquals(28, jdt.getMonthLength(2));
57
58         jdt = new JDateTime(2000, 1, 1);
59         assertEquals(31, jdt.getMonthLength());
60         assertEquals(29, jdt.getMonthLength(2));
61     }
62
63
64     public void testToString() {
65
66         JDateTime jdt = new JDateTime(2003, 1, 1, 1, 1, 01.001);
67         assertEquals("2003-01-01 01:01:01.001", jdt.toString());
68
69         jdt.set(200, 10, 10, 10, 10, 10.012);
70         assertEquals("0200-10-10 10:10:10.012", jdt.toString());
71
72         jdt.set(2003, 10, 10, 10, 10, 10.123);
73         assertEquals("2003-10-10 10:10:10.123", jdt.toString());
74     }
75
76     public void testAddMonths() {
77         JDateTime gt = new JDateTime(2003, 1, 31);
78         gt.setMonthFix(Boolean.FALSE);
79         gt.addMonth(1);
80         assertEquals("2003-03-03 00:00:00.000", gt.toString());
81
82         gt.setDate(2003, 1, 31);
83         gt.addMonth(1, true);
84         assertEquals("2003-02-28 00:00:00.000", gt.toString());
85
86         gt.setDate(2004, 1, 31);
87         gt.addMonth(1);
88         assertEquals("2004-03-02 00:00:00.000", gt.toString());
89
90         gt.setDate(2004, 1, 31);
91         gt.addMonth(1, true);
92         assertEquals("2004-02-29 00:00:00.000", gt.toString());
93
94         gt.setDate(2003, 1, 25);
95         gt.add(0, 1, 6, true);
96         assertEquals("2003-02-28 00:00:00.000", gt.toString());
97
98         gt.setDate(2003, 1, 20);
99         gt.add(0, 10, 11, true);
100         assertEquals("2003-11-30 00:00:00.000", gt.toString());
101
102         gt.setDate(2004, 2, 29);
103         gt.addYear(1, true);
104         assertEquals("2005-02-28 00:00:00.000", gt.toString());
105
106
107
108         gt.setDate(2004, 2, 29);
109         gt.addYear(-1, true);
110         assertEquals("2003-02-28 00:00:00.000", gt.toString());
111
112         gt.setDate(2003, 11, 31); // == 2003-12-01
113
gt.add(0, -8, -31, true);
114         assertEquals("2003-02-28 00:00:00.000", gt.toString());
115         gt.setDate(2003, 11, 31);
116         gt.add(0, -8, -31, false);
117         assertEquals("2003-03-01 00:00:00.000", gt.toString());
118
119         gt.setDate(2004, 5, 31);
120         gt.addMonth(-3, true);
121         assertEquals("2004-02-29 00:00:00.000", gt.toString());
122         gt.setDate(2003, 11, 31);
123         gt.addMonth(-10, true);
124         assertEquals("2003-02-01 00:00:00.000", gt.toString());
125
126     }
127
128
129     public void testMiscSetsGets() {
130         JDateTime gt = new JDateTime(2003, 11, 26, 21, 8, 25.173);
131         
132         gt.setYear(2002);
133         assertEquals(2002, gt.getYear());
134
135         gt.setMonth(10);
136         assertEquals(10, gt.getMonth());
137         
138         gt.setDay(27);
139         assertEquals(27, gt.getDay());
140
141         gt.setHour(22);
142         assertEquals(22, gt.getHour());
143
144         gt.setMinute(8);
145         assertEquals(8, gt.getMinute());
146
147         gt.setSecond(24);
148         assertEquals(24, (int)gt.getSecond());
149         assertEquals(24.173, gt.getSecond(), 0.0005);
150
151         gt.setSecond(25.371);
152         assertEquals(25.371, gt.getSecond(), 0.0005);
153
154         gt.setMillisecond(173);
155         assertEquals(173, gt.getMillisecond());
156
157         assertEquals("2002-10-27 22:08:25.173", gt.toString());
158
159     }
160
161     public void testLeapYears() {
162
163         JDateTime gt = new JDateTime(1984, 2, 29);
164         assertTrue(gt.isLeapYear());
165         assertEquals("1984-02-29 00:00:00.000", gt.toString());
166
167         gt.set(1985, 2, 29);
168         assertFalse(gt.isLeapYear());
169         assertEquals("1985-03-01 00:00:00.000", gt.toString());
170
171         gt.set(2004, 2, 29);
172         assertTrue(gt.isLeapYear());
173         assertEquals("2004-02-29 00:00:00.000", gt.toString());
174
175         gt.set(1900, 2, 29);
176         assertFalse(gt.isLeapYear()); // not a leap year
177
assertEquals("1900-03-01 00:00:00.000", gt.toString());
178
179         gt.set(2000, 2, 29);
180         assertTrue(gt.isLeapYear()); // a leap year
181
assertEquals("2000-02-29 00:00:00.000", gt.toString());
182
183         gt.set(1600, 2, 29);
184         assertTrue(gt.isLeapYear()); // a leap year
185
assertEquals("1600-02-29 00:00:00.000", gt.toString());
186
187         for (int y = -4700; y < 5000; y++) {
188             gt.set(y, 2, 29);
189             assertEquals(31 + 29, gt.getDayOfYear());
190             if (gt.isLeapYear()) {
191                 assertEquals(29, gt.getDay());
192             } else {
193                 assertEquals(1, gt.getDay());
194             }
195         }
196     }
197
198
199     public void testFormatTemplates() {
200         JDateTime jdt = new JDateTime(1975, 1, 1);
201         assertEquals("2442413.5", jdt.getJulianDate().toString());
202         assertEquals("1975-01-01 00:00:00.000", jdt.toString());
203
204         jdt.setFormat("YYYY.MM.DD");
205         assertEquals("1975.01.01", jdt.toString());
206
207         String JavaDoc defaultTemplate = JDateTime.DEFAULT_FORMAT;
208         JDateTime.DEFAULT_FORMAT = "YYYY/MM/DD hh:mm";
209         assertEquals("1975.01.01", jdt.toString());
210
211         jdt.setFormat(null);
212         assertEquals("1975/01/01 00:00", jdt.toString());
213
214         assertEquals("01: January (Jan)", jdt.toString("MM: MML (MMS)"));
215         assertEquals("01 is 3: Wednesday (Wed)", jdt.toString("DD is D: DL (DS)"));
216
217         jdt.addDay(27);
218         assertEquals("028 / 05 / W05", jdt.toString("DDD / WW / WWW"));
219
220         JDateTime.DEFAULT_FORMAT = defaultTemplate;
221     }
222
223
224
225     public void testQuotes() {
226         JDateTime jdt = new JDateTime(1968, 9, 30);
227         assertEquals("SHE WAS Bsample'O'R'N ON 30 September 1968", jdt.toString("SHE 'WAS' 'Bsample''O''R''N' ON DD MML YYYY"));
228         assertEquals("5 is a week number and W is a letter", jdt.toString("W is a week number and 'W is a letter"));
229         assertEquals("1968YYYYY.", jdt.toString("YYYY'YYYYY'."));
230         assertEquals("19681968Y.", jdt.toString("YYYYYYYYY'."));
231         assertEquals("YY1968YYY", jdt.toString("'YY'YYYY'YY'Y"));
232         assertEquals("30 o'day DD30", jdt.toString("DD 'o''day DD'DD"));
233         
234     }
235
236
237
238
239     public void testSetFromString() {
240         JDateTime jdt = new JDateTime();
241         jdt.fromString("2003-11-24 23:18:38.173");
242         assertEquals("2003-11-24 23:18:38.173", jdt.toString());
243
244         jdt.fromString("2003-11-23");
245         assertEquals("2003-11-23 00:00:00.000", jdt.toString());
246
247         jdt.fromString("2003-14-23");
248         assertEquals("2004-02-23 00:00:00.000", jdt.toString());
249
250         jdt.fromString("2003-4-3");
251         assertEquals("2003-04-03 00:00:00.000", jdt.toString());
252
253         jdt.fromString("2003-4-x");
254         assertEquals("2003-04-03 00:00:00.000", jdt.toString()); // because of error, date is not changed
255

256         jdt.fromString("2003-11-23 13:67");
257         assertEquals("2003-11-23 14:07:00.000", jdt.toString());
258
259         jdt.fromString("2003-11-23 13:07:55");
260         assertEquals("2003-11-23 13:07:55.000", jdt.toString());
261
262         jdt.fromString("2003-11-23 13:07:5");
263         assertEquals("2003-11-23 13:07:05.000", jdt.toString());
264
265         jdt.fromString("123456-7-8");
266         assertEquals("123456-07-08 00:00:00.000", jdt.toString());
267
268         jdt.fromString("2003-11-23");
269         assertEquals("2003-11-23 00:00:00.000", jdt.toString());
270     }
271     
272     public void testLoadFromStoreTo() {
273         Calendar JavaDoc c = Calendar.getInstance();
274         c.set(2001, 0, 1, 2, 3, 4);
275         c.set(Calendar.MILLISECOND, 500);
276         JDateTime jdt = new JDateTime();
277         jdt.loadFrom(c);
278         assertEquals("2001-01-01 02:03:04.500", jdt.toString());
279         Calendar JavaDoc c1 = jdt.convertToCalendar();
280         assertEquals(2001, c1.get(Calendar.YEAR));
281         assertEquals(0, c1.get(Calendar.MONTH));
282         assertEquals(1, c1.get(Calendar.DAY_OF_MONTH));
283         assertEquals(2, c1.get(Calendar.HOUR_OF_DAY));
284         assertEquals(3, c1.get(Calendar.MINUTE));
285         assertEquals(4, c1.get(Calendar.SECOND));
286         assertEquals(500, c1.get(Calendar.MILLISECOND));
287
288
289         GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc(2002, 5, 2, 3, 4, 5);
290         gc.set(GregorianCalendar.MILLISECOND, 600);
291         jdt.loadFrom(gc);
292         assertEquals("2002-06-02 03:04:05.600", jdt.toString());
293         GregorianCalendar JavaDoc gc1 = new GregorianCalendar JavaDoc();
294         jdt.storeTo(gc1);
295         assertEquals(2002, gc1.get(GregorianCalendar.YEAR));
296         assertEquals(5, gc1.get(GregorianCalendar.MONTH));
297         assertEquals(2, gc1.get(GregorianCalendar.DAY_OF_MONTH));
298         assertEquals(3, gc1.get(GregorianCalendar.HOUR_OF_DAY));
299         assertEquals(4, gc1.get(GregorianCalendar.MINUTE));
300         assertEquals(5, gc1.get(GregorianCalendar.SECOND));
301         assertEquals(600, gc1.get(GregorianCalendar.MILLISECOND));
302
303         
304         Date JavaDoc d = new Date JavaDoc(101, 2, 3, 4, 5, 6);
305         jdt.loadFrom(d);
306         assertEquals("2001-03-03 04:05:06.000", jdt.toString());
307         Date JavaDoc d2 = jdt.convertToDate();
308         assertEquals(101, d2.getYear());
309         assertEquals(2, d2.getMonth());
310         assertEquals(3, d2.getDate());
311         assertEquals(4, d2.getHours());
312         assertEquals(5, d2.getMinutes());
313         assertEquals(6, d2.getSeconds());
314
315         
316         JDateTime gt_new = new JDateTime(2003, 6, 5, 4, 3, 2.1);
317         jdt.setJulianDate(gt_new.getJulianDate());
318         assertEquals("2003-06-05 04:03:02.100", jdt.toString());
319         JDateTime gt2 = (JDateTime) jdt.clone();
320         assertEquals(2003, gt2.getYear());
321         assertEquals(6, gt2.getMonth());
322         assertEquals(5, gt2.getDay());
323         assertEquals(4, gt2.getHour());
324         assertEquals(3, gt2.getMinute());
325         assertEquals(2, (int)gt2.getSecond());
326         assertEquals(100, gt2.getMillisecond());
327
328         
329         java.sql.Date JavaDoc sd = new java.sql.Date JavaDoc(123, 4, 5);
330         jdt.loadFrom(sd);
331         assertEquals("2023-05-05 00:00:00.000", jdt.toString());
332         java.sql.Date JavaDoc sd2 = new java.sql.Date JavaDoc(1, 2, 3);
333         jdt.storeTo(sd2);
334         assertEquals(123, sd2.getYear());
335         assertEquals(4, sd2.getMonth());
336         assertEquals(5, sd2.getDate());
337
338         
339         java.sql.Timestamp JavaDoc st = new java.sql.Timestamp JavaDoc(123, 4, 5, 6, 7, 8, 500000000);
340         jdt.loadFrom(st);
341         assertEquals("2023-05-05 06:07:08.500", jdt.toString());
342         java.sql.Timestamp JavaDoc st2 = jdt.convertToSqlTimestamp();
343         assertEquals(123, st2.getYear());
344         assertEquals(4, st2.getMonth());
345         assertEquals(5, st2.getDate());
346         assertEquals(6, st2.getHours());
347         assertEquals(7, st2.getMinutes());
348         assertEquals(8, st2.getSeconds());
349         assertEquals(500, st2.getNanos()/1000000);
350     }
351
352     public void testMillis() {
353         GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc();
354         JDateTime jdt = new JDateTime();
355
356         long now = System.currentTimeMillis();
357         gc.setTimeInMillis(now);
358         jdt.setTimeInMillis(now);
359         assertEquals(now, jdt.getTimeInMillis());
360         assertEquals(gc.getTimeInMillis(), jdt.getTimeInMillis());
361
362         JDateTime jdt2 = new JDateTime();
363         jdt2.setTimeInMillis(jdt.getTimeInMillis());
364         assertEquals(jdt.toString(), jdt2.toString());
365         assertEquals(gc.getTimeInMillis(), jdt2.getTimeInMillis());
366
367
368         for (int year = 1000; year < 3000; year++) {
369             if (year == 1582) continue;
370             jdt.setYear(year);
371             jdt.setMillisecond(0);
372             gc.set(Calendar.YEAR, year);
373             gc.set(Calendar.MILLISECOND, 0);
374             for (int month = 1; month <= 12; month++) {
375                 jdt.setMonth(month);
376                 gc.set(Calendar.MONTH, month - 1);
377                 for (int sec = 0; sec < 60; sec++) {
378                     jdt.setSecond(sec);
379                     gc.set(Calendar.SECOND, sec);
380                     assertEquals(gc.getTimeInMillis(), jdt.getTimeInMillis());
381
382                     jdt.setTimeInMillis(gc.getTimeInMillis());
383                     gc.setTimeInMillis(gc.getTimeInMillis());
384                     assertEquals(gc.getTimeInMillis(), jdt.getTimeInMillis());
385                 }
386             }
387         }
388     }
389
390     public void testTimeZones() {
391         GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc();
392         JDateTime jdt1 = new JDateTime();
393         gc.setTimeInMillis(jdt1.getTimeInMillis());
394
395         TimeZone JavaDoc tz = TimeZone.getTimeZone("GMT+01:00");
396         jdt1.setTimeZone(tz);
397         gc.setTimeZone(tz);
398         assertEquals(gc.getTimeInMillis(), jdt1.getTimeInMillis());
399
400         JDateTime jdt2 = (JDateTime) jdt1.clone();
401         assertEquals(jdt1, jdt2);
402
403         tz = TimeZone.getTimeZone("GMT+02:00");
404         jdt2.setTimeZone(tz);
405         gc.setTimeZone(tz);
406         assertEquals(gc.getTimeInMillis(), jdt1.getTimeInMillis());
407
408         if (jdt2.getHour() != 0) {
409             assertEquals(jdt1.getHour() + 1, jdt2.getHour());
410             assertEquals(jdt1.getMinute(), jdt2.getMinute());
411             assertEquals((int)jdt1.getSecond(), (int) jdt2.getSecond());
412         }
413
414         tz = TimeZone.getTimeZone("GMT-12:00");
415         jdt1.setTimeZone(tz);
416         gc.setTimeZone(tz);
417         assertEquals(gc.getTimeInMillis(), jdt1.getTimeInMillis());
418
419         tz = TimeZone.getTimeZone("GMT+10:00");
420         jdt2.setTimeZone(tz);
421         gc.setTimeZone(tz);
422         assertEquals(gc.getTimeInMillis(), jdt2.getTimeInMillis());
423         assertEquals(jdt1.getTimeInMillis(), jdt2.getTimeInMillis());
424
425         java.util.Date JavaDoc date = jdt1.convertToDate();
426         assertEquals(date.getTime(), jdt1.getTimeInMillis());
427
428         GregorianCalendar JavaDoc cal = jdt1.convertToGregorianCalendar();
429         assertEquals(cal.getTimeInMillis(), jdt1.getTimeInMillis());
430         assertEquals(cal.getTimeZone(), jdt1.getTimeZone());
431
432         java.sql.Date JavaDoc sqlDate = jdt1.convertToSqlDate();
433         assertEquals(sqlDate.getTime(), jdt1.getTimeInMillis());
434
435         java.sql.Timestamp JavaDoc sqlTimestamp = jdt1.convertToSqlTimestamp();
436         assertEquals(sqlTimestamp.getTime(), jdt1.getTimeInMillis());
437     }
438
439 }
440
Popular Tags