Introduction
- In this blog, I am going to explain the program for the date comparison program in Java
Software Requirements
Program
- import java.util.Date;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- public class DateComparison {
- private static void DateComparison() {
- Date comparetodaydate = new Date();
- Date comparedate = (Date) comparetodaydate.clone();
- Date comparedate1970 = new Date(24 L * 60 L * 60 L * 1000 L);
- System.out.println("Comparison of two dates by using the equals() method:");
- System.out.println();
- System.out.println("Today's date:" + comparetodaydate);
- System.out.println("Compairing date:" + comparedate);
- if (comparetodaydate.equals(comparedate)) {
- System.out.println("The two dates are equal");
- } else {
- System.out.println("The two dates are not equal");
- }
- System.out.println();
- System.out.println("Comparison of two dates by using the equals() method:");
- System.out.println();
- System.out.println("Today's date:" + comparetodaydate);
- System.out.println("Compairing date:" + comparedate1970);
- if (comparetodaydate.equals(comparedate1970)) {
- System.out.println("The two dates are equal");
- } else {
- System.out.println("The two dates are not equal");
- }
- System.out.println();
- System.out.println("Comparison of two dates by using the before() method:");
- System.out.println();
- System.out.println("Today's date:" + comparetodaydate);
- System.out.println("Compairing date:" + comparedate1970);
- if (comparetodaydate.before(comparedate1970)) {
- System.out.println("Today's date comes before the compairing date");
- } else {
- System.out.println("Today's date does not come before the compairing date");
- }
- System.out.println();
- System.out.println("Comparison of two dates by using the after() method::");
- System.out.println();
- System.out.println("Today's date:" + comparetodaydate);
- System.out.println("Compairing date:" + comparedate1970);
- if (comparetodaydate.after(comparedate1970)) {
- System.out.println("Today's date comes after the compairing date");
- } else {
- System.out.println("Today's date does not come after the compairing date");
- System.out.println();
- }
- System.out.println();
- }
- public static void main(String[] args) {
- System.out.println();
- DateComparison();
- }
- }