Home > Software engineering >  Comparing java.utilDate with LocalDate in java
Comparing java.utilDate with LocalDate in java

Time:11-05

I have a method wherein have to check whether a LocalDate falls in between two java.util.Date values. there are methods after and before in java.util.Date and there are methods isAfter and isBefore in LocalDate.

The code snippet which i have is as :

 /**
 * checks if date passed falls between start & end date
 *
 * @param date
 * @param startDate
 * @param endDate
 * @return
 */
public static boolean isBetween(Date date, Date startDate, Date endDate) {
    return (startDate == null || date.after(startDate) || date.equals(startDate))
            && (endDate == null || date.before(endDate) || date.equals(endDate));
}

There is no method in the API to compare across..

CodePudding user response:

Convert LocalDate to String and convert this String to java.util.Date and you can use your method.

To convert LocalDate to String:

LocalDate localDate = LocalDate.now();                                    
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");          
String localDateToString = localDate.format(dateFormatter1);

And to convert String to Date:

SimpleDateFormat formatter=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); 
Date date1=formatter1.parse(sDate1);  

Once you've converted LocalDate to Date, you can compare dates using your method.

CodePudding user response:

You need to decide on many corner and edge cases. Here’s a shot.

public static boolean isBetween(LocalDate date, Date startDate, Date endDate) {
    ZoneId zone = ZoneId.systemDefault();
    // Is before start?
    if (startDate != null) {
        LocalDate startLocalDate = startDate.toInstant().atZone(zone).toLocalDate();
        if (date.isBefore(startLocalDate)) {
            return false;
        }
    }
    // Is after end?
    if (endDate != null) {
        LocalDate endLocalDate = endDate.toInstant().atZone(zone).toLocalDate();
        if (date.isAfter(endLocalDate)) {
            return false;
        }
    }
    // If we end up here, the date is between start and end inclusive
    return true;
}

I am assuming that the old-fashioned Date objects are to be interpreted in the default time zone of the JVM. On one hand this is standard, on the other hand the default time zone can be changed at any time, also from other programs running in the same JVM, so this is fragile. I am discarding the time of day part of the thus interpreted date. Whether the time is 00:00 or it’s 23:59:59.999, I deem the LocalDate inside the interval if the date agrees. You may want quite different behaviour.

If you wanted to take the time into account, you should probably convert everything to Instant or ZonedDateTime instead.

As you can see, mixing old-fashioned and modern classes leads to quite some complication. I am converting Date to LocalDate in order to take advantage of java.time, which in turn also gets more complicated since the Dates may be null.

CodePudding user response:

You will have to convert java.util.Date to LocalDate instances for comparison. Since you have to compare LocalDate with other dates, so losing the time part seems to be a logical compromise here.

public static boolean isBetween(LocalDate localDate, Date start, Date end){
    LocalDate startLocalDate = Instant.ofEpochMilli(start.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    LocalDate endLocalDate = Instant.ofEpochMilli(end.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    return (localDate.isEqual(startLocalDate) || localDate.isEqual(endLocalDate)) || (localDate.isAfter(startLocalDate) && localDate.isBefore(endLocalDate));
}

I have made the endDate as inclusive upper limit.

  • Related