From 7721acd0ef955dd0a3709cf3fa040a6a5d127e06 Mon Sep 17 00:00:00 2001 From: "Paul C. Benedict Jr" Date: Sat, 27 Feb 2010 03:29:43 +0000 Subject: [PATCH] LANG-594: Add truncatedEquals and truncatedCompareTo git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@916906 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/lang3/time/DateUtils.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/time/DateUtils.java b/src/main/java/org/apache/commons/lang3/time/DateUtils.java index b89ee50dd..f6c2adae0 100644 --- a/src/main/java/org/apache/commons/lang3/time/DateUtils.java +++ b/src/main/java/org/apache/commons/lang3/time/DateUtils.java @@ -48,6 +48,7 @@ * @author Gary Gregory * @author Phil Steitz * @author Robert Scholte + * @author Paul Benedict * @since 2.0 * @version $Id$ */ @@ -1693,6 +1694,80 @@ private static long getFragment(Calendar calendar, int fragment, int unit) { return result; } + /** + * Determines if two calendars are equal up to no more than the specified + * most significant field. + * + * @param cal1 the first calendar, not null + * @param cal2 the second calendar, not null + * @param field the field from Calendar + * @return true if equal; otherwise false + * @throws IllegalArgumentException if any argument is null + * @see #truncate(Calendar, int) + * @see #truncatedEquals(Date, Date, int) + * @since 3.0 + */ + public static boolean truncatedEquals(Calendar cal1, Calendar cal2, int field) { + return truncatedCompareTo(cal1, cal2, field) == 0; + } + + /** + * Determines if two dates are equal up to no more than the specified + * most significant field. + * + * @param date1 the first date, not null + * @param date2 the second date, not null + * @param field the field from Calendar + * @return true if equal; otherwise false + * @throws IllegalArgumentException if any argument is null + * @see #truncate(Date, int) + * @see #truncatedEquals(Calendar, Calendar, int) + * @since 3.0 + */ + public static boolean truncatedEquals(Date date1, Date date2, int field) { + return truncatedCompareTo(date1, date2, field) == 0; + } + + /** + * Determines how two calendars compare up to no more than the specified + * most significant field. + * + * @param cal1 the first calendar, not null + * @param cal2 the second calendar, not null + * @param field the field from 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 null + * @see #truncate(Calendar, int) + * @see #truncatedCompareTo(Date, Date, int) + * @since 3.0 + */ + public static int truncatedCompareTo(Calendar cal1, Calendar cal2, int field) { + Calendar truncatedCal1 = truncate(cal1, field); + Calendar truncatedCal2 = truncate(cal2, field); + return truncatedCal1.compareTo(truncatedCal2); + } + + /** + * Determines how two dates compare up to no more than the specified + * most significant field. + * + * @param date1 the first date, not null + * @param date2 the second date, not null + * @param field the field from Calendar + * @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 null + * @see #truncate(Calendar, int) + * @see #truncatedCompareTo(Date, Date, int) + * @since 3.0 + */ + public static int truncatedCompareTo(Date date1, Date date2, int field) { + Date truncatedDate1 = truncate(date1, field); + Date truncatedDate2 = truncate(date2, field); + return truncatedDate1.compareTo(truncatedDate2); + } + /** * Returns the number of millis of a datefield, if this is a constant value *