简体   繁体   English

如何比较java中的两个日期?

[英]how to compare two dates in java?

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 我不想使用JODA-TIME

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 如果您使用Date类,请考虑使用Calendar类。这是最优雅的解决方案,使用Calendar和Comparator

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);

为什么不使用compareTo()?

int java.util.Date#compareTo(Date anotherDate)
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
}       

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 Joda时间或没有这个SO帖子有日期比较的所有答案

How to compare two Dates without the time portion? 如何在没有时间部分的情况下比较两个日期?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM