Date Program ISC 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.

Test your program with the following data and some random data:

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 45 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.io.*;
class FindDate{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("DAY NUMBER: ");
        int dayNum = Integer.parseInt(br.readLine());
        System.out.print("YEAR: ");
        int year = Integer.parseInt(br.readLine());
        System.out.print("DATE AFTER (N DAYS): ");
        int n = Integer.parseInt(br.readLine());
        if(dayNum < 1 || dayNum > 366){
            System.out.println("DAY NUMBER OUT OF RANGE.");
            return;
        }
        if(n < 1 || n > 100){
            System.out.println("DAY AFTER (N DAYS) OUT OF RANGE.");
            return;
        }
        if(year < 1000 || year > 9999){
            System.out.println("INVALID YEAR.");
            return;
        }
        int m[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if(isLeapYear(year))
            m[1] = 29;
        String mn[] = {"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};
        int i = 0;
        while(dayNum - m[i] > 0){
            dayNum -= m[i];
            i++;
        }
        System.out.print("DATE: " + dayNum);
        if(dayNum % 10 == 1 && dayNum > 11)
            System.out.print("ST ");
        else if(dayNum % 10 == 2 && dayNum > 12)
            System.out.print("ND ");
        else if(dayNum % 10 == 3 && dayNum > 13)
            System.out.print("RD ");
        else
            System.out.print("TH ");
        System.out.println(mn[i] + ", " + year);
        dayNum += n;
        while(i < 12 && dayNum - m[i] > 0){
            dayNum -= m[i];
            i++;
        }
        if(dayNum > 31){
            i = 0;
            while(dayNum - m[i] > 0 && i < 12){
                dayNum -= m[i];
                i++;
            }
        }
        System.out.print("DATE AFTER " + n + " DAYS: " + dayNum);
        if(dayNum % 10 == 1 && dayNum > 11)
            System.out.print("ST ");
        else if(dayNum % 10 == 2 && dayNum > 12)
            System.out.print("ND ");
        else if(dayNum % 10 == 3 && dayNum > 13)
            System.out.print("RD ");
        else
            System.out.print("TH ");
        System.out.println(mn[i] + ", " + year);
    }
    public static boolean isLeapYear(int y){
        if(y % 400 == 0)
            return true;
        if(y % 100 == 0)
            return false;
        if(y % 4 == 0)
            return true;
        return false;
    }
}

Leave a Reply

%d bloggers like this: