beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Date comparison in java: compare two dates of different formats

By Chaitanya Singh | Filed Under: Java Date

While developing an application there are certain scenarios where you may need to compare two dates which are in different format. Here I am sharing a code which compares two provided dates which can be in any format.

As you can see in the below example that we have created a method where you need to provide the from date format, to date format and from/to date.

package beginnersbook.com;
import java.text.ParseException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
class Details{
   public static void main(String args[])throws ParseException
   {
      Details obj = new Details();
      String str= obj.datecheckcmp("dd/MM/yyyy", "MM/dd/yyyy", "27/12/2013", "11/28/2013");
      System.out.println(str);
   }
   String datecheckcmp(String fromDateFormat, String toDateFormat,
String fromdate, String todate)throws ParseException
   {
      String CheckFormat = "dd-MMM-yyyy";
      String dateStringFrom;
      String dateStringTo;
      Date DF = new Date();
      Date DT = new Date();
      int flagtodate=0;
      int flagfromdate=0;
      try
      {
         //DateFormatdf = DateFormat.getDateInstance(DateFormat.SHORT);
         DateFormat FromDF = new SimpleDateFormat(fromDateFormat);
         FromDF.setLenient(false);  // this is important!
         Date FromDate = FromDF.parse(fromdate);
         dateStringFrom = new SimpleDateFormat(CheckFormat).format(FromDate);
         DateFormat FromDF1 = new SimpleDateFormat(CheckFormat);
         DF=FromDF1.parse(dateStringFrom);
         System.out.println("Date is ok = " + dateStringFrom);
      }
      catch (ParseException e)
      {
         flagfromdate = 1;
      }
      catch (IllegalArgumentException e)
      {
         flagfromdate = 1;
      }
      try
      {
         //DateFormatdf = DateFormat.getDateInstance(DateFormat.SHORT);
         DateFormat ToDF = new SimpleDateFormat(toDateFormat);
         ToDF.setLenient(false); // this is important!
         Date ToDate = ToDF.parse(todate);
         dateStringTo = new SimpleDateFormat(CheckFormat).format(ToDate);
         DateFormat ToDF1 = new SimpleDateFormat(CheckFormat);
         DT=ToDF1.parse(dateStringTo);
         System.out.println("Date is ok = " + dateStringTo); 
      }
      catch (ParseException e)
      {
         flagtodate=1;
      }
      catch (IllegalArgumentException e)
      {
         flagtodate=1;
      }
      if(flagfromdate == 0 &&flagtodate==0)
      {
         if(DF.equals(DT))
         {
    	    // if the date is same
            return "FromDate and ToDate are same"; 
         }
         else if(DF.before(DT))
         {
    	    //if the from date is before the to date
            return "FromDate is less than ToDate"; 
         }
         else
         {
            // if the date from is after the to date
           return "FromDate is greater than the ToDate"; 
         }
      }
   return "Error";
   }
}

Output:

Date is ok = 27-Dec-2013
Date is ok = 28-Nov-2013
FromDate is greater than the ToDate

Enjoyed this post? Try these related posts

  1. Day calculation from date
  2. Java – Get time in milliseconds using Date, Calendar and ZonedDateTime
  3. Java calendar class: add/subtract Year, months, days, hour, minutes
  4. Java date
  5. Java LocalDate – equals() method example
  6. Java – Date Format to display the Day of the week

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Date and Time

  • Java Date Time
  • Java LocalDate
  • Java LocalTime
  • Java LocalDateTime
  • Java ZonedDateTime
  • Java DateTimeFormatter
  • current date time

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2019 BeginnersBook . Privacy Policy . Sitemap