欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java判断两个日期时间是否在同一天

程序员文章站 2024-03-20 21:04:58
...

Java判断两个日期时间是否在同一天

使用org.apache.commons.lang3.time.DateUtils包下的isSameDay方法

DateUtils.isSameDay(Date1, Date2);

源代码

 /**
     * <p>Checks if two date objects are on the same day ignoring time.</p>
     * <p>检查两个日期对象是否在同一天忽略时间</p>
     * 
     * <p>28 Mar 2002 13:45 and 28 Mar 2002 06:01 would return true.
     * 28 Mar 2002 13:45 and 12 Mar 2002 13:45 would return false.
     * </p>
     * 
     * @param date1  the first date, not altered, not null
     * @param date2  the second date, not altered, not null
     * @return true if they represent the same day 如果它们代表同一天,则为true
     * @throws IllegalArgumentException if either date is <code>null</code>
     * @since 2.1
     */
    public static boolean isSameDay(final Date date1, final Date date2) {
        if (date1 == null || date2 == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        final Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        final Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        return isSameDay(cal1, cal2);
    }
相关标签: 项目问题 java