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
// Java Program to Display name of the weekdays in calendar
// year Sample code showing different data class parameters
// Importing Libraries
import java.util.Date;
import java.util.*;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of Date class- date
Date date = new Date();
// Printing date
System.out.println(date.toString());
System.out.println(date.getTime());
// Remember to add 1 to it because this print
// current month Jan is assigned 0
System.out.println(date.getMonth() + 1);
// Remember to add 1 to it because this print
// epoch year 1970 is set as reference
system.out.println(date.getYear() + 1900);
// For week internally it starts with Monday=1
System.out.println(date.getDay(date));
// no ambiguity here in displaying week
}
}
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
// Importing generic Classes/Files
import java.io.*;
// Importing specific text class for formatting
// week via inbuilt function getweek() of data class
import java.text.DateFormatSymbols;
// Dealing with week parameter of data class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Inbuilt function to invoke weekdays by creating
// an object of DateFormatSymbols
String[] week_days
= new DateFormatSymbols().getWeekdays();
// Computing length to get end bound
// for iteration
int length = week_days.length;
// Loop is started with 2 because 0th day is
// saturday and 1st day is sunday
for (int d = 2; d < (length - 1); d++) {
// Iterating over the string array of weekdays
// to get respective names
String day = week_days[d];
// Printing ith index weekday
System.out.println((d - 1) + "th weekday - "
+ day);
}
}
}
Output1th 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
// Importing generic Classes/Files
import java.io.*;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a list of weekdays
String[] weekdays
= { "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday" };
// Iterating 5 times only
// since there are 5 weekdays
for (int d = 1; d < 6; d++) {
// Message printing weekdays in calendar year
System.out.println(d + "th weekday - "
+ weekdays[d - 1]);
}
}
}
Output1th weekday - Monday
2th weekday - Tuesday
3th weekday - Wednesday
4th weekday - Thursday
5th weekday - Friday
Similar Reads
Java Program to Display Name of Months of Calendar Year in Short Format
As we know that in a calendar year, there are a total of 12 Months. In order to convert the name of months to a shorter format, there are basically two ways, ie we can either make use of DateFormatSymbols class or SimpleDateFormat class. These classes have methods that are used to convert the names
3 min read
Java Program to Display Dates of a Calendar Year in Different Format
As different countries do opt for different formats. So here the goal is simply to print dates of calendar in different years. The generic symbolic notation opted across the globe is: Symbolic NotationDepictsyyearMmonth in year dday in month Eday of week Concept: Whenever it comes down to the date a
4 min read
How to Calculate the Week Number of the Year for a Given Date in Java?
In Java, we have the Calendar class and the newer LocalDate class from java.time package. We can use these classes to find which week number of the year for a given date. The java.time package offers better functionality than the older date and time classes such as Calendar class. In this article, w
3 min read
Java Program to Display Name of a Month in (MMM) Format
Java is a most powerful programming language, by which we can do many things and Java is an industry preferable language. So it has a huge field of features. Here we discuss one of the best features of Java, that is how we can represent Month in a short form or MMM Format. There are two ways to do t
3 min read
How to Find Which Week of the Year in Java?
In Java, we have the Calendar class and the newer LocalDate class from the java.time package which we can use to find which week of the year it is. The java.time package offers better functionality and ease of use than older date and time classes such as Calendar class. In this article, we will lear
4 min read
Java Program to Display Current Date and Time
Java is a most powerful programming language, by which we can do many things and Java is an industry preferable language. So it has a huge field of features. Here we discuss one of the best features of Java, that is how to represent the current date and time using Java. There are many ways to do thi
3 min read
Java Program to Generate Calendar of Any Year Without calendar.get() Function
Java program for generating the calendar of any desired year and month let us first go through an illustration before landing upon logic and procedural part. Illustration: Say the user wants to get the calendar of April 2011. Then, he is required to enter the year along with the month as integers an
4 min read
How to get Day, Month and Year from Date in Java
Given a date in the form of a string, the task is to write a Java Program to get the day, month, and year from the given date. Examples: Input: date = "2020-07-18"Output:Day: 18Month: JulyYear: 2020Explanation: The given date is '2020-07-18', so the day is: 18, the month is: July, and the year is: 2
4 min read
Find Day of Week using SimpleDateFormat class in Java
Given the day, month, and year, the task is to find the corresponding day of the week using SimpleDateFormat class in Java. Examples Input: 11-08-2020 Output: Tuesday Explanation: The day corresponding to the date 11-08-2020 is tuesday. Input: 17-08-2006 Output: Thursday Explanation: The day corresp
2 min read
Java Program to Print the Months in Different Formats
For printing months in different formats, we are going to use two classes of java.util package. That is the first one Calendar class and another one is Formatter class. From Calendar class use getInstance() method to get instance (time and date information) of calendar according to the current time
2 min read