- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compare date time using after() method of Java Calendar
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 afterDate = Calendar.getInstance(); afterDate.set(Calendar.YEAR, 2025); afterDate.set(Calendar.MONTH, 05); afterDate.set(Calendar.DATE, 30);
Here is our date, which is 11-22-2018
Calendar currentDate = 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 afterDate = Calendar.getInstance(); afterDate.set(Calendar.YEAR, 2025); afterDate.set(Calendar.MONTH, 05); afterDate.set(Calendar.DATE, 30); java.util.Date dt = afterDate.getTime(); System.out.println("Future Date = "+dt); Calendar currentDate = Calendar.getInstance(); System.out.println("Current date = " + (currentDate.get(Calendar.MONTH) + 1) + "-" + currentDate.get(Calendar.DATE) + "-" + currentDate.get(Calendar.YEAR)); System.out.println("Date "+dt+ " is after current date? = " + afterDate.after(currentDate)); } }
Output
Future Date = Mon Jun 30 12:36:27 UTC 2025 Current date = 11-22-2018 Date Mon Jun 30 12:36:27 UTC 2025 is after current date? = true
- Related Articles
- Compare date time using before() method of Java Calendar
- Set the Date and Time with Gregorian Calendar in Java
- Get time in milliseconds using Java Calendar
- Increment a Date using the Java Calendar Class
- Java Program to compare dates if a date is after another date
- Java Program to add days to current date using Java Calendar
- Create a Date object using the Calendar class in Java
- Java Program to decrement a Date using the Calendar Class
- Display Month of Year using Java Calendar
- Java Program to compare string using compareTo() method
- Display Day Name of Week using Java Calendar
- Java Integer compare() method
- Display complete date and time information using Formatter in Java
- Get week of month and year using Java Calendar
- How to compare only date part without comparing time in JavaScript?

Advertisements