How to Compare Dates in Java | How to Compare Time in Java

How to compare Dates and Time in Java?

Write a java program to read the time intervals (HH: MM) and to compare system time if the system time between your time intervals prints correct time and exit else try again to repute the same thing. By using String Tokenizer class.

We will see the source code and output of:

  • How to compare two dates in java
  • How to compare two times in java

Description:

Initially take the input interval of time in hours, minutes, and seconds. The system time can be read as using the object of the class GregorianCalendar.  This GregorianCalendar class contains a method by name get which returns a required value from the hour, minutes, and seconds of the system time.

Calendar class contains variables hour, minute, second which gives the system time through the object of the class GregorianCalendar.

How to Compare Time in Java

In this program, we will compare two times in java. We will take the input time from the user and then compare time with system time and print the difference between the two times.

Example:

import java.io.*;
import java.util.*;
import java.text.*;
public class Time
{
public static void main(String ar[])
{
int HOUR,SECOND,MINUTE;
Scanner s=new Scanner(System.in);
System.out.println("Enter seconds : ");
int a=s.nextInt();
System.out.println("Enter Minutes : ");
int b=s.nextInt();
System.out.println("Enter Hours : ");
int c=s.nextInt();
GregorianCalendar date=new GregorianCalendar();
int second=date.get(Calendar.SECOND);
int minute=date.get(Calendar.MINUTE);
int hour=date.get(Calendar.HOUR);
if(second >a)
SECOND=second-a;
else
SECOND=a-second;
if(minute >b)
MINUTE=minute-b;
else
MINUTE=b-minute;
if(hour>c)
HOUR=hour-c;
else
HOUR=c-hour;
System.out.println("Difference between two times is "+HOUR+":"+MINUTE+":"+SECOND);
} }
Output: Time Comparison
How to compare dates in java

How to Compare Date in Java?

In this program, we will learn to compare dates in java. We will enter two dates and then we will find the difference between the dates in java.

Example:

// Java program to compare two Dates

import java.io.*;
import java.util.*;
import java.text.*;
import java.time.*;

public class Dates { 
   public static void main(String[] args) 
   { 

      // Create LocalDate objects with dates 
      LocalDate d1 = LocalDate.of(2020, 02, 10); 
      LocalDate d2 = LocalDate.of(2021, 01, 20); 

      // Printing date date1 and date2 
      System.out.println("Date1: " + d1); 
      System.out.println("Date2: " + d2); 

      // Here we are comparing two date, date1 and date2
      if (d1.isAfter(d2)) { 

         // Print which date is first ( date1 or date2)  
         System.out.println("Date1 is after Date2"); 
      } 

      else if (d1.isBefore(d2)) { 

         // Compare Date1 < Date2 then print
         System.out.println("Date1 is before Date2"); 
      } 

      else if (d1.isEqual(d2)) { 

         // If both date are equal
         System.out.println("Date1 is equal to Date2"); 
      } 
   } 
} 
Output: Date Comparison
How to Compare Date in Java?

Recommended Post:

Find the solution to the salesforce Question and many more

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment