Future Date Java Program | ISC Computer Science 2019 Practical

Design a program to accept a day number (between 1 and 366), year (in 4 digits) from the user to generate and display the corresponding date. Also, accept ‘N’ (1 ≤ N ≤ 100) from the user to compute and display the future date corresponding to ‘N’ days after the generated date. Display an error message if the value of the day number, year and N are not within the limit or not according to the condition specified.

Example 1
INPUT:
DAY NUMBER: 255
YEAR: 2018
DATE AFTER (N DAYS): 22
OUTPUT:
DATE: 12TH SEPTEMBER, 2018
DATE AFTER 22 DAYS: 4TH OCTOBER, 2018

Example 2
INPUT:
DAY NUMBER: 360
YEAR: 2018
DATE AFTER (N DAYS): 45
OUTPUT:
DATE: 26TH DECEMBER, 2018
DATE AFTER 22 DAYS: 9TH FEBRUARY, 2019

Example 3
INPUT:
DAY NUMBER: 500
YEAR: 2018
DATE AFTER (N DAYS): 33
OUTPUT:
DAY NUMBER OUT OF RANGE

Example 4
INPUT:
DAY NUMBER: 150
YEAR: 2018
DATE AFTER (N DAYS): 330
OUTPUT:
DATE AFTER (N DAYS) OUT OF RANGE

import java.util.Scanner;
class FindDate{
    static int i = 1;
    static int dayNum = 0;
    static int year = 0;
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("DAY NUMBER: ");
        dayNum = Integer.parseInt(in.nextLine());
        System.out.print("YEAR: ");
        year = Integer.parseInt(in.nextLine());
        System.out.print("DATE AFTER (N DAYS): ");
        int n = Integer.parseInt(in.nextLine());
        if((dayNum < 1 || dayNum > 366) || dayNum == 366 && !isLeap(year)){
            System.out.println("DAY NUMBER OUT OF RANGE");
            return;
        }
        if(n < 1 || n > 100){
            System.out.println("DATE AFTER (N DAYS) OUT OF RANGE");
            return;
        }
        System.out.print("DATE: ");
        displayDate();
        dayNum += n;
        System.out.print("DATE AFTER (N DAYS): ");
        displayDate();
    }
    public static boolean isLeap(int y){
        if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
            return true;
        return false;
    }
    public static void displayDate(){
        String month[] = {"", "JANURAY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};
        int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if(isLeap(year))
            days[2] = 29;
        while(dayNum > days[i]){
            dayNum -= days[i];
            i++;
            if(i > 12){
                i = 1;
                year++;
                if(isLeap(year))
                    days[2] = 29;
                else
                    days[2] = 28;
            }
        }
        String suffix = "TH";
        if(dayNum < 4 || dayNum > 20){
            int lastDigit = dayNum % 10;
            switch(lastDigit){
            case 1:
                suffix = "ST";
                break;
            case 2:
                suffix = "ND";
                break;
            case 3:
                suffix = "RD";
                break;
            }
        }
        System.out.println(dayNum + suffix + " " + month[i] + ", " + year);
    }
}

Leave a Reply

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