KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > datetime > TestJDateTime2


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