java中如何比较两个日期?

发布于 2024-12-19 08:47:40 字数 406 浏览 1 评论 0原文

我正在尝试比较两个日期,我只想比较日期部分而不是时间部分,这就是我在程序中存储日期的方式:

Thu Jan 27 23:20:00 GMT 2011

我有一个:

ArrayList<Date> dateList;

并且我想使用

dateList.compares(newDate);
// if the answer was false which means I 
// have new date then add the newdate to my list

,但由于时间部分也涉及我无法得到正确的答案。 我该如何解决我的问题?

我不想使用 JODA-TIME

I'm trying to compare two dates together and I only want to compare the date part not the time part this is how I store date inside my program :

Thu Jan 27 23:20:00 GMT 2011

I have an:

ArrayList<Date> dateList;

and I want to use

dateList.compares(newDate);
// if the answer was false which means I 
// have new date then add the newdate to my list

but since the time part also is involved I can't get the proper answer.
How can I fix my problem?

I don't want use JODA-TIME

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北恋 2024-12-26 08:47:40

您可以像这样逐个比较值

d1.getDate().equals(d2.getDate()) &&
d1.getYear().equals(d2.getYear()) &&
d1.getMonth().equals(d2.getMonth())

或者

Date date1 = new Date(d1.getYear(), d1.getMonth(), d1.getDate());
Date date2 = new Date(d2.getYear(), d2.getMonth(), d2.getDate());
date1.compareTo(date2);

如果您使用 Date 类,请考虑使用 Calendar 类
这是最优雅的解决方案,使用日历和比较器来实现此

public class CalendarDateWithoutTimeComparator implements Comparator<Calendar> {

    public int compare(Calendar cal1, Calendar cal2) {
        if(cal1.get(Calendar.YEAR) != cal2.get(Calendar.YEAR)) {
            return cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
        } else if (cal1.get(Calendar.MONTH) != cal2.get(Calendar.MONTH)) {
            return cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
        }
        return cal1.get(Calendar.DAY_OF_MONTH) - cal2.get(Calendar.DAY_OF_MONTH);
    }
}

用法:

Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
// these calendars are equal

CalendarDateWithoutTimeComparator comparator = new CalendarDateWithoutTimeComparator();
System.out.println(comparator.compare(c1, c2));

List<Calendar> list = new ArrayList<Calendar>();
list.add(c1);
list.add(c2);

Collections.sort(list, comparator);

You can compare value by value like this

d1.getDate().equals(d2.getDate()) &&
d1.getYear().equals(d2.getYear()) &&
d1.getMonth().equals(d2.getMonth())

Or

Date date1 = new Date(d1.getYear(), d1.getMonth(), d1.getDate());
Date date2 = new Date(d2.getYear(), d2.getMonth(), d2.getDate());
date1.compareTo(date2);

If you work with Date class, consider using Calendar class instead
Here's the most elegant solution, using Calendar and Comparator for this

public class CalendarDateWithoutTimeComparator implements Comparator<Calendar> {

    public int compare(Calendar cal1, Calendar cal2) {
        if(cal1.get(Calendar.YEAR) != cal2.get(Calendar.YEAR)) {
            return cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
        } else if (cal1.get(Calendar.MONTH) != cal2.get(Calendar.MONTH)) {
            return cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
        }
        return cal1.get(Calendar.DAY_OF_MONTH) - cal2.get(Calendar.DAY_OF_MONTH);
    }
}

Usage:

Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
// these calendars are equal

CalendarDateWithoutTimeComparator comparator = new CalendarDateWithoutTimeComparator();
System.out.println(comparator.compare(c1, c2));

List<Calendar> list = new ArrayList<Calendar>();
list.add(c1);
list.add(c2);

Collections.sort(list, comparator);
负佳期 2024-12-26 08:47:40

为什么不使用compareTo()?

int java.util.Date#compareTo(Date anotherDate)

Why dont you use compareTo()?

int java.util.Date#compareTo(Date anotherDate)
巴黎盛开的樱花 2024-12-26 08:47:40
Date check_in = new Date(2010,7,31);
Date check_out = new Date(2011,5,22);

int result = check_out.compareTo(check_in); 

if(result<0)
{
  // check_out < check_in
}
else if(result>0)
{
 // check_out > check_in
}
else
{
// check_out == check_in
}       
Date check_in = new Date(2010,7,31);
Date check_out = new Date(2011,5,22);

int result = check_out.compareTo(check_in); 

if(result<0)
{
  // check_out < check_in
}
else if(result>0)
{
 // check_out > check_in
}
else
{
// check_out == check_in
}       
肤浅与狂妄 2024-12-26 08:47:40

您可以使用此

int compareDates(Calendar c1, Calendar c2) {
    if(c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR)){
        return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
    } else if(c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH)){
        return c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
    }
    return (c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH));
}

编辑:

Joda 时间或否此帖子包含日期比较的所有答案

如何比较两个没有时间部分的日期?

You can use this

int compareDates(Calendar c1, Calendar c2) {
    if(c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR)){
        return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
    } else if(c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH)){
        return c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
    }
    return (c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH));
}

EDIT:

Joda time or no this SO post has all answers for date comparison

How to compare two Dates without the time portion?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文