
- 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 along with time in Java?
The java.time.LocalDateTime class represents the local date and time 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.LocalDateTime; public class Test { public static void main(String args[]) { LocalDateTime dateTime1 = LocalDateTime.of(2007, 11, 25, 10, 15, 45); LocalDateTime dateTime2 = LocalDateTime.of(1999, 9, 12, 07, 25, 55); Boolean bool1 = dateTime1.isAfter(dateTime2); Boolean bool2 = dateTime1.isBefore(dateTime2); Boolean bool3 = dateTime1.isEqual(dateTime2); if(bool1){ System.out.println(dateTime1+" is after "+dateTime2); }else if(bool2){ System.out.println(dateTime1+" is before "+dateTime2); }else if(bool3){ System.out.println(dateTime1+" is equla to "+dateTime2); } } }
Output
2007-11-25T10:15:45 is after 1999-09-12T07:25:55
Example
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class CreateDateTime { public static void main(String args[]) throws ParseException { String dateTimeStr1 = "26-09-1989 8:27:45"; String dateTimeStr2 = "12-11-2010 2:30:12"; //Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:SS"); Date dateTime1 = formatter.parse(dateTimeStr1); Date dateTime2 = formatter.parse(dateTimeStr2); Boolean bool1 = dateTime1.after(dateTime2); Boolean bool2 = dateTime1.before(dateTime2); Boolean bool3 = dateTime1.equals(dateTime2); if(bool1){ System.out.println(dateTimeStr1+" is after "+dateTimeStr2); }else if(bool2){ System.out.println(dateTimeStr1+" is before "+dateTimeStr2); }else if(bool3){ System.out.println(dateTimeStr1+" is equla to "+dateTimeStr2); } } }
Output
26-09-1989 8:27:45 is before 12-11-2010 2:30:12
- Related Articles
- How to compare two dates in Java?
- How to compare two dates with JavaScript?
- How to compare two dates in String format in Java?
- How to compare two Dates in C#?
- Compare Dates in Java
- PHP program to compare two dates
- How to compare dates in JavaScript?
- What are various ways to compare dates in Java?
- How to Compare two String in Java?
- How to compare two arrays in Java?
- How to create a vector with dates between two dates in R?
- How to calculate time difference between two times or dates?
- How to count days between two dates in Java
- How do we compare Python Dates?
- How to compare two ArrayList for equality in Java?

Advertisements