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 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
Recommended Post:
- How to compare dates in java|algorithm with source code
- Java roll dice 10000 times with algorithm and source code
- Write a Java program that displays the number of characters, lines, and words in a text
- Write a Java program that reads a file and displays the file on the screen with a line number before each line
- Write a Java program that reads a file name from the user, then displays information about whether the file exists, readable, writable, type of file, and the length of the file in bytes
- Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text
- Write a Java program for sorting a given list of names in ascending order
- Write a java program to Checks whether a given string is a palindrome or not
- Write a java program to perform multiplication of two matrices
- write a java program that prints the Fibonacci series for a given number.
- Write a Java program that finds the factorial of a number
- Write a Java program that finds prime numbers between 1 to n
- Write a Java program that prints all real and imaginary solutions to the quadratic equation
- Odd and Even number in java | Algorithm
- Even number in java | algorithm with source code
- Java greater number using a loop
- Java Area of Rectangle
- What is a polygon? | Area of Triangle in java
- To calculate the Area of Circle in the java program
- Java Addition through user input
- Addition program in java with source code
Find the solution to the salesforce Question and many more