Java Program to Display Name of the Weekdays in Calendar Year
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.
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:. 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
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
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
1th weekday - Monday 2th weekday - Tuesday 3th weekday - Wednesday 4th weekday - Thursday 5th weekday - Friday