

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to compare dates if a date is after another date
To compare dates if a date is after another date, use the Calendar.after() method.
The Calendar.after() method returns whether this Calendar's time is after the time represented by the specified Object. First, let us set a date which is after (future date) the current date
Calendar date1 = Calendar.getInstance(); date1.set(Calendar.YEAR, 2040); date1.set(Calendar.MONTH, 10); date1.set(Calendar.DATE, 25);
Here is our current date
Calendar date = Calendar.getInstance();
Now, use the after() method to compare both the dates as shown in the following example
Example
import java.util.Calendar; public class Demo { public static void main(String[] args) { Calendar date1 = Calendar.getInstance(); date1.set(Calendar.YEAR, 2040); date1.set(Calendar.MONTH, 10); date1.set(Calendar.DATE, 25); java.util.Date dt = date1.getTime(); System.out.println("Date One = "+dt); Calendar date2 = Calendar.getInstance(); System.out.println("Date Two = " + (date2.get(Calendar.MONTH) + 1) + "-" + date2.get(Calendar.DATE) + "-" + date2.get(Calendar.YEAR)); if(date1.after(date2)) { System.out.println("Date One is after Date Two!"); } else { System.out.println("Date One is before Date Two!"); } } }
Output
Date One = Sun Nov 25 06:52:14 UTC 2040 Date Two = 11-23-2018 Date One is after Date Two!
- Related Questions & Answers
- Java Program to compare dates if a date is before another date
- How to see if a date is before or after another date in Java 8?
- Compare date time using after() method of Java Calendar
- Python Program to Check if a Date is Valid and Print the Incremented Date if it is
- Compare Dates in Java
- C Program to check if a date is valid or not
- Compare date strings in MySQL
- PHP program to compare two dates
- How to compare the first date and the last date from a MySQL column with date records?
- Compare date when the AdmissionDate is less than the current date in MySQL
- Java Program to convert a String to Date
- MySQL query to delete a DATE older than 30 days from another date?
- How to compare two JavaScript Date Objects?
- How to compare date strings in Python?
- MySQL date comparison to fetch dates between a given range?
Advertisements