
- 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
What are various ways to compare dates in Java?
Using the LocalDate class
The java.time.LocalDate class represents the local date i.e. the date without time zone, you can use this object instead of Date. This class provides various methods such as isBefore(), isAfter() and, isEqual() to compare two dates −
Example
import java.time.LocalDate; public class Sample { public static void main(String args[]) { LocalDate date1 = LocalDate.of(2007, 11, 25); LocalDate date2 = LocalDate.of(1999, 9, 12); Boolean bool1 = date1.isAfter(date2); Boolean bool2 = date1.isBefore(date2); Boolean bool3 = date1.isEqual(date2); if(bool1){ System.out.println(date1+" is after "+date2); }else if(bool2){ System.out.println(date1+" is before "+date2); }else if(bool3){ System.out.println(date1+" is equla to "+date2); } } }
Output
2007-11-25 is after 1999-09-12
Using the Date class
The java.util.Date class represents a specific instant time This class provides various methods such as before(), after() and, equals() to compare two dates −
Example
import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.util.Date; public class Sample { public static void main(String args[]) throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MM"); //Parsing the given String to Date object Date date1 = formatter.parse("2007-11-25"); Date date2 = formatter.parse("1999-9-12"); Boolean bool1 = date1.after(date2); Boolean bool2 = date1.before(date2); Boolean bool3 = date1.equals(date2); if(bool1){ System.out.println(date1+" is after "+date2); }else if(bool2){ System.out.println(date1+" is before "+date2); }else if(bool3){ System.out.println(date1+" is equals to "+date2); } } }
Output
Sun Jan 11 00:00:00 IST 2009 is after Thu Dec 09 00:00:00 IST 1999
Using the Calendar class
Similar to the Date class, the Calendar class provides various methods such as before(), after() and, equals() to compare two dates −
Example
import java.util.Calendar; public class Sample { public static void main(String args[]) { Calendar obj1 = Calendar.getInstance(); obj1.set(2007, 11, 25); Calendar obj2 = Calendar.getInstance(); obj2.set(1999, 9, 15); Boolean bool1 = obj1.after(obj2); Boolean bool2 = obj1.before(obj2); Boolean bool3 = obj1.equals(obj2); if(bool1){ System.out.println(obj1+" is after "+obj2); }else if(bool2){ System.out.println(obj1+" is before "+obj2); }else if(bool3){ System.out.println(obj1+" is equla to "+obj2); } } }
Output
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2007,MONTH=11,WEEK_OF_YEAR=45,WEEK_OF_MONTH=1,DAY_OF_MONTH=25,DAY_OF_YEAR=312,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=33,SECOND=47,MILLISECOND=366,ZONE_OFFSET=19800000,DST_OFFSET=0] is after java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1999,MONTH=9,WEEK_OF_YEAR=45,WEEK_OF_MONTH=1,DAY_OF_MONTH=15,DAY_OF_YEAR=312,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=33,SECOND=47,MILLISECOND=392,ZONE_OFFSET=19800000,DST_OFFSET=0]
- Related Articles
- What are various ways to compare time values in Java?
- Compare Dates in Java
- How to compare two dates in Java?
- How to compare two dates in String format in Java?
- How to compare two dates along with time in Java?
- How to compare dates in JavaScript?
- How to compare two Dates in C#?
- What are the various ways to save the environment from excessive plastic wastes?
- PHP program to compare two dates
- Java Program to compare dates if a date is after another date
- Java Program to compare dates if a date is before another date
- How to compare two dates with JavaScript?
- Describe the various ways by which seeds are dispersed.
- Various ways to define a variable in JavaScript
- How do we compare Python Dates?
