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); } } } |
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
// 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 ]); } } } |
1th weekday - Monday 2th weekday - Tuesday 3th weekday - Wednesday 4th weekday - Thursday 5th weekday - Friday