Java Program to Display Name of the Weekdays in Calendar Year
Last Updated :
18 Aug, 2021
Concept: In java when it comes down to the date and time problems after hitting the brute force method one should always remember the Date class of java which not only provides to print current or forthcoming year, month, day, date, time, hour, minutes, and even precision to seconds. Not only one can display these parameters but also can be formatted to display them all in different formats. This class takes a step ahead.
Now in order to display the name of weekdays in a calendar year:
Approaches:
- Using existing Date format class
- Using the brute force approach
Approach 1: Using DateFormat class
It is used to display data and time and manipulate date and time in java and in addition to this it is also used for formatting date, time, week, months, years in java across time zone associated data.
Note: Epoch time is 1 Jan 1970
So in order to import this class from a package called java.utils
Syntax:
import java.util.Date ;
After importing this class one can create an object of the Date class in order to print the current date and time. Now in order to print the default date and time simply call the print command using toString() method to get the current date and time. Suppose if the user wants a particular date, time, and month from a current time:
Sample Example: Simple code to clarify the implementation of the date class before moving ahead to displaying the name of weekdays.
Java
import java.util.Date;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
Date date = new Date();
System.out.println(date.toString());
System.out.println(date.getTime());
System.out.println(date.getMonth() + 1 );
system.out.println(date.getYear() + 1900 );
System.out.println(date.getDay(date));
}
}
|
So now if one wants to print date and time in different formats customized date, time, weekdays, years and so on which is the aim of the problem statement.
- A class named text is to be imported
- Then a class is used called- SimpleDateFormat
- Now after this a method needs to be called format
Syntax:
import java.text.*;
The following Java code illustrates the usage of the invoked inbuilt class :
Java
import java.io.*;
import java.text.DateFormatSymbols;
class GFG {
public static void main(String[] args)
{
String[] week_days
= new DateFormatSymbols().getWeekdays();
int length = week_days.length;
for ( int d = 2 ; d < (length - 1 ); d++) {
String day = week_days[d];
System.out.println((d - 1 ) + "th weekday - "
+ day);
}
}
}
|
Output
1th weekday - Monday
2th weekday - Tuesday
3th weekday - Wednesday
4th weekday - Thursday
5th weekday - Friday
DateFormatSymbols is an in-built class in Java available publicly used for combining various date-time formatting data entities, like the month names, weekdays, and the time zone associated data. DateFormatSymbols are used by SimpleDateFormat to carry out encapsulation of the captured information. This class supports an in-built method getWeekdays() of DateFormatSymbols which is used to retrieve the name of the weekdays of the calendar. All the days are returned in a string format. The method has the following syntax in Java :
String[] getWeekdays()
- The method does not accept any arguments or parameters.
- It returns the names of the calendar weekdays in the form of a string array.
Approach 2: Brute force
There are five weekdays in a calendar, which can be maintained in the form of a string array and simulating a for or while loop iterating over the array containing weekdays or a simple switch case.
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
String[] weekdays
= { "Monday" , "Tuesday" , "Wednesday" ,
"Thursday" , "Friday" };
for ( int d = 1 ; d < 6 ; d++) {
System.out.println(d + "th weekday - "
+ weekdays[d - 1 ]);
}
}
}
|
Output
1th weekday - Monday
2th weekday - Tuesday
3th weekday - Wednesday
4th weekday - Thursday
5th weekday - Friday
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...