Compare time in java
Introduction: This article discusses how to compare time in java. Maximum of the time we need to examine the date and datetime items. Date comparisons are vital if you want to retrieve statistics from a database for a selected date and time or if you need to filter out the facts based totally on date and time.
Using the CompareTo() approach of the LocalTime elegance, evaluate times. The CompareTo() approach of the magnificence compares two LocalTime objects. The primary LocalTime item is the object to examine, and the second object is exceeded as a parameter to her CompareTo() approach to explore this LocalTime object.
Syntax of the compare time method in java: The syntax of the compare time in java is given below:
public int compareTo (LocalTime other)
Parameters of the compare time method in java: Accepts only a single parameter. LocalTime objects to compare. It must not be null.
Returns value of the compare time method in java: The CompareTo() technique returns three values based on evaluating objects.
- Returns a tremendous value if this LocalTime item is extra than the specified LocalTime item.
- Returns a negative value if this LocalTime object is less than the selected LocalTime object.
- Returns null if this LocalTime item is identical to the desired LocalTime object.
Example 1: Here, we give an example of the compareTo() method in java. The example is shown below:
import java.time.*;
public class CompareTime
{
public static void main(String[] args)
{
LT time1 = LocalTime.parse("05:50:59");
LT time2 = LocalTime.parse("12:10:09");
int value = time1.compareTo(time2);
System.out.println("Int Value:" + value);
if (value > 0)
System.out.println("LT1 is greater");
else if (value == 0)
System.out.println("LT1 is equal to" + " LT2");
else
System.out.println("LT2 is greater");
sc.close;
}
}
Output: We compile the above program and also run this program. The result is given below:
Int Value:1
LT2 is greater
Example 2: Here, we give another example of the compareTo() method in java. The example is given below:
package javaTpoint.JavaExample;
import java.time.*;
import java.util.Scanner;
public class CompareToExample2 {
public static void main(String[] args)
{
int hr1, hr2, min1, min2, sec1, sec2, yr1, yr2, month1, month2, day1, day2;
LocalDateTime date1, date2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Datetime for initializing the first LocaleDateTime object:");
System.out.println("Enter the year:");
yr1 = sc.nextInt();
System.out.println("Enter the month:");
month1 = sc.nextInt();
System.out.println("Enter the day:");
day1 = sc.nextInt();
System.out.println("Enter the hours:");
hr1 = sc.nextInt();
System.out.println("Enter the minutes:");
min1 = sc.nextInt();
System.out.println("Enter the seconds:");
sec1 = sc.nextInt();
date1 = LocalDateTime.of(yr1, month1, day1, hour1, min1, sec1);
System.out.println("Date1: " + date1);
System.out.println("Enter Datetime for initializing the second LocaleDateTime object:");
System.out.println("Enter the year:");
yr2 = sc.nextInt();
System.out.println("Enter the month:");
month2 = sc.nextInt();
System.out.println("Enter the day:");
day2 = sc.nextInt();
System.out.println("Enter the hours:");
hr2 = sc.nextInt();
System.out.println("Enter the minutes:");
min2 = sc.nextInt();
System.out.println("Enter the seconds:");
sec2 = sc.nextInt();
sc.close();
date2 = LocalDateTime.of(yr2, month2, day2, hour2, min2, sec2);
System.out.println("Date2: " + date2);
int returnVal = date1.compareTo(date2);
System.out.println("Value returned by comparTo() method:" + returnVal);
if (returnVal > 0) {
System.out.println("Date1 is greater than date2.");
}else if (returnVal == 0) {
System.out.println("Date1 is equal to date2.");
}else {
System.out.println("Date1 is smaller than date2.");
}
}
}
Output: We compile the above program and also run this program. The result is given below:
Enter Datetime for initializing the first LocaleDateTime object:
Enter the year: 2022
Enter the month: 03
Enter the day: 17
Enter the hours: 12
Enter the minutes: 05
Enter the seconds: 48
Date1: 2022-03-17T12:05:48
Enter Datetime for initializing the second LocaleDateTime object:
Enter the year: 2022
Enter the month: 03
Enter the day: 15
Enter the hours: 10
Enter the minutes: 00
Enter the seconds: 40
Date1: 2022-03-15T10:00:40
Value returned by comparTo() method: 1
Date1 is greater than date2.
So, here we give some examples of the compareTo() method and share the output of these. We also discuss the syntax, parameter, and return value of the compareTo() process in java.