KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > datetime > JDateTimeMoreTest


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

3 package jodd.datetime;
4
5 import java.util.GregorianCalendar JavaDoc;
6
7 import junit.framework.TestCase;
8
9 public class JDateTimeMoreTest extends TestCase {
10
11     public void test1582() {
12         JDateTime jdt1582 = new JDateTime(1582, 10, 4);
13         assertEquals(4, jdt1582.getDayOfWeek());
14         jdt1582.addDay(1);
15         assertEquals(1582, jdt1582.getYear());
16         assertEquals(10, jdt1582.getMonth());
17         assertEquals(15, jdt1582.getDay());
18         assertEquals(5, jdt1582.getDayOfWeek());
19     }
20
21     public void testCompareToAndAdd() {
22         JDateTime gt1 = new JDateTime();
23         if (gt1.getDay() > 28) { // back and forth adds works without corrections
24
gt1.setDay(28); // for days that exists in all months
25
}
26
27         // check for year 1582
28
if (gt1.getMonth() == 10) {
29             if ((gt1.getDay() > 4) && ((gt1.getDay() < 15))) {
30                 gt1.setDay(4);
31             }
32         }
33
34         JDateTime gt2 = (JDateTime) gt1.clone();
35         assertEquals(0, gt1.compareTo(gt2));
36
37         for (int i = 1; i < 1000; i++) {
38             gt2.add(i, 0, 0);
39             assertEquals(-1, gt1.compareTo(gt2));
40             gt2.addYear(-2 * i);
41             assertEquals(1, gt1.compareTo(gt2));
42             gt2.addYear(i);
43             assertEquals(0, gt1.compareTo(gt2));
44         }
45         
46         for (int i = 1; i < 60000; i++) {
47             gt2.add(0, i, 0);
48             assertEquals(-1, gt1.compareTo(gt2));
49             gt2.addMonth(-i);
50             assertEquals(0, gt1.compareTo(gt2));
51         }
52         for (int i = 1; i < 5000; i++) { // Year 1582, months moving
53
gt2.add(0, i, 0); // before it still doesn't work
54
assertEquals(-1, gt1.compareTo(gt2));
55             gt2.addMonth(-2 * i);
56             assertEquals(1, gt1.compareTo(gt2));
57             gt2.addMonth(i);
58             assertEquals(0, gt1.compareTo(gt2));
59         }
60
61         for (int i = 1; i < 10000; i++) {
62             gt2.add(0, 0, i);
63             assertEquals(-1, gt1.compareTo(gt2));
64             gt2.addDay(-3 * i);
65             assertEquals(1, gt1.compareTo(gt2));
66             gt2.addDay(2 * i);
67             assertEquals(0, gt1.compareTo(gt2));
68         }
69
70         for (int i = 1; i < 100000; i++) {
71             gt2.addTime(i, 0, 0);
72             assertEquals(-1, gt1.compareTo(gt2));
73             gt2.addHour(-4 * i);
74             assertEquals(1, gt1.compareTo(gt2));
75             gt2.addHour(3 * i);
76             assertEquals(0, gt1.compareTo(gt2));
77         }
78
79         for (int i = 1; i < 100000; i++) {
80             gt2.addTime(0, i, 0);
81             assertEquals(-1, gt1.compareTo(gt2));
82             gt2.addMinute(-5 * i);
83             assertEquals(1, gt1.compareTo(gt2));
84             gt2.addMinute(4 * i);
85             assertEquals(0, gt1.compareTo(gt2));
86         }
87
88         for (int i = 1; i < 1000000; i++) {
89             gt2.addTime(0, 0, i);
90             assertEquals(-1, gt1.compareTo(gt2));
91             gt2.addSecond(- 7 * i);
92             assertEquals(1, gt1.compareTo(gt2));
93             gt2.addSecond(6 * i);
94             assertEquals(0, gt1.compareTo(gt2));
95         }
96
97         for (int i = 1; i < 1000000; i++) {
98             gt2.addTime(0, 0, i / 1000.0);
99             assertEquals(-1, gt1.compareTo(gt2));
100             gt2.addMillisecond(-8 * i);
101             assertEquals(1, gt1.compareTo(gt2));
102             gt2.addMillisecond(7 * i);
103             assertEquals(0, gt1.compareTo(gt2));
104         }
105     }
106     
107
108
109
110     public void testWeekOfYear() {
111         JDateTime gt = new JDateTime();
112         GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc();
113         int[] _fdiw = {0, GregorianCalendar.MONDAY, GregorianCalendar.TUESDAY, GregorianCalendar.WEDNESDAY, GregorianCalendar.THURSDAY, GregorianCalendar.FRIDAY, GregorianCalendar.SATURDAY, GregorianCalendar.SUNDAY};
114         
115         // test all starting dates (first day in week)
116
for (int fdiw = 1; fdiw <= 7; fdiw++) {
117             gc.setFirstDayOfWeek(_fdiw[fdiw]);
118
119             // test all minimal days in first week
120
for (int min = 1; min <= 7; min++) {
121                 gc.setMinimalDaysInFirstWeek(min);
122                 gt.setWeekDefinitionAlt(fdiw, min);
123
124                 // test many years
125
for (int y = 1800; y < 3000; y++) {
126
127                     if (y == 1916) {
128                         continue; // skip this year due to specific daylight savings
129
}
130
131                     gt.set(y, 1, 1);
132                     gc.set(y, 0, 1);
133         
134                     int total = gt.isLeapYear() ? 366 : 365;
135                     
136                     // test all days
137
for (int i = 0; i < total; i++) {
138                         assertEquals(gc.get(GregorianCalendar.DAY_OF_MONTH), gt.getDay());
139                         assertEquals(gc.get(GregorianCalendar.MONTH) + 1, gt.getMonth());
140                         assertEquals(gc.get(GregorianCalendar.YEAR), gt.getYear());
141                         assertEquals(gc.get(GregorianCalendar.DAY_OF_YEAR), gt.getDayOfYear());
142                         int dow = gc.get(GregorianCalendar.DAY_OF_WEEK) - 1;
143                         if (dow == 0) {
144                             dow = 7;
145                         }
146                         assertEquals(dow, gt.getDayOfWeek());
147                         assertEquals(gc.get(GregorianCalendar.WEEK_OF_YEAR), gt.getWeekOfYear());
148                         assertEquals(gc.get(GregorianCalendar.WEEK_OF_MONTH), gt.getWeekOfMonth());
149
150                         gt.addDay(1);
151                         gc.roll(GregorianCalendar.DAY_OF_YEAR, true);
152                     }
153                 }
154             }
155         }
156     }
157 }
158
Popular Tags