Programming Examples
Java program to print day name for given date
Write a java program to accept a date in the string format dd/mm/yyyy and accept the name of the day on the the 1st January of the corresponding year. Find the day for the given date.
Example: Input:
Date: 5/7/2001
Day on 1st January: MONDAY
Output:
Day on 5/7/2001
THURSDAY
Run the program on the Following input:
Input Date | Day on 1st January | Output Day for |
04/9/1998 | THURSDAY | FRIDAY |
31/8/1999 | FRIDAY | TUESDAY |
06/12/2000 | SATURDAY | WEDNESDAY |
Solution
import java.util.*;
public class DayName
{
public static boolean isLeapYear(int y)
{
boolean ret = false;
if (y % 400 == 0) {
ret = true;
}
else if (y % 100 == 0) {
ret = false;
}
else if (y % 4 == 0) {
ret = true;
}
else {
ret = false;
}
return ret;
}
public static void main(String args[])
{
int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String dayNames[] = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY",
"FRIDAY", "SATURDAY", "SUNDAY"};
Scanner in = new Scanner(System.in);
System.out.print("Enter Date(dd/mm/yyyy): ");
String dateStr = in.nextLine();
StringTokenizer st = new StringTokenizer(dateStr, "/");
int tokenCount = st.countTokens();
if (tokenCount <= 0 || tokenCount > 3) {
System.out.println("Invalid Date");
return;
}
int day = Integer.parseInt(st.nextToken());
int month = Integer.parseInt(st.nextToken());
int year = Integer.parseInt(st.nextToken());
boolean leapYear = isLeapYear(year);
if (leapYear) {
monthDays[1] = 29;
}
if (month < 1 || month > 12) {
System.out.println("Invalid Month");
return;
}
if (day < 1 || day > monthDays[month - 1]) {
System.out.println("Invalid Day");
return;
}
System.out.print("Day on 1st January: ");
String startDayName = in.nextLine();
int startDayIdx = -1;
for (int i = 0; i < dayNames.length; i++) {
if (dayNames[i].equalsIgnoreCase(startDayName)) {
startDayIdx = i;
break;
}
}
if (startDayIdx == -1)
{
System.out.println("Invalid Day Name");
return;
}
//Calculate total days
int tDays = 0;
for (int i = 0; i < month - 1; i++) {
tDays += monthDays[i];
}
tDays += day;
int currDayIdx = tDays % 7 + startDayIdx - 1;
if (currDayIdx >= 7) {
currDayIdx -= 7;
}
System.out.println("Day on " + dateStr + " : " + dayNames[currDayIdx]);
}
}
Output
Enter Date(dd/mm/yyyy): 04/9/1998
Day on 1st January: THURSDAY
Day on 04/9/1998 : FRIDAY