1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.util;
17
18 import java.util.Calendar;
19 import java.util.Date;
20 import java.util.GregorianCalendar;
21
22 import org.junit.Test;
23 import org.kuali.rice.kns.util.DateUtils;
24 import org.kuali.test.KNSTestCase;
25
26 public class DateUtilsTest extends KNSTestCase {
27 Calendar nowCal;
28 Calendar prevCal;
29 Calendar nextCal;
30
31 Date nowDate;
32 Date prevDate;
33 Date nextDate;
34
35 @Override
36 public void setUp() throws Exception {
37 super.setUp();
38
39 nowCal = GregorianCalendar.getInstance();
40 nowDate = nowCal.getTime();
41
42 prevCal = new GregorianCalendar();
43 prevCal.setTime(nowDate);
44 prevCal.add(Calendar.DAY_OF_YEAR, -1);
45 prevDate = prevCal.getTime();
46
47 nextCal = new GregorianCalendar();
48 nextCal.setTime(nowDate);
49 nextCal.add(Calendar.DAY_OF_YEAR, 1);
50 nextDate = nextCal.getTime();
51
52 assertTrue(prevCal.before(nowCal));
53 assertTrue(nextCal.after(nowCal));
54
55 assertTrue(prevDate.before(nowDate));
56 assertTrue(nextDate.after(nowDate));
57 }
58
59 @Test public void testIsSameDayDateDate_prevDate() {
60 assertFalse(DateUtils.isSameDay(prevDate, nowDate));
61 }
62
63 @Test public void testIsSameDayDateDate_sameDate() {
64 assertTrue(DateUtils.isSameDay(nowDate, nowDate));
65 }
66
67 @Test public void testIsSameDayDateDate_nextDate() {
68 assertFalse(DateUtils.isSameDay(nowDate, nextDate));
69 }
70
71 @Test public void testIsSameDayCalendarCalendar_prevCal() {
72 assertFalse(DateUtils.isSameDay(nowCal, prevCal));
73 }
74
75 @Test public void testIsSameDayCalendarCalendar_sameCal() {
76 assertTrue(DateUtils.isSameDay(nowCal, nowCal));
77 }
78
79 @Test public void testIsSameDayCalendarCalendar_nextCal() {
80 assertFalse(DateUtils.isSameDay(nextCal, nowCal));
81 }
82
83 @Test public void testConvertToSqlDate() {
84 java.sql.Date sqlDate = DateUtils.convertToSqlDate(nowDate);
85 assertEquals(sqlDate.getTime(), nowDate.getTime());
86 }
87
88 @Test public void testClearTimeFieldsDate() {
89 java.util.Date timeless = DateUtils.clearTimeFields(nowDate);
90
91 assertTimeCleared(timeless.getTime());
92 }
93
94 @Test public void testClearTimeFieldsSqlDate() {
95 java.sql.Date timeless = DateUtils.clearTimeFields(DateUtils.convertToSqlDate(nowDate));
96
97 assertTimeCleared(timeless.getTime());
98 }
99
100
101 private void assertTimeCleared(long timeInMilliseconds) {
102 Calendar cal = new GregorianCalendar();
103 cal.setTimeInMillis(timeInMilliseconds);
104
105 assertFalse(0 == cal.get(Calendar.YEAR));
106
107 assertFalse(0 == cal.get(Calendar.DATE));
108 assertEquals(0, cal.get(Calendar.HOUR_OF_DAY));
109 assertEquals(0, cal.get(Calendar.MINUTE));
110 assertEquals(0, cal.get(Calendar.SECOND));
111 assertEquals(0, cal.get(Calendar.MILLISECOND));
112 }
113 }