DATE
PROGRAMS
PROGRAM 16
Design a class in java to accept a date in dd/mm/yyyy format. Check for the
validity of the date and display an appropriate message. Year lying between
1900-3000 (both inclusive) will be valid.
Sample input: 17/07/2024
Sample output: Valid date
Sample input: 29/02/2022
Sample output: Not a valid date
ALGORITHM
Step 1:- Start
Step 2:- Display a message to enter a date in "dd/mm/yyyy" format.
Step 3:- Accept the date as a string input.
Step 4:- Extract the day (dd), month (mm), and year (y) from the string using
substring().
Step 5:- Initialize a boolean flag f to false.
Step 6:- Check if the year y is between 1900 and 3000:
Step 6.1:- If y is within range, proceed with validation:
Step 6.2:- If the month is February (2) and the year is a leap year (divisible by 4
or 400), check if dd == 29 and set f = true.
Step 6.3:- If the month is February (2) in a non-leap year, ensure dd <= 28 and
set f = true.
Step 6.4:- If the month is January, March, May, July, August, October, or
December, ensure dd <= 31 and set f = true.
Step 6.5:- If the month is April, June, September, or November, ensure dd <= 30
and set f = true.
Step 7:- If f is true, print "It is a valid date.", else print "It is an invalid date.".
Step 8:- If the year is out of range, print "Invalid year.".
Step 9:- End.
CODE:
METHOD/FunCTIOn DESCRIPTIOn:
Function Name Access Specifier Return Type Description
main(String args[]) Public void Reads user input,
processes the date and
determines its validity.
VARIAbLE DESCRIPTIOn:
Variable Name Datatype Description
d String Stores the input date.
dd int Extracted day from the
input date.
mm int Extracted month from the
input date.
y int Extracted year from the
input date.
f boolean Flag variable to check the
validity of the date.
OuTPuT :
PROGRAM 17
Design a class in java to input a date and check whether it is a palindromic
date.
Sample input:
Enter a date: 10/02/2001
Sample output:
The date is a palindromic date
ALGORITHM
Step 1:- Start
Step 2:- Display a message prompting the user to enter a date in dd/mm/yyyy
format.
Step 3:- Accept the date input as a string.
Step 4:- Split the date string into three parts using the "/" delimiter.
Step 5:- Concatenate the day, month, and year into a single string.
Step 6:- Initialize an empty string dateRev to store the reversed date.
Step 7:- Loop through the original date string in reverse order and store the
characters in dateRev.
Step 8:- Compare date and dateRev:
Step 8.1:- If both are equal, print "It is a Palindromic date".
Step 8.2:- Otherwise, print "It is not a Palindromic date".
Step 9:- End.
CODE:
METHOD/FunCTIOn DESCRIPTIOn:
Function Name Access Specifier Return Type Description
main(String args[]) public void Reads user input,
processes the date and
checks if it is palindromic.
VARIAbLE DESCRIPTIOn:
Variable Name Datatype Description
d String Stores the input date.
dateSlice String[] Array that holds the split
parts of the date string (day,
month, year)
date String Concatenated String
containing day, month and
year without slashes.
dateRev String Stores the reverse of the
date String.
i int Loop control variable for
reversing the string.
OuTPuT :
PROGRAM 18
Design a class in java to accept a day number (between 1 to 366), year (4 digits)
to generate and display the corresponding date. The program further accepts
'n' (1<=n<=100) from the user to compute and display the future date
corresponding to 'n' days after the generated date.
Sample input:
Day number: 233
Year: 2008
Days after (n): 17
Sample output:
20 August 2008
Days after 17 days:
6 September 2008
ALGORITHM
Algorithm for main() function:
Step 1:- Start
Step 2:- Read dayNumber, year, and daysAfter from the user.
Step 3:- Validate the input:
Step 3.1:- dayNumber should be between 1 and 366.
Step 3.2:- year should be between 1000 and 9999.
Step 3.3:- daysAfter should be between 1 and 100.
Step 3.4:- If any condition is false, print "Invalid Input" and terminate.
Step 4:- Initialize an array representing the number of days in each month.
Step 4.1:- If the year is a leap year, set February to 29, else 28.
Step 5:- Determine the month corresponding to dayNumber:
Step 5.1:- Loop through the months, subtracting the month's days from
dayNumber until the correct month is found.
Step 6:- Convert the month number to its name using getMonthString().
Step 7:- Print the original date.
Step 8:- Calculate the future date:
Step 8.1:- Add daysAfter to dayNumber.
Step 8.2:- Adjust futureDay and futureMonth while futureDay exceeds the
current month’s days.
Step 9:- Convert futureMonth to its name using getMonthString().
Step 10:- Print the future date.
Step 11:- End.
Algorithm for isLeapYear(int year) function:
Step 1:- Start
Step 2:- If year is divisible by 4 and not by 100, return true.
Step 3:- If year is divisible by 400, return true.
Step 4:- Otherwise, return false.
Step 5:- End.
Algorithm for getMonthString(int month) function:
Step 1:- Start
Step 2:- Use a switch statement to return the corresponding month name for a
given month number.
Step 3:- If the month is invalid, return "Invalid Input".
Step 4:- End.
CODE:
METHOD/FunCTIOn DESCRIPTIOn:
Function Name Access Specifier Return Type Description
main(String args[]) public void Reads user input, validates
it, determines the original
and future date and
displays the result.
isLeapYear() public boolean Determines if s a given
year is a leap year.
getMonthString() public String Returns the month name
for a given month number.
VARIAbLE DESCRIPTIOn:
Variable Name Datatype Description
dayNumber int Stores the entered day
number (1-366).
year int Stores the entered 4-digit
year.
daysAfter int Stores the number of days
to add to the given date.
daysInMonths[] int Stores the number of days
in each month, considering
leap years.
month int Stores the month number
corresponding to
dayNumber.
monthString String Stores the month name for
month.
futureDay int Stores the day number after
adding daysAfter.
futureMonth int Stores the month number
after adding daysAfter.
futureMonthString String Stores the month name for
futureMonth.
OuTPuT :
PROGRAM 19
Design a class in java to input long integer data not less than five digits. Your
program should reject it if data is less than five digits. In the input, the last four
digits will be taken as year (validity of the year should be checked) and the
remaining digit as the total number of days. Your program should display the
output as an actual date.
Sample input: 272008
Sample output: 27 January 2008
Sample input: 362017
Sample output: 5 February 2017
ALGORITHM
Step 1:- Start
Step 2:- Read an integer input from the user.
Step 3:- If the input is less than 10000, print "Invalid Input" and exit the
program.
Step 4:- Extract the year by dividing input by 1000.
Step 5:- Extract the day number by taking input % 1000.
Step 6:- Initialize an array daysInMonth containing the number of days in each
month.
Step 7:- Initialize an array months containing the names of the months.
Step 8:- Initialize month to 0.
Step 9:- Loop through daysInMonth:
Step 9.1:- If day is less than or equal to the current month's days, stop.
Step 9.2:- Otherwise, subtract the current month's days from day and move to
the next month.
Step 10:- If month is out of bounds or day is invalid for that month, print
"Invalid date or year."
Step 11:- Otherwise, print the date in the format day month year.
Step 12:- End.
CODE:
METHOD/FunCTIOn DESCRIPTIOn:
Function Name Access Specifier Return Type Description
main(String args[]) public void Reads user input, extracts
the date, determines the
month, validates it and
prints the formatted date.
VARIAbLE DESCRIPTIOn:
Variable Name Datatype Description
input long Stores the number input by
the user.
year long Extracts and stores the year
from input.
day long Extracts and stores the day
number from input.
daysInMonth[] int Array that stores the
number of days in each
month.
months[] String Array that stores the names
of the month.
month int Stores the computed month
index.
i int Loop variable used to iterate
through the months.
OuTPuT :
PROGRAM 20
Design a class in java to accept date and weekdays name of the 1 day of that
month. Display the weekday of that date.
Sample input:
Enter a date: 27/04/2017
Enter the 1st day of that month: Saturday
Sample output:
Day of the date is Thursday
ALGORITHM
Algorithm for main() Function
Step 1:- Start
Step 2:- Display a prompt asking the user to enter a date in dd/mm/yyyy
format.
Step 3:- Read and store the user input.
Step 4:- Display a prompt asking the user to enter the first day of that month.
Step 5:- Read and store the first day of the month.
Step 6:- Split the entered date into day, month, and year.
Step 7:- Convert the extracted day, month, and year into integers.
Step 8:- Call the findWeekday() function with the extracted values.
Step 9:- Retrieve the corresponding weekday name using getWeekdayName().
Step 10:- Display the final weekday.
Step 11:- End.
Algorithm for findWeekday() Function
Step 1:- Start
Step 2:- Initialize an array containing the number of days in each month.
Step 3:- Set totalDays = day.
Step 4:- Loop through previous months and add their days to totalDays.
Step 5:- If the year is a leap year and the month is after February, add an extra
day.
Step 6:- Initialize an array with weekday names.
Step 7:- Find the index of the first day of the month by comparing with the
weekday array.
Step 8:- Compute the weekday index using the formula:
weekday = (totalDays + indexOfFirstDayOfMonth - 1) % 7
Step 9:- Return the computed weekday index.
Step 10:- End.
Algorithm for isLeapYear() Function
Step 1:- Start
Step 2:- Check if the year is divisible by 4 and not divisible by 100, OR divisible
by 400.
Step 3:- If true, return true (leap year).
Step 4:- Otherwise, return false (not a leap year).
Step 5:- End.
Algorithm for getWeekdayName() Function
Step 1:- Start
Step 2:- Initialize an array containing weekday names.
Step 3:- Return the weekday name corresponding to the given index.
Step 4:- End.
CODE:
METHOD/FunCTIOn DESCRIPTIOn:
Function Name Access Specifier Return Type Description
main(String args[]) public void Reads user input,
processes the date and
prints the weekday.
findWeekday() public int Computes the weekday
index for the given date.
isLeapYear() public boolean Checks if the given year is
a leap year.
getWeekdayName() public String Returns the name of the
weekday based on index.
VARIAbLE DESCRIPTIOn:
Variable Name Datatype Description
dateInput String Stores the input date as a
String.
firstDayOfMonth String Stores the first weekday of
the month.
dateParts[] String Stores the split values of
dateInput(day, month, year)
day int Stores the day extracted
from dateInput.
month int Stores the month extracted
from dateInput.
year int Stores the year extracted
from dateInput.
daysInMonth[] int Stores the number of days
in each month.
totalDays int Stores the total number of
days calculated from
previous months.
weekdays[] String Stores the names of the
days of the week.
indexOfFirstDayOfMonth int Stores the index of the first
weekday of the month.
weekday int Stores the computed index
of the weekday for the given
date.
OuTPuT :