Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date Comparison using Java [duplicate]

Tags:

java

date

I have two dates:

  1. toDate (user input in MM/dd/yyyy format)
  2. currentDate (obtained by new Date())

I need to compare the currentDate with toDate. I have to display a report only when the toDate is equal to or more than currentDate. How can I do that?

like image 995
minil Avatar asked May 11 '10 13:05

minil


People also ask

How can I compare two date values in Java?

In Java, two dates can be compared using the compareTo() method of Comparable interface. This method returns '0' if both the dates are equal, it returns a value "greater than 0" if date1 is after date2 and it returns a value "less than 0" if date1 is before date2.

How do you check if two dates are on the same day Java?

Core Java The class Date represents a specific instant in time, with millisecond precision. To find out if two Date objects contain the same day, we need to check if the Year-Month-Day is the same for both objects and discard the time aspect.

How do you check if a date is after another date in Java?

The after() method is used to check if a given date is after another given date. Return Value: true if and only if the instant represented by this Date object is strictly later than the instant represented by when; false otherwise.


1 Answers

It is easier to compare dates using the java.util.Calendar. Here is what you might do:

Calendar toDate = Calendar.getInstance(); Calendar nowDate = Calendar.getInstance(); toDate.set(<set-year>,<set-month>,<set-day>);   if(!toDate.before(nowDate)) {     //display your report } else {     // don't display the report } 
like image 73
Abdel Raoof Olakara Avatar answered Oct 05 '22 23:10

Abdel Raoof Olakara