Skip to main content

Java Program to accept two dates and display the difference between them

The following code is to get the difference between the two valid dates in milliseconds, seconds, minutes, hours and days.
Code:-

import java.util.*;
public class DateDiff{
    Scanner scan=new Scanner(System.in);
    int year, month, date;
    public void input(){
        int year, month, date;
        try{
            System.out.println("Enter 1st Year in \"yyyy\" format: ");
            this.year=scan.nextInt();
            System.out.println("Enter 1st Month in \"mm\" format: ");
            this.month=scan.nextInt();
            System.out.println("Enter 1st Date in \"dd\" format: ");
            this.date=scan.nextInt();
            System.out.println("\n\nEnter 2nd Year in \"yyyy\" format: ");
            year=scan.nextInt();
            System.out.println("Enter 2nd Month in \"mm\" format: ");
            month=scan.nextInt();
            System.out.println("Enter 2nd Date in \"dd\" format: ");
            date=scan.nextInt();
            difference(year, month, date);
        }
        catch(NumberFormatException e){
            System.out.println("Error Code: "+e);
            System.exit(0);
        }
    }

    public void difference(int year, int month, int date){
        Calendar cal1=Calendar.getInstance();
        Calendar cal2=Calendar.getInstance();
        cal2.set(year, month, date);
        cal1.set(this.year, this.month, this.date);
        long mill1=cal1.getTimeInMillis();
        long mill2=cal2.getTimeInMillis();
        long diff=mill2-mill1;
        long sec=diff/1000;
        long min=diff/(60*1000);
        long hour=diff/(60*60*1000);
        long day=diff/(24*60*60*1000);
        System.out.println("\n\nDifference between \""+this.year+"/"+this.month+"/"+this.date+"\" and \""+year+"/"+month+"/"+date+"\" is:- ");
        System.out.println("Time in Milliseconds: "+diff+" millisecs.");
        System.out.println("Time in Seconds: "+sec+" secs.");
        System.out.println("Time in Minutes: "+min+" mins.");
        System.out.println("Time in Hours: "+hour+" hrs.");
        System.out.println("Time in Days: "+day+" days.");
    }

    public static void main(String[] args){
        DateDiff date=new DateDiff();
        date.input();
    }
}

Output:-
Enter 1st Year in "yyyy" format:
1997
Enter 1st Month in "mm" format:
12
Enter 1st Date in "dd" format:
08


Enter 2nd Year in "yyyy" format:
2013
Enter 2nd Month in "mm" format:
03
Enter 2nd Date in "dd" format:
28


Difference between "1997/12/8" and "2013/3/28" is:-
Time in Milliseconds: 482889600000 millisecs.
Time in Seconds: 482889600 secs.
Time in Minutes: 8048160 mins.
Time in Hours: 134136 hrs.
Time in Days: 5589 days.

Comments

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Java Program to calculate the Strike Rate of a Cricket Batsman

import java.io.*; import java.util.*; public class StrikeRate{     Scanner scan=new Scanner(System.in);     int ballsFaced, runs;     double strikeRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Faced: ");             ballsFaced=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0); ...

WP similar or related posts widget without using any plugin

Hi guys, Recently I was working on some WP website and my client told me that he required a widget for displaying related/similar posts on the single post page. But as his website was already using many plugins, even for some pretty small tasks like this one, I decided not to use another WP plugin (plugins are not good for your WP websites, we will discuss about that on some other post.) I am not explaining the code as it is pretty simple if you are familiar with WP classes. But please let me know if you have any questions related to the PHP code posted below in the comments section or even much better, on Gist. You can add the following code directly in your child theme's functions.php file or you can create a separate file and include this at the bottom of functions.php file. <?php class similar_posts_widget extends WP_Widget {     function __construct()     {         parent::__construct('similar_posts_...