Java Program to Find Day of the Week for a Given Date

In this program, you'll learn to Find Day of the Week for a Given Date in Java.Write a function that calculates the day of the week for any particular date in the past or future. A typical application is to calculate the day of the week on which someone was born or some other special event occurred.

Step 1
  • Read date of birth as three numbers dd, mm, yyyy (year in 4 digits)
  • Count the number of days, td, from 1.1.1900 to dd.mm.yyyy (including both dates)
    • For example, number of days from 1.1.1900 to 2.2.1900 is 33, (that is td=33).
  • Divide td by 7 and get the remainder R ( if td=33 then R=5 )
  • Decide the day depending on the value of R:
    • if R=0 then day is Sunday
    • if R=1 then day is Monday and so on....
    • if R=6 then day is Saturday
    • (For example, 2.3.1900 was a Friday (1900 was not a leapyear)
Step 2
Can be done easily as follows (keeping in mind that td is eventually divided by 7 and the remainder only matters)
  • find dy= yyyy-1900
  • find ly = (dy-1)/4 (taking the quotient and forgetting the remainder)
  • find the magic number, mn, corresponding to the month mm from the table:
  • Month(mm) 1 2 3 4 5 6 7 8 9 10 11 12
  • MagicNumber(mn) 0 3 3 -1 1 -3 -1 2 -2 0 3 -2
  • find td=dd+dy+ly+mn
  • if mm>2 and yyyy is a leapyear then td=td+1
    • find R=td%7

Find Day of the Week for a Given Date


When you run the program, the output will be:

Enter your Date of Birth. Like DD/MM/YY Format
Date in two digits (DD) : 09
Month in two digits (MM) : 05
Year in four digits (YYYY) : 2020
Your Entered Date 9/5/2020.That day is Thursday


أحدث أقدم