SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Weekday Name of a Date Program in Java and Python

10 February 2013

Weekday name of a date program with algorithm, explanation, Java solution and simple Python solution for ISC students.

Question:

Write a program to accept a date and the weekday name of 1st January of that year. Find and display the weekday name of the given date.

INPUT: Enter the day: 5 Enter the month: 7 Enter the year: 2001 Enter weekday on 1st January: Monday OUTPUT: Day on 5/7/2001: Thursday

Algorithm:

Step 1: Start.

Step 2: Accept day, month, year and weekday of 1st January.

Step 3: Store month lengths and weekday names in arrays.

Step 4: Adjust February if the year is a leap year.

Step 5: Find the array position of the weekday of 1st January.

Step 6: Count the total days from 1st January to the given date.

Step 7: Use remainder by 7 to find the weekday of the given date and display it.

Step 8: Use a loop to match the entered weekday with its index in the days array.

Step 9: Use a month loop to add all month lengths before the entered month.

Step 10: Calculate the final day index using remainder by 7.

Step 11: Stop.

Explanation:

The program finds the weekday by counting how many days have passed from 1st January to the given date. Since the weekday advances by one after each day, the remainder after division by 7 gives the required weekday.

The month array stores the number of days in each month. If the given year is a leap year, February is changed from 28 to 29 days before calculating the total.

The days array stores weekday names. The program first finds the index of the weekday given for 1st January, then adds the number of elapsed days up to the required date.

The expression using modulus 7 keeps the answer within the valid weekday index range. This avoids long conditional statements and makes the logic suitable for date-based ISC practical programs.

The program relies on counting days between a known reference and the given date. Once the total number of elapsed days is known, division by 7 gives the weekday offset because weekdays repeat every seven days. Leap years are important because February has an extra day in those years, changing the total count. The solution usually separates month lengths into an array and adjusts February when needed. This makes the date calculation systematic instead of depending on memorised weekday tables.

Java Program:

Java
/**
* The class Date_DayMethod1 finds the weekday name of a given date
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;
class Date_DayMethod1
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        String days[]={"","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        System.out.print("Enter the day : ");
        int d=sc.nextInt();
        System.out.print("Enter the month : ");
        int m=sc.nextInt();
        System.out.print("Enter the year : ");
        int y=sc.nextInt();
        if((y%400==0) || ((y%100!=0)&&(y%4==0)))
        {
            month[2]=29;
        }
        if(m<0 || m>12 || d<0 || d>month[m] || y<0 || y>9999) // Performing Date Validation
        {
            System.out.println("Invalid Date");
        }
        else
        {
            int dn=0;
            for(int i=1;i<m;i++)
            {
                dn=dn+month[i];
            }
            dn=dn+d;

            System.out.print("Enter the Day on 1st January in this year: ");
            String s=sc.nextLine().trim();

            //finding the day of the week which corresponds to the given day name
            int x=0;
            for(int i=1;i<=7;i++)
            {
                if (s.equalsIgnoreCase(days[i]))
                x=i;
            }

            // the main calculation of finding the name of the day of the week of the given date starts here
            for(int i=1;i<dn;i++)
            {
                x++;
                if(x==8)
                x=1;
            }
            System.out.print("Output : "+d+"/"+m+"/"+y+" is a "+days[x]);
        }
    }
}

Equivalent Python Program:

Python
# Read the date values and separate day, month and year for calculation.
# Month lengths and leap-year checks control valid date movement/counting.
# Display the calculated date/day result after all updates are complete.

month = [0,31,28,31,30,31,30,31,31,30,31,30,31]
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
d = int(input("Enter the day: "))
m = int(input("Enter the month: "))
y = int(input("Enter the year: "))
first_day = input("Enter weekday on 1st January: ")
if y % 400 == 0 or (y % 100 != 0 and y % 4 == 0):
    month[2] = 29
day_number = 0
for i in range(7):
    if days[i].lower() == first_day.lower():
        day_number = i
        break
total = d
for i in range(1, m):
    total = total + month[i]
index = (day_number + total - 1) % 7
print("Day on " + str(d) + "/" + str(m) + "/" + str(y) + ": " + days[index])

Output:

INPUT: Enter the day: 5 Enter the month: 7 Enter the year: 2001 Enter weekday on 1st January: Monday OUTPUT: Day on 5/7/2001: Thursday

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →