/** * Determines how two dates compare up to no more than the specified * most significant field. * * @param date1 the first date, not <code>null</code> * @param date2 the second date, not <code>null</code> * @param field the field from <code>Calendar</code> * @return a negative integer, zero, or a positive integer as the first * date is less than, equal to, or greater than the second. * @throws IllegalArgumentException if any argument is <code>null</code> * @see #truncate(Calendar, int) * @see #truncatedCompareTo(Date, Date, int) * @since 3.0 */ public static int truncatedCompareTo(final Date date1, final Date date2, final int field) { final Date truncatedDate1 = truncate(date1, field); final Date truncatedDate2 = truncate(date2, field); return truncatedDate1.compareTo(truncatedDate2); }
/** * Determines how two calendars compare up to no more than the specified * most significant field. * * @param cal1 the first calendar, not <code>null</code> * @param cal2 the second calendar, not <code>null</code> * @param field the field from {@code Calendar} * @return a negative integer, zero, or a positive integer as the first * calendar is less than, equal to, or greater than the second. * @throws IllegalArgumentException if any argument is <code>null</code> * @see #truncate(Calendar, int) * @see #truncatedCompareTo(Date, Date, int) * @since 3.0 */ public static int truncatedCompareTo(final Calendar cal1, final Calendar cal2, final int field) { final Calendar truncatedCal1 = truncate(cal1, field); final Calendar truncatedCal2 = truncate(cal2, field); return truncatedCal1.compareTo(truncatedCal2); }
/** * <p>Truncates a date, leaving the field specified as the most * significant field.</p> * * <p>For example, if you had the date-time of 28 Mar 2002 * 13:45:01.231, if you passed with HOUR, it would return 28 Mar * 2002 13:00:00.000. If this was passed with MONTH, it would * return 1 Mar 2002 0:00:00.000.</p> * * @param date the date to work with, either {@code Date} or {@code Calendar}, not null * @param field the field from {@code Calendar} or <code>SEMI_MONTH</code> * @return the different truncated date, not null * @throws IllegalArgumentException if the date is <code>null</code> * @throws ClassCastException if the object type is not a {@code Date} or {@code Calendar} * @throws ArithmeticException if the year is over 280 million */ public static Date truncate(final Object date, final int field) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } if (date instanceof Date) { return truncate((Date) date, field); } else if (date instanceof Calendar) { return truncate((Calendar) date, field).getTime(); } else { throw new ClassCastException("Could not truncate " + date); } }
/** * 2016-11-10 07:33:23, 则返回2016-11-1 00:00:00 */ public static Date beginOfMonth(@NotNull final Date date) { return DateUtils.truncate(date, Calendar.MONTH); }
/** * 2016-12-10 07:33:23, 则返回2016-12-10 07:00:00 */ public static Date beginOfHour(@NotNull final Date date) { return DateUtils.truncate(date, Calendar.HOUR_OF_DAY); }
/** * 2016-11-10 07:33:23, 则返回2016-1-1 00:00:00 */ public static Date beginOfYear(@NotNull final Date date) { return DateUtils.truncate(date, Calendar.YEAR); }
/** * 2016-12-10 07:33:23, 则返回2016-12-10 07:33:00 */ public static Date beginOfMinute(@NotNull final Date date) { return DateUtils.truncate(date, Calendar.MINUTE); }
/** * 2016-11-10 07:33:23, 则返回2016-1-1 00:00:00 */ public static Date beginOfYear(@NotNull final Date date) { return DateUtils.truncate(date, Calendar.YEAR); }
/** * 2016-12-10 07:33:23, 则返回2016-12-10 07:00:00 */ public static Date beginOfHour(@NotNull final Date date) { return DateUtils.truncate(date, Calendar.HOUR_OF_DAY); }
/** * 2016-11-10 07:33:23, 则返回2016-11-10 00:00:00 */ public static Date beginOfDate(@NotNull final Date date) { return DateUtils.truncate(date, Calendar.DATE); }
/** * 2016-12-10 07:33:23, 则返回2016-12-10 07:33:00 */ public static Date beginOfMinute(@NotNull final Date date) { return DateUtils.truncate(date, Calendar.MINUTE); }
/** * 2016-11-10 07:33:23, 则返回2016-11-1 00:00:00 */ public static Date beginOfMonth(@NotNull final Date date) { return DateUtils.truncate(date, Calendar.MONTH); }
/** * 2016-11-10 07:33:23, 则返回2016-11-10 00:00:00 */ public static Date beginOfDate(@NotNull final Date date) { return DateUtils.truncate(date, Calendar.DATE); }
/** * 2017-1-20 07:33:23, 则返回2017-1-16 00:00:00 */ public static Date beginOfWeek(@NotNull final Date date) { return DateUtils.truncate(DateUtil.subDays(date, DateUtil.getDayOfWeek(date) - 1), Calendar.DATE); }
/** * 2017-1-23 07:33:23, 则返回2017-1-22 00:00:00 */ public static Date nextWeek(@NotNull final Date date) { return DateUtils.truncate(DateUtil.addDays(date, 8 - DateUtil.getDayOfWeek(date)), Calendar.DATE); }
/** * 2017-1-23 07:33:23, 则返回2017-1-22 00:00:00 */ public static Date nextWeek(@NotNull final Date date) { return DateUtils.truncate(DateUtil.addDays(date, 8 - DateUtil.getDayOfWeek(date)), Calendar.DATE); }
/** * 2017-1-20 07:33:23, 则返回2017-1-16 00:00:00 */ public static Date beginOfWeek(@NotNull final Date date) { return DateUtils.truncate(DateUtil.subDays(date, DateUtil.getDayOfWeek(date) - 1), Calendar.DATE); }
assertEquals("Truncating "+ fdf.format(truncatedDate) +" as Date with CalendarField-value "+ calendarField +" must return itself", truncatedDate, DateUtils.truncate(truncatedDate, calendarField)); assertEquals(truncatedDate, DateUtils.truncate(lastTruncateDate, calendarField)); assertFalse(fdf.format(lastTruncateDate) +" is not an extreme when truncating as Date with CalendarField-value "+ calendarField, truncatedDate.equals(DateUtils.truncate(nextTruncateDate, calendarField))); assertEquals("Truncating "+ fdf.format(truncatedCalendar) +" as Calendar with CalendarField-value "+ calendarField +" must return itself", truncatedCalendar, DateUtils.truncate(truncatedCalendar, calendarField)); assertEquals(truncatedCalendar, DateUtils.truncate(lastTruncateCalendar, calendarField)); assertFalse(fdf.format(lastTruncateCalendar) +" is not an extreme when truncating as Calendar with CalendarField-value "+ calendarField, truncatedCalendar.equals(DateUtils.truncate(nextTruncateCalendar, calendarField))); assertEquals("Truncating "+ fdf.format(truncatedDate) +" as Date cast to Object with CalendarField-value "+ calendarField +" must return itself as Date", truncatedDate, DateUtils.truncate((Object) truncatedDate, calendarField)); assertEquals(truncatedDate, DateUtils.truncate((Object) lastTruncateDate, calendarField)); assertFalse(fdf.format(lastTruncateDate) +" is not an extreme when truncating as Date cast to Object with CalendarField-value "+ calendarField, truncatedDate.equals(DateUtils.truncate((Object) nextTruncateDate, calendarField))); assertEquals("Truncating "+ fdf.format(truncatedCalendar) +" as Calendar cast to Object with CalendarField-value "+ calendarField +" must return itself as Date", truncatedDate, DateUtils.truncate((Object) truncatedCalendar, calendarField)); assertEquals(truncatedDate, DateUtils.truncate((Object) lastTruncateCalendar, calendarField)); assertFalse(fdf.format(lastTruncateCalendar) +" is not an extreme when truncating as Calendar cast to Object with CalendarField-value "+ calendarField, truncatedDate.equals(DateUtils.truncate((Object) nextTruncateCalendar, calendarField)));
/** * This checks that this is a 7 divisble iterator of Calendar objects * that are dates (no time), and exactly 1 day spaced after each other * (in addition to the proper start and stop dates) */ private static void assertWeekIterator(final Iterator<?> it, final Calendar start, final Calendar end) { Calendar cal = (Calendar) it.next(); assertCalendarsEquals("", start, cal, 0); Calendar last = null; int count = 1; while (it.hasNext()) { //Check this is just a date (no time component) assertCalendarsEquals("", cal, DateUtils.truncate(cal, Calendar.DATE), 0); last = cal; cal = (Calendar) it.next(); count++; //Check that this is one day more than the last date last.add(Calendar.DATE, 1); assertCalendarsEquals("", last, cal, 0); } assertFalse("There were " + count + " days in this iterator", count % 7 != 0); assertCalendarsEquals("", end, cal, 0); }
final Calendar now = Calendar.getInstance(); for (int i = 0; i< 7; i++) { final Calendar today = DateUtils.truncate(now, Calendar.DATE); final Calendar sunday = DateUtils.truncate(now, Calendar.DATE); sunday.add(Calendar.DATE, 1 - sunday.get(Calendar.DAY_OF_WEEK)); final Calendar monday = DateUtils.truncate(now, Calendar.DATE); if (monday.get(Calendar.DAY_OF_WEEK) == 1) { monday.add(Calendar.DATE, 2 - monday.get(Calendar.DAY_OF_WEEK)); final Calendar centered = DateUtils.truncate(now, Calendar.DATE); centered.add(Calendar.DATE, -3);