Java tutorial
import java.util.Calendar; import java.util.logging.Logger; /** * {@link java.util.Calendar} utilities * * @author Kazuhiro Sera */ public final class CalendarUtil { public static Calendar testData; public static Calendar getCurrentTime() { if (testData != null) { Calendar result = deepCopy(testData); testData = null; return result; } else { return Calendar.getInstance(); } } public static Calendar getCurrentTruncDate() { return dateTrunc(getCurrentTime()); } public static Integer getYear(Calendar calendar) { Integer year = calendar.get(Calendar.YEAR); return year; } public static Integer getMonth(Calendar calendar) { Integer month = calendar.get(Calendar.MONTH) + 1; return month; } public static Integer getDay(Calendar calendar) { Integer day = calendar.get(Calendar.DATE); return day; } public static Integer getDayOfWeekNumber(Calendar cal) { return cal.get(Calendar.DAY_OF_WEEK); } public static Integer get24Hour(Calendar calendar) { Integer hour = calendar.get(Calendar.HOUR_OF_DAY); return hour; } public static Integer getMinute(Calendar calendar) { Integer minute = calendar.get(Calendar.MINUTE); return minute; } public static Integer getSecond(Calendar calendar) { Integer second = calendar.get(Calendar.SECOND); return second; } public static Integer getMillisecond(Calendar calendar) { Integer millisecond = calendar.get(Calendar.MILLISECOND); return millisecond; } public static Calendar getCalendar(String yyyy, String mm, String dd) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, Integer.valueOf(yyyy)); cal.set(Calendar.MONTH, Integer.valueOf(mm) - 1); cal.set(Calendar.DATE, Integer.valueOf(dd)); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal; } public static Calendar getCalendar(int yyyy, int mm, int dd) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, yyyy); cal.set(Calendar.MONTH, mm - 1); cal.set(Calendar.DATE, dd); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal; } public static Calendar getCalendar(String yyyy, String mm, String dd, String hh, String mi, String ss) { Calendar cal = getCalendar(yyyy, mm, dd); cal.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hh)); cal.set(Calendar.MINUTE, Integer.valueOf(mi)); cal.set(Calendar.SECOND, Integer.valueOf(ss)); return cal; } public static Calendar getCalendar(int yyyy, int mm, int dd, int hh, int mi, int ss) { Calendar cal = getCalendar(yyyy, mm, dd); cal.set(Calendar.HOUR_OF_DAY, hh); cal.set(Calendar.MINUTE, mi); cal.set(Calendar.SECOND, ss); return cal; } public static Calendar dateTrunc(Calendar calendar) { if (calendar == null) { return null; } Calendar date = (Calendar) calendar.clone(); date.set(Calendar.HOUR_OF_DAY, 0); date.set(Calendar.MINUTE, 0); date.set(Calendar.SECOND, 0); date.set(Calendar.MILLISECOND, 0); return date; } public static boolean isFirstAfterSecond(Calendar first, Calendar second) { long firstValue = first.getTimeInMillis(); long secondValue = second.getTimeInMillis(); return (firstValue > secondValue) ? true : false; } public static Calendar deepCopy(Calendar src) { Calendar dest = Calendar.getInstance(); dest.setTimeInMillis(src.getTimeInMillis()); return dest; } public static Calendar addYears(Calendar src, int years) { Calendar dest = deepCopy(src); dest.add(Calendar.YEAR, years); return dest; } public static Calendar addMonths(Calendar src, int months) { Calendar dest = deepCopy(src); dest.add(Calendar.MONTH, months); return dest; } public static Calendar addDays(Calendar src, int days) { Calendar dest = deepCopy(src); dest.add(Calendar.DATE, days); return dest; } public static Calendar addHours(Calendar src, int hours) { Calendar dest = deepCopy(src); dest.add(Calendar.HOUR_OF_DAY, hours); return dest; } public static Calendar addMinutes(Calendar src, int minutes) { Calendar dest = deepCopy(src); dest.add(Calendar.MINUTE, minutes); return dest; } public static Calendar addSeconds(Calendar src, int seconds) { Calendar dest = deepCopy(src); dest.add(Calendar.SECOND, seconds); return dest; } public static Calendar getCalendar(long timeInMillis) { Calendar dest = Calendar.getInstance(); dest.setTimeInMillis(timeInMillis); return dest; } public static String toYYYYMMDDHHMISS(Calendar calendar) { return String.format("%04d", getYear(calendar)) + String.format("%02d", getMonth(calendar)) + String.format("%02d", getDay(calendar)) + String.format("%02d", get24Hour(calendar)) + String.format("%02d", getMinute(calendar)) + String.format("%02d", getSecond(calendar)); } }------package net.seratch.taskun.util; import java.util.Calendar; import junit.framework.TestCase; public class CalendarUtilTest extends TestCase { public void test_getYear_A$Calendar() throws Exception { Integer expected = 2009; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.YEAR, expected); Integer actual = CalendarUtil.getYear(arg0); assertEquals(expected, actual); } public void test_getYear_A$Calendar_1899() throws Exception { Integer expected = 1899; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.YEAR, expected); Integer actual = CalendarUtil.getYear(arg0); assertEquals(expected, actual); } public void test_getYear_A$Calendar_1900() throws Exception { Integer expected = 1900; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.YEAR, expected); Integer actual = CalendarUtil.getYear(arg0); assertEquals(expected, actual); } public void test_getYear_A$Calendar_2037() throws Exception { Integer expected = 2037; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.YEAR, expected); Integer actual = CalendarUtil.getYear(arg0); assertEquals(expected, actual); } public void test_getYear_A$Calendar_2038() throws Exception { Integer expected = 2038; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.YEAR, expected); Integer actual = CalendarUtil.getYear(arg0); assertEquals(expected, actual); } public void test_getMonth_A$Calendar() throws Exception { Integer expected = 10; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.MONTH, expected - 1); Integer actual = CalendarUtil.getMonth(arg0); assertEquals(expected, actual); } public void test_getMonth_A$Calendar_jun0() throws Exception { Integer expected = 1; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.MONTH, 0); Integer actual = CalendarUtil.getMonth(arg0); assertEquals(expected, actual); } public void test_getMonth_A$Calendar_jun12() throws Exception { Integer expected = 1; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.MONTH, 12); Integer actual = CalendarUtil.getMonth(arg0); assertEquals(expected, actual); } public void test_getMonth_A$Calendar_dec() throws Exception { Integer expected = 12; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.MONTH, 11); Integer actual = CalendarUtil.getMonth(arg0); assertEquals(expected, actual); } public void test_getDay_A$Calendar() throws Exception { Integer expected = 10; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.DATE, expected); Integer actual = CalendarUtil.getDay(arg0); assertEquals(expected, actual); } public void test_get24Hour_A$Calendar() throws Exception { Integer expected = 23; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.HOUR_OF_DAY, expected); Integer actual = CalendarUtil.get24Hour(arg0); assertEquals(expected, actual); } public void test_get24Hour_A$Calendar_0() throws Exception { Integer expected = 0; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.HOUR_OF_DAY, expected); Integer actual = CalendarUtil.get24Hour(arg0); assertEquals(expected, actual); } public void test_get24Hour_A$Calendar_24() throws Exception { Integer expected = 0; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.HOUR_OF_DAY, 24); Integer actual = CalendarUtil.get24Hour(arg0); assertEquals(expected, actual); } public void test_getMinute_A$Calendar() throws Exception { Integer expected = 59; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.MINUTE, expected); Integer actual = CalendarUtil.getMinute(arg0); assertEquals(expected, actual); } public void test_getMinute_A$Calendar_60() throws Exception { Integer expected = 0; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.MINUTE, 60); Integer actual = CalendarUtil.getMinute(arg0); assertEquals(expected, actual); } public void test_getMinute_A$Calendar_0() throws Exception { Integer expected = 0; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.MINUTE, expected); Integer actual = CalendarUtil.getMinute(arg0); assertEquals(expected, actual); } public void test_getSecond_A$Calendar() throws Exception { Integer expected = 33; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.SECOND, expected); Integer actual = CalendarUtil.getSecond(arg0); assertEquals(expected, actual); } public void test_getSecond_A$Calendar_60() throws Exception { Integer expected = 0; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.SECOND, 60); Integer actual = CalendarUtil.getSecond(arg0); assertEquals(expected, actual); } public void test_getSecond_A$Calendar_0() throws Exception { Integer expected = 0; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.SECOND, expected); Integer actual = CalendarUtil.getSecond(arg0); assertEquals(expected, actual); } public void test_getCalendar_A$String$String$String() throws Exception { long expected = 1262185200000L; String arg0 = "2009"; String arg1 = "12"; String arg2 = "31"; Calendar actual = CalendarUtil.getCalendar(arg0, arg1, arg2); assertEquals(expected, actual.getTimeInMillis()); } public void test_getCalendar_A$String$String$String$String$String$String() throws Exception { long expected = 1262271599000L; String arg0 = "2009"; String arg1 = "12"; String arg2 = "31"; String arg3 = "23"; String arg4 = "59"; String arg5 = "59"; Calendar actual = CalendarUtil.getCalendar(arg0, arg1, arg2, arg3, arg4, arg5); assertEquals(expected, actual.getTimeInMillis()); } public void test_dateTrunc_A$Calendar() throws Exception { Calendar today = CalendarUtil.dateTrunc(Calendar.getInstance()); assertEquals(0, today.get(Calendar.HOUR_OF_DAY)); assertEquals(0, today.get(Calendar.MINUTE)); assertEquals(0, today.get(Calendar.SECOND)); assertEquals(0, today.get(Calendar.MILLISECOND)); } public void test_getCurrentTime_A$() throws Exception { Calendar actual = CalendarUtil.getCurrentTime(); assertNotNull(actual); } public void test_getCurrentTime_A$_testData() throws Exception { CalendarUtil.testData = Calendar.getInstance(); Calendar actual = CalendarUtil.getCurrentTime(); assertNotNull(actual); assertNull(CalendarUtil.testData); } public void test_getCurrentTruncDate_A$() throws Exception { Calendar actual = CalendarUtil.getCurrentTruncDate(); assertEquals(0, actual.get(Calendar.HOUR)); assertEquals(0, actual.get(Calendar.MINUTE)); assertEquals(0, actual.get(Calendar.SECOND)); assertEquals(0, actual.get(Calendar.MILLISECOND)); } public void test_isFirstAfterSecond_A$Calendar$Calendar_true() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "3", "1"); Calendar arg1 = CalendarUtil.getCalendar("2010", "2", "28"); // when boolean actual = CalendarUtil.isFirstAfterSecond(arg0, arg1); // then boolean expected = true; assertEquals(expected, actual); } public void test_isFirstAfterSecond_A$Calendar$Calendar_false() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "3", "1"); Calendar arg1 = CalendarUtil.getCalendar("2010", "2", "28"); // when boolean actual2 = CalendarUtil.isFirstAfterSecond(arg1, arg0); // then boolean expected2 = false; assertEquals(expected2, actual2); } public void test_isFirstAfterSecond_A$Calendar$Calendar_same() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "3", "1"); // when boolean actual3 = CalendarUtil.isFirstAfterSecond(arg0, arg0); // then boolean expected3 = false; assertEquals(expected3, actual3); } public void test_deepCopy_A$Calendar() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "3", "1"); // when Calendar actual = CalendarUtil.deepCopy(arg0); // then actual.add(Calendar.SECOND, 1); assertNotSame(arg0, actual); assertTrue(CalendarUtil.isFirstAfterSecond(actual, arg0)); } public void test_addYears_A$Calendar$int_plus() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "3", "1"); // when Calendar actual = CalendarUtil.addYears(arg0, 1); // then assertNotSame(arg0.getTimeInMillis(), actual.getTimeInMillis()); assertEquals((int) 2011, (int) CalendarUtil.getYear(actual)); } public void test_addYears_A$Calendar$int_minus() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "3", "1"); // when Calendar actual = CalendarUtil.addYears(arg0, -1); // then assertNotSame(arg0.getTimeInMillis(), actual.getTimeInMillis()); assertEquals((int) 2009, (int) CalendarUtil.getYear(actual)); } public void test_addMonths_A$Calendar$int_plus() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "2", "1"); // when Calendar actual = CalendarUtil.addMonths(arg0, 1); // then assertNotSame(arg0.getTimeInMillis(), actual.getTimeInMillis()); assertEquals((int) 3, (int) CalendarUtil.getMonth(actual)); } public void test_addMonths_A$Calendar$int_minus() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "2", "1"); // when Calendar actual = CalendarUtil.addMonths(arg0, -1); // then assertNotSame(arg0.getTimeInMillis(), actual.getTimeInMillis()); assertEquals((int) 1, (int) CalendarUtil.getMonth(actual)); } public void test_addDays_A$Calendar$int_plus() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "2", "28"); // when Calendar actual = CalendarUtil.addDays(arg0, 1); // then assertEquals((int) 3, (int) CalendarUtil.getMonth(actual)); assertEquals((int) 1, (int) CalendarUtil.getDay(actual)); } public void test_addDays_A$Calendar$int_minus() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "2", "28"); // when Calendar actual = CalendarUtil.addDays(arg0, -1); // then assertNotSame(arg0.getTimeInMillis(), actual.getTimeInMillis()); assertEquals((int) 27, (int) CalendarUtil.getDay(actual)); } public void test_addHours_A$Calendar$int_plus() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "2", "1", "22", "23", "34"); // when Calendar actual = CalendarUtil.addHours(arg0, 1); // then assertNotSame(arg0.getTimeInMillis(), actual.getTimeInMillis()); assertEquals((int) 23, (int) CalendarUtil.get24Hour(actual)); } public void test_addHours_A$Calendar$int_minus() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "2", "1", "22", "23", "34"); // when Calendar actual = CalendarUtil.addHours(arg0, -1); // then assertNotSame(arg0.getTimeInMillis(), actual.getTimeInMillis()); assertEquals((int) 21, (int) CalendarUtil.get24Hour(actual)); } public void test_addMinutes_A$Calendar$int_plus() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "2", "1", "22", "23", "34"); // when Calendar actual = CalendarUtil.addMinutes(arg0, 1); // then assertNotSame(arg0.getTimeInMillis(), actual.getTimeInMillis()); assertEquals((int) 24, (int) CalendarUtil.getMinute(actual)); } public void test_addMinutes_A$Calendar$int_minus() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "2", "1", "22", "23", "34"); // when Calendar actual = CalendarUtil.addMinutes(arg0, -1); // then assertNotSame(arg0.getTimeInMillis(), actual.getTimeInMillis()); assertEquals((int) 22, (int) CalendarUtil.getMinute(actual)); } public void test_addSeconds_A$Calendar$int_plus() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "2", "1", "22", "23", "34"); // when Calendar actual = CalendarUtil.addSeconds(arg0, 1); // then assertNotSame(arg0.getTimeInMillis(), actual.getTimeInMillis()); assertEquals((int) 35, (int) CalendarUtil.getSecond(actual)); } public void test_addSeconds_A$Calendar$int_minus() throws Exception { // given Calendar arg0 = CalendarUtil.getCalendar("2010", "2", "1", "22", "23", "34"); // when Calendar actual = CalendarUtil.addSeconds(arg0, -1); // then assertNotSame(arg0.getTimeInMillis(), actual.getTimeInMillis()); assertEquals((int) 33, (int) CalendarUtil.getSecond(actual)); } public void test_getMillisecond_A$Calendar() throws Exception { Integer expected = 234; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.MILLISECOND, expected); Integer actual = CalendarUtil.getMillisecond(arg0); assertEquals(expected, actual); } public void test_getMillisecond_A$Calendar_999() throws Exception { Integer expected = 999; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.MILLISECOND, expected); Integer actual = CalendarUtil.getMillisecond(arg0); assertEquals(expected, actual); } public void test_getMillisecond_A$Calendar_1000() throws Exception { Integer expected = 0; Calendar arg0 = Calendar.getInstance(); arg0.set(Calendar.MILLISECOND, 1000); Integer actual = CalendarUtil.getMillisecond(arg0); assertEquals(expected, actual); } public void test_getCalendar_A$int$int$int() throws Exception { Calendar actual = CalendarUtil.getCalendar(2000, 2, 3); assertTrue(2000 == actual.get(Calendar.YEAR)); assertTrue(1 == actual.get(Calendar.MONTH)); assertTrue(3 == actual.get(Calendar.DATE)); } public void test_getCalendar_A$int$int$int$int$int$int() throws Exception { Calendar actual = CalendarUtil.getCalendar(2000, 2, 3, 4, 5, 6); assertTrue(2000 == actual.get(Calendar.YEAR)); assertTrue(1 == actual.get(Calendar.MONTH)); assertTrue(3 == actual.get(Calendar.DATE)); assertTrue(4 == actual.get(Calendar.HOUR)); assertTrue(5 == actual.get(Calendar.MINUTE)); assertTrue(6 == actual.get(Calendar.SECOND)); } public void test_getDayOfWeekNumber_A$Calendar() throws Exception { assertEquals((int) 1, (int) CalendarUtil.getDayOfWeekNumber(CalendarUtil.getCalendar("2010", "4", "18"))); assertEquals((int) 2, (int) CalendarUtil.getDayOfWeekNumber(CalendarUtil.getCalendar("2010", "4", "19"))); assertEquals((int) 3, (int) CalendarUtil.getDayOfWeekNumber(CalendarUtil.getCalendar("2010", "4", "20"))); assertEquals((int) 4, (int) CalendarUtil.getDayOfWeekNumber(CalendarUtil.getCalendar("2010", "4", "21"))); assertEquals((int) 5, (int) CalendarUtil.getDayOfWeekNumber(CalendarUtil.getCalendar("2010", "4", "22"))); assertEquals((int) 6, (int) CalendarUtil.getDayOfWeekNumber(CalendarUtil.getCalendar("2010", "4", "23"))); assertEquals((int) 7, (int) CalendarUtil.getDayOfWeekNumber(CalendarUtil.getCalendar("2010", "4", "24"))); } public void test_getCalendar_A$long() throws Exception { long arg0 = 12345L; Calendar actual = CalendarUtil.getCalendar(arg0); assertEquals(arg0, actual.getTimeInMillis()); } public void test_toYYYYMMDDHHMISS_A$Calendar() throws Exception { Calendar arg0 = CalendarUtil.getCalendar("2010", "02", "3", "1", "2", "3"); String actual = CalendarUtil.toYYYYMMDDHHMISS(arg0); String expected = "20100203010203"; assertEquals(expected, actual); } }