简体   繁体   中英

What is the best way to compare a String date and Date Object in java?

I am reading List of Person Object which has a String endDate. While writing an iterator I have a condition to return active Users for the company. Which means endDate should be before today. So when I do this:

   String date = person.getEndDate();
        Date endDate = null;
        Date today = new Date();
       SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
       endDate = sdf.parse(date);

        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(endDate);
        cal2.setTime(today);

        cal1.clear(Calendar.HOUR);
        cal1.clear(Calendar.MINUTE);
        cal1.clear(Calendar.SECOND);
        cal1.clear(Calendar.MILLISECOND);
        cal1.clear(Calendar.HOUR_OF_DAY);

        cal2.clear(Calendar.HOUR);
        cal2.clear(Calendar.MINUTE);
        cal2.clear(Calendar.SECOND);
        cal2.clear(Calendar.MILLISECOND);
        cal2.clear(Calendar.HOUR_OF_DAY);

                 if (cal1 != null && cal1.before(cal2)) {
                return person;
            }else{
                return null;
            }

This sets endDate to something like:Tue Jun 23 00:00:00 EDT 2015 But at the time of writing this today date becomes: Tue Jun 23 12:09:01 EDT 2015

When comparing cal1.before(cal2), this doesn't yield active or inactive user. What is the most effective way to compare in such a situation? Any suggestions?

Your date format is wrong.Refer DateFormat m is for minute M is for month and you have to format the current date as well to compare.

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date today = sdf.parse(sdf.format(new Date()));
Date date2 = sdf.parse("06/25/2015");
System.out.println(date2.after(today));

It seems like you want just the date from the entire Calendar object. Have you considered using the JodaTime library? It offers a lot of date-time manipulation options and also solves the problem of Thread Safety.

Using JodaTime , your code could be rewritten as follows:

The following are the imports that you might need:

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

Code:

        String date = person.getEndDate();
        DateTime endDate = null;
        DateTime today = new DateTime();
        DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy");
        endDate = dtf.parseDateTime(date);

        LocalDate cal1 = endDate.toLocalDate();
        LocalDate cal2 = today.toLocalDate();

        if (cal1 != null && cal1.isBefore(cal2)) {
            return person;
        } else {
            return null;
        }

Hope this helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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