
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to compare two dates in String format in Java?
The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string.
- One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object.
- To parse/convert a string as a Date object Instantiate this class by passing desired format string.
- Parse the date string using the parse() method.
- The util.Date class represents a specific instant time This class provides various methods such as before(), after() and, equals() to compare two dates
Example
Once you create date objects from strings you can compare them using either of these methods as shown below −
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Demo { public static void main(String args[])throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MM"); String dateStr1 = "2007-11-25"; String dateStr2 = "1999-9-12"; //Parsing the given String to Date object Date date1 = formatter.parse(dateStr1); Date date2 = formatter.parse(dateStr2); Boolean bool1 = date1.after(date2); Boolean bool2 = date1.before(date2); Boolean bool3 = date1.equals(date2); if(bool1){ System.out.println(dateStr1+" is after "+dateStr2); }else if(bool2){ System.out.println(dateStr1+" is before "+dateStr2); }else if(bool3){ System.out.println(dateStr1+" is equals to "+dateStr2); } } }
Output
2007-11-25 is after 1999-9-12
Parse() method of the LocalDate class
The parse() method of the LocalDate class accepts a String value representing a date and returns a LocalDate object.
Example
import java.time.LocalDate; public class Test { public static void main(String args[]){ String dateStr1 = "2007-11-25"; String dateStr2 = "1999-9-12"; LocalDate date1 = LocalDate.parse(dateStr1); LocalDate date2 = LocalDate.parse(dateStr1); Boolean bool1 = date1.isAfter(date2); Boolean bool2 = date1.isBefore(date2); Boolean bool3 = date1.isEqual(date2); if(bool1){ System.out.println(dateStr1+" is after "+dateStr2); }else if(bool2){ System.out.println(dateStr1+" is before "+dateStr2); }else if(bool3){ System.out.println(dateStr1+" is equal to "+dateStr2); } } }
Output
2007-11-25 is equal to 1999-9-12
- Related Articles
- How to compare two dates in Java?
- How to compare two dates along with time in Java?
- How to compare two Dates in C#?
- How to Compare two String in Java?
- Compare Dates in Java
- How to compare two dates with JavaScript?
- PHP program to compare two dates
- How to compare dates in JavaScript?
- How to compare Python string formatting: % with .format?
- What are various ways to compare dates in Java?
- How to compare String equality in Java?
- How to compare two arrays in Java?
- How to get string as date in MySQL with dates as dot format specifier?
- How to count days between two dates in Java
- How to format a date to String in Java?

Advertisements