Date Format Program in Java and Python
Date formatting program with algorithm, explanation, Java solution and simple Python solution for ISC students.
Question:
Write a program to input a date in 8 digit ddmmyyyy format and display it in dd/mm/yyyy format and in dd, month name, yyyy format. The program should check whether the date is valid.
Algorithm:
Step 1: Start.
Step 2: Accept the date as an 8-character string.
Step 3: If the string length is not 8, display Invalid Date and stop.
Step 4: Extract day from positions 0-1, month from positions 2-3 and year from positions 4-7.
Step 5: Store maximum days of each month in an array.
Step 6: If the year is a leap year, set February maximum days to 29.
Step 7: Check that month lies from 1 to 12.
Step 8: Check that day lies from 1 to the maximum days of that month.
Step 9: If valid, display the date in dd/mm/yyyy format.
Step 10: Display the date again using the month-name array.
Step 11: Stop.
Explanation:
The date is accepted as a string because the day, month and year are present at fixed positions in the 8 digit format. The first two characters represent the day, the next two represent the month, and the last four represent the year.
The arrays maxdays and month are used for validation and display. maxdays stores the maximum number of days in each month, while month stores the month names.
Before validating the date, the program checks whether the year is a leap year. If it is a leap year, February is allowed to have 29 days; otherwise, it remains 28 days.
The date is valid only when the month lies between 1 and 12 and the day lies within the allowed range for that month. After validation, the program displays the date in slash format and then in a readable month-name format.
The program focuses on validating and formatting date parts. Day, month and year are accepted separately or extracted from input, then the day and month are printed with two digits. If either value is less than 10, a leading zero is needed. The logic must also consider valid month lengths and leap years if validation is included. Formatting is separate from date calculation: the numeric values may be stored as integers, but the final display is built as a string in dd/mm/yyyy form.
Java Program:
/**
* The class Date_DDMMYY inputs a Date in ddmmyyyy 8-digit format and prints it in dd/mm/yyyy format
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class Date_DDMMYY
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int l, y, d, m;
String dd, mm, yy;
//array storing the maximum days of every month
int maxdays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//array storing the month names
String month[]={ "", "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December" };
System.out.print("Enter any date in 8 digits (ddmmyyyy) format: ");
String date = sc.nextLine(); //inputting the date in String format
l = date.length(); //finding number of digits in the given input
if(l==8) //performing the task only when number of digits is 8
{
dd = date.substring(0,2); //extracting the day in String format
mm = date.substring(2,4); //extracting the month in String format
yy = date.substring(4); //extracting the year in String format
d = Integer.parseInt(dd); //day in Integer format
m = Integer.parseInt(mm); //month in Integer format
y = Integer.parseInt(yy); //year in Integer format
if((y%400==0) || ((y%100!=0)&&(y%4==0))) // condition for leap year
{
maxdays[2]=29;
}
/* checking whether the day, month and year are within acceptable range
i.e. there cannot be an input like 35012013 because 35/01/2013 is unacceptable*/
if(m<0 || m>12 || d<0 || d>maxdays[m] || y<0 || y>9999) // Performing Date Validation
{
System.out.println("The day, month or year are outside acceptable limit");
}
else
{
/* First Part */
System.out.println("Date in dd/mm/yyyy format = "+dd+"/"+mm+"/"+yy);
/* Second Part */
System.out.print("Date in dd, month name, yyyy format = "+dd+" "+month[m]+", "+yy);
}
}
else
System.out.println("Wrong Input");
}
}Equivalent Python Program:
# Read the date values and separate day, month and year for calculation.
# Helper functions keep repeated calculations separate from the main logic.
# Month lengths and leap-year checks control valid date movement/counting.
# Display the calculated date/day result after all updates are complete.
def is_leap(y):
return y % 400 == 0 or (y % 100 != 0 and y % 4 == 0)
maxdays = [0,31,28,31,30,31,30,31,31,30,31,30,31]
month = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
s = input("Enter the date in ddmmyyyy format: ")
if len(s) != 8:
print("Invalid Date")
else:
d = int(s[0:2])
m = int(s[2:4])
y = int(s[4:8])
if is_leap(y):
maxdays[2] = 29
if m < 1 or m > 12 or d < 1 or d > maxdays[m]:
print("Invalid Date")
else:
print(s[0:2] + "/" + s[2:4] + "/" + str(y))
print(str(d) + " " + month[m] + ", " + str(y))Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.