How to compare objects of LocalDate and Calendar class?

Maulik Doshi :

I am trying to compare dates from two object having different types. Is there any way to convert Calendar object to LocalDater or vice-versa?

Thank you :)

public class ABC{

    public static void main(String args[]){
        Calendar c1= Calendar.getInstance();
        LocalDate c2= LocalDate.now();      
        System.out.println(c1.compareTo(c2));
    }
}
Olivier Grégoire :

You need to compare dates, so let's do just that.

Using this answer as reference:

Calendar calendar = Calendar.getInstance();
LocalDate localDate = LocalDate.now();

LocalDate calendarAsLocalDate = calendar.toInstant()
    .atZone(calendar.getTimeZone().toZoneId())
    .toLocalDate();

return calendarAsLocalDate.compareTo(localDate)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=462070&siteId=1