Tuesday , 10 December 2019
Home » Class 12 » Java Program for finding Difference between 2 Dates

Java Program for finding Difference between 2 Dates

Question:

Write a program to accept 2 dates in the string format dd/mm/yyyy and find the difference in days between the 2 dates.

Example:    

INPUT:

Date 1  : 20/12/2012

Date 2  : 11/02/2013

OUTPUT:  Difference = 54 days

The program should include the part for validating the inputs namely the date and the day on 1st January of that year.

Programming Code:

/**
* The class Date_Difference inputs 2 dates and finds the difference between them
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class Date_Difference
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

//function for checking for Leap Year

int isLeap(int y)
    {
        if((y%400==0) || ((y%100!=0)&&(y%4==0)))
            return 29;
        else
            return 28;
    }

//function for checking date validation

boolean dateValidate(int d, int m, int y)
    {
        month[2]=isLeap(y);
        if(m<1 || m>12 || d<1 || d>month[m] || y<1000 || y>9999)
            return false;
        else
            return true;
    }

//function for finding day number from year = 1 till the inputted year

int dayno(int d, int m, int y)
    {
        int dn=0;
        month[2]=isLeap(y);
        for(int i=1;i

Check Also

Infix Postfix Prefix Conversion – Correct Method (ISC Computer Science)

In this video we discuss about the correct way of converting an Infix Expression to Postfix and Prefix.

Leave a Reply

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