BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Date comparison in java: compare two dates of different formats

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

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

Top Related Articles:

  1. Convert String to date in Java
  2. Date validation in java
  3. Compare two dates with each other in Java
  4. Java 8 – Calculate days between two dates
  5. Java Access Modifiers – Public, Private, Protected & Default

Tags: Java-Date

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap