Comparing dates in Java

2019-06-11 12:53发布

I already read date functions but i cant think a best way to solve my problem.

I have a couple of dates from database which is String and i want to compare it to may current date. I am using the compareTo, but there is a problem using this function i guess it is because of i was comparing strings.

This is my function:

public int dateCompare(String today, String date2){
    return today.compareTo(date2);
}

And when i use it in sample dates:

dateCompare("04/19/2013","04/18/2013");

it returns 1, and when i change the value of first parameter to "04/20/2013" it still returns 1.

Please HELP...

3条回答
ら.Afraid
2楼-- · 2019-06-11 13:02

This is the quikest i have used....

Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
查看更多
爷、活的狠高调
3楼-- · 2019-06-11 13:04

Java has a Date object. You should be using that instead.

import java.util.Calendar;
import java.util.Date;

public class CompareDates {

    public static void main(String[] args) {
        // Create a calendar, this will default to today
        Calendar cal = Calendar.getInstance();
        // Subtract 1 day
        cal.add(Calendar.DATE, -1);
        // Compare the result (1)
        System.out.println(dateCompare(new Date(), cal.getTime()));
        // Add 2 days
        cal.add(Calendar.DATE, 2);
        // Compare the result (-1)
        System.out.println(dateCompare(new Date(), cal.getTime()));            
    }

    public static int dateCompare(Date today, Date date2) {
        System.out.println("Compare " + today + " with " + date2);
        return today.compareTo(date2);
    }
}

You could also just make use of the Date API and use before and after...

Date now = new Date();

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
System.out.println(now + " isBefore " + cal.getTime() + " = " + now.before(cal.getTime()));
System.out.println(now + " isAfter " + cal.getTime() + " = " + now.after(cal.getTime()));
cal.add(Calendar.DATE, 2);
System.out.println(now + " isBefore " + cal.getTime() + " = " + now.before(cal.getTime()));
System.out.println(now + " isAfter " + cal.getTime() + " = " + now.after(cal.getTime()));
查看更多
ら.Afraid
4楼-- · 2019-06-11 13:21

Fault is in the approach,

public int dateCompare(String today, String date2){
    return today.compareTo(date2);
}

You are considering this as dateComparison, but look at the method arguments, String, String.

So you are actually comparing two string.

You should firstly be converting those Strings to date...

Like,

public Date parseDate(String dateStr) {
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    return df.parse (dateStr);
}

public int dateCompare(String today, String date2){
    return (parseDate(today)).compareTo(parseDate(date2));
}

EDIT :

Modified for multiple formats:

public Date parseDate(String dateStr, String parsingFormat) throws SomeParsingException {
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    return df.parse (dateStr);
}

public Date parseDate (String dateStr) throws SomeException  {
    Date d1 = null;
    try {
        d1 = parseDate(dateStr, "MM/dd/yyyy");
    } catch (SomeParsingException  ex) {
        try {
           d1 = parseDate(dateStr, "MM-dd-yyyy");

        } catch (SomeParsingException  ex) {
             try {
                 d1 = parseDate(dateStr, "MM.dd.yyyy");
             } catch(SomeParsingException  ex) {
                 throw SomeException ("Unparseable date");
             }
        }
    }
   return d1;
}
查看更多
登录 后发表回答