Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Java Exercises: Reads a date and prints the day of the date

Java Basic: Exercise-225 with Solution

Write a Java program to that reads a date (from 2004/1/1 to 2004/12/31) and prints the day of the date. Jan. 1, 2004, is Thursday. Note that 2004 is a leap year.

Pictorial Presentation:

Java Basic Exercises: Reads a date and prints the day of the date.

Sample Solution:

Java Code:

import java.util.*;
 
class Main {
 
    static int days[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    static String name[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
 
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
		System.out.println("Input month and date(o o to exit)");
		
        for(;;){
            int m=in.nextInt(), d=in.nextInt();
            if(m==0&&d==0) break;
            System.out.println(solve(m,d));
        }
    }
 
    static String solve(int month, int date){
        int cur = 3;
		System.out.println("Name of the day:");
        for(int i=0; i<month-1; i++) cur += days[i];
        cur += date-1;
        return name[cur%7];
    }
 
}

Sample Output:

Input month and date(o o to exit)
9
15
Name of the day:
Wednesday

Flowchart:

Flowchart: Reads a date and prints the day of the date.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous:Write a Java program to test the followings.
Next: Write a Java program to print mode values from a given a sequence of integers. The mode value is the element which occurs most frequently. If there are several mode values, print them in ascending order.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe