How to compare dates in java algorithm with source code

How to compare dates in java algorithm with source code

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

Description:

Intially 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.

Source code :

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);

            }     }

Expected Output :

Enter seconds : 02

Enter Minutes :10

Enter Hour :8

Difference between two times is

1 :13 :40

How to compare dates in java|algorithm with source code

How to compare dates in java algorithm with source code

https://en.wikipedia.org/wiki/Java_(programming_language)

Leave a Comment

Your email address will not be published. Required fields are marked *