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 Tutorial
Java is a high-level, object-oriented programming language used to build applications across platformsâfrom web and mobile apps to enterprise software. It is known for its Write Once, Run Anywhere capability, meaning code written in Java can run on any device that supports the Java Virtual Machine (
11 min read
Java OOP(Object Oriented Programming) Concepts
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers
Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling
Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc.An Exception is an unwanted or unexpected event that occurs during the execution of a program (i.e., at runti
10 min read
Collections in Java
Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Interface
An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
Java Programs - Java Programming Examples
In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read