Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a date is greater than another in Java? [duplicate]

Tags:

java

date

I am working on a Java application which generates a report for a duration entered by a user in the command line. The user needs to enter the dates in the following format: dd-MM-yyyy

> java report startDate endDate

Example:

java report 01-01-2013 31-03-2013

In the code I save the dates in two strings. I have to make sure that the start date entered by the user should be a date earlier than the end-date. Is there an built-in function which can help me achieve this by passing these two strings to it?

like image 588
Manas Avatar asked Oct 01 '13 07:10

Manas


People also ask

How do you check if a date is greater than another date 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 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.

How can I compare two dates?

For comparing the two dates, we have used the compareTo() method. If both dates are equal it prints Both dates are equal. If date1 is greater than date2, it prints Date 1 comes after Date 2. If date1 is smaller than date2, it prints Date 1 comes after Date 2.

How do you validate a date range in Java?

SimpleDateFormat df = new SimpleDateFormat("dd:MM:yyyy"); df. setLenient(false); Date date = df. parse(dateRelease); Then it will throw ParseException when the date is not in a valid range.


2 Answers

You can use Date.before() or Date.after() or Date.equals() for date comparison.

Taken from here:

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date;  public class DateDiff {      public static void main( String[] args )     {         compareDates("2017-01-13 00:00:00", "2017-01-14 00:00:00");// output will be Date1 is before Date2         compareDates("2017-01-13 00:00:00", "2017-01-12 00:00:00");//output will be Date1 is after Date2         compareDates("2017-01-13 00:00:00", "2017-01-13 10:20:30");//output will be Date1 is before Date2 because date2 is ahead of date 1 by 10:20:30 hours         compareDates("2017-01-13 00:00:00", "2017-01-13 00:00:00");//output will be Date1 is equal Date2 because both date and time are equal     }      public static void compareDates(String d1,String d2)     {         try{             // If you already have date objects then skip 1              //1             // Create 2 dates starts             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");             Date date1 = sdf.parse(d1);             Date date2 = sdf.parse(d2);              System.out.println("Date1"+sdf.format(date1));             System.out.println("Date2"+sdf.format(date2));System.out.println();              // Create 2 dates ends             //1              // Date object is having 3 methods namely after,before and equals for comparing             // after() will return true if and only if date1 is after date 2             if(date1.after(date2)){                 System.out.println("Date1 is after Date2");             }             // before() will return true if and only if date1 is before date2             if(date1.before(date2)){                 System.out.println("Date1 is before Date2");             }              //equals() returns true if both the dates are equal             if(date1.equals(date2)){                 System.out.println("Date1 is equal Date2");             }              System.out.println();         }         catch(ParseException ex){             ex.printStackTrace();         }     }      public static void compareDates(Date date1,Date date2)     {         // if you already have date objects then skip 1         //1          //1          //date object is having 3 methods namely after,before and equals for comparing         //after() will return true if and only if date1 is after date 2         if(date1.after(date2)){             System.out.println("Date1 is after Date2");         }          //before() will return true if and only if date1 is before date2         if(date1.before(date2)){             System.out.println("Date1 is before Date2");         }          //equals() returns true if both the dates are equal         if(date1.equals(date2)){             System.out.println("Date1 is equal Date2");         }          System.out.println();     } } 
like image 183
SpringLearner Avatar answered Sep 19 '22 19:09

SpringLearner


Parse the string into date, then compare using compareTo, before or after

Date d = new Date(); d.compareTo(anotherDate) 

i.e

Date date1 = new SimpleDateFormat("MM/dd/yyyy").parse(date1string) Date date2 = new SimpleDateFormat("MM/dd/yyyy").parse(date2string)  date1.compareTo(date2); 

Copying the comment provided below by @MuhammadSaqib to complete this answer.

Returns the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument, and a value greater than 0 if this Date is after the Date argument. and NullPointerException - if anotherDate is null.

javadoc for compareTo http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#compareTo(java.util.Date)

like image 23
Jayaram Avatar answered Sep 22 '22 19:09

Jayaram