1 package org.oddjob.schedules; 2 3 import java.text.ParseException ; 4 import java.text.SimpleDateFormat ; 5 import java.util.Calendar ; 6 import java.util.Date ; 7 import java.util.TimeZone ; 8 9 import org.oddjob.util.DateHelper; 10 11 import junit.framework.TestCase; 12 13 16 public class DateUtilsTest extends TestCase { 17 18 static SimpleDateFormat dateFormat = new SimpleDateFormat ("dd-MMM-yy"); 19 static SimpleDateFormat timeFormat = new SimpleDateFormat ("dd-MMM-yy HH:mm"); 20 21 25 public DateUtilsTest(String arg0) { 26 super(arg0); 27 } 28 29 static Date parseDate(String s) { 30 try { 31 return dateFormat.parse(s); 32 } catch (Exception e) { 33 e.printStackTrace(); 34 } 35 return null; 36 } 37 38 static Date parseTime(String s) { 39 try { 40 return timeFormat.parse(s); 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } 44 return null; 45 } 46 47 50 public void testStartOfDayDate() { 51 assertTrue(DateUtils.startOfDay( 52 parseTime("11-jul-03 12:27"), TimeZone.getDefault()) 53 .equals(parseDate("11-jul-03"))); 54 } 55 56 59 public void testEndOfDayDate() { 60 assertTrue(DateUtils.endOfDay( 61 parseTime("11-jul-03 12:27"), TimeZone.getDefault()).equals( 62 DateUtils.oneMillisBefore(parseDate("12-jul-03")))); 63 } 64 65 68 public void testDayOfWeekDate() { 69 70 assertTrue(DateUtils.dayOfWeek( 71 parseTime("11-jul-03 12:27"), TimeZone.getDefault()) == 6); 72 } 73 74 77 public void testDayOfMonthDate() { 78 79 assertTrue(DateUtils.dayOfMonth( 80 parseTime("11-jul-03 12:27"), TimeZone.getDefault()) == 11); 81 } 82 83 84 public void testCompareCalendars() throws ParseException { 85 Calendar c1 = Calendar.getInstance(); 86 c1.setTime(DateHelper.parseDateTime("21-jun-05 10:00")); 87 Calendar c2 = Calendar.getInstance(); 88 c2.setTime(DateHelper.parseDateTime("21-jun-05 12:00")); 89 90 assertEquals(-1, DateUtils.compare(c1, c2)); 91 assertEquals(1, DateUtils.compare(c2, c1)); 92 assertEquals(0, DateUtils.compare(c1, c1)); 93 } 94 } 95 | Popular Tags |