The Wayback Machine - https://web.archive.org/web/20241001000542/https://www.geeksforgeeks.org/java-program-to-display-dates-of-a-calendar-year-in-different-format/
Open In App

Java Program to Display Dates of a Calendar Year in Different Format

Last Updated : 27 Jun, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 Notation Depicts
y year
M month in year 
d day in month 
E day of week

Concept: Whenever it comes down to the date and time the primary goal is to Date Class. It is necessary to be imported into our program.

Date Class was introduced in java in JDK version 1.0 but was later improved in JDK 1.1 by introducing Calendar Class. It deprecated most Calendar Class as 4 out of 6 constructors are marked as deprecated and most of the methods inside it. These two JDK are outdated now and hence known as legacy API. Finally, in 2014 in JDK8 new date/Time API was introduced. it includes java.time package which is now the main API for a date, time, instants, and duration.

Class Description
Clock A clock providing access t the current instant, date, and time using a time zone
Duration A time-based amount of time, such as ‘34.5 seconds’
Instant An instantaneous point on the time-line
LocalDate A date without a time zine, calendar year such as 2007-12-03 
LocalTime A time without a time zone in the calendar system
MonthDay A month day in the calendar system
OffsetTime A time with an offset from Greenwich in the calendar system
Period A data-based amount of time in the calendar system 
Year A year in the calendar system
ZonedDateTime A date with a time zone in a calendar system
ZoneOffset A time zone offset from Greenwich 

 Package java.time.in JDK 8

There are different ways in which we can obtain the current date and time in java. Java doesn’t have a built-in Date class, though we can import java.time package to work with the time and date API. Some classes are as below:

When there is a need to display date and time in a different format, the ofPattern() method is used to accept all sorts of values. There are numerous formats that can be invoked later on can be mixed and match letters to achieve the required pattern.

Here the dealing will be with 3 types of formats:

  1. yyyy-MM-dd
  2. DD/MM/YY
  3. dd MMM yyyy

Below are the java programs for different date format.

Format 1: yyyy-MM-dd

Java




// Java Program to Display Dates in Different Format
 
// Importing Classes/Files
import java.io.*;
// Importing speccificaly Time Class and functionalities
import java.time.*;
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Create an object of LoacalDate type
        LocalDate date = LocalDate.now();
        // .now() method to tore the current date
 
        // Print current date
        System.out.println(date);
    }
}


Output:

2020-11-03

Format 2: DD/MM/YY

Java




// Java Program to Display Dates in Different Format
 
// Importing generic Classes/Files
import java.io.*;
// Importing specific Date and Time Classes/Files
import java.time.*;
import java.time.format.DateTimeFormatter;
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Create date time object and store current time in
        // default format yy-mm-dd
        LocalDateTime date = LocalDateTime.now();
 
        // Creating DateTimeFormatter object
        // to specify date format
        DateTimeFormatter myDateFormat
            = DateTimeFormatter.ofPattern("dd/MM/yyyy");
 
        // Change date into your format and store it in
        // string object
        String formattedDate = date.format(myDateFormat);
 
        // Print formatted date
        System.out.println(formattedDate);
    }
}


Output:

 03/11/2020

Format 3: Day, dd MMM yyyy

Java




// Java Program to Display Dates in Different Format
 
// Importing generic Classes/Files
import java.io.*;
// Importing Date and time specific Classes
import java.time.*;
import java.time.format.DateTimeFormatter;
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Create date time object and store current time in
        // default format yy-mm-dd
        LocalDateTime date = LocalDateTime.now();
 
        // Specify the date format
        // Note: more than 3 characters result in full name
        DateTimeFormatter myDateFormat
            = DateTimeFormatter.ofPattern(
                "EEEE, dd MMM yyyy");
        // e.g- MMM = Oct and MMMM = October
 
        // Change date into req format and store it in
        // string object
        String formattedDate = date.format(myDateFormat);
 
        // Printing formatted date
        System.out.println(formattedDate);
    }
}


Output:

 Tuesday, 03 Nov 2020


Previous Article
Next Article

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 of the months to the shorter format, ie for eg, if
3 min read
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 formatt
5 min read
Java Program to Display Time in Different Country Format
Locale class and DateFormat class is used to display time different Country Format. The DateFormat class in Java is used for formatting dates. A specified date can be formatted into the Data/Time string. For example, a date can be formatted into: mm/dd/yyyy. Date Format classes are not synchronized. A Locale object logically consists of the fields
2 min read
Java Program to Find if a Given Year is a Leap Year
Leap Year contains 366 days, which comes once every four years. In this article, we will learn how to write the leap year program in Java. Facts about Leap YearEvery leap year corresponds to these facts : A century year is a year ending with 00. A century year is a leap year only if it is divisible by 400.A leap year (except a century year) can be
5 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 this using some classes: Method 1: Using SimpleDate
3 min read
Java Program Format Time in MMMM Format
In Java, The MMM format for months is a short form of the month using 3 characters. SimpleDateFormat class is used for implementing this format in java and you can import this package by using the following code. import java.text.SimpleDateFormat Example: Months Name(MMMM Format)MMM FormatJanuaryJanFebruaryFebMarchMarAprilAprMayMayJuneJunJulyJulAug
2 min read
Java Program to Format Time in AM-PM format
Date Time Class Format is used to display date and time and manipulate date and time in java and in addition to this it is also used for formatting date and time class in java across time zone associated data. So in order to import this class from a package called 'java.utils'. After importing this class one can create an object of the Date class i
4 min read
Year format() method in Java with Examples
The format() method of Year class in Java is used to format the current Year object according to the DateTimeFormatter passed to it as a parameter. Syntax: public String format(DateTimeFormatter formatter) Parameter: This method accepts a single parameter formatter. It specifies a DateTimeFormatter according to which the current Year object will be
2 min read
How to Create Different Data Format of Cells in a Spreadsheet Using Java?
Formatting in excel is a neat trick in excel which is used to change the appearance of the data represented in the worksheet, formatting can be done in multiple ways such as we can format the data of the cells by using the styles. By default, all worksheet cells are formatted with the General number format. With the General format, anything you typ
4 min read
How to Create Different Packages For Different Classes in Java?
Let us first know what is a class and package in Java. Class in java is a model for creating objects. It means that the properties and actions of the objects are written in class. Properties are represented by variables and actions of the objects are represented by methods. So, a class contains variables and methods. The same variables are also ava
6 min read
Calendar.before() method in Java
java.util.Calendar.before() is a method in Calendar class of java.util package. The method returns true if the time represented by this Calendar is before the time represented by when Object. If it is not the case, false is returned. Syntax : public boolean before(Object when) Where, when is the Object that is to be compared. Below are some example
2 min read
Calendar.get() Method in Java
java.util.Calendar.get() method is a method of java.util.Calendar class. The Calendar class provides some methods for implementing a concrete calendar system outside the package. Some examples of Calendar fields are : YEAR, DATE, MONTH, DAY_OF_WEEK, DAY_OF_YEAR, WEEK_OF_YEAR, MINUTE, SECOND, HOUR, AM_PM, WEEK_OF_MONTH, DAY_OF_WEEK_IN_MONTH, HOUR_OF
3 min read
java.util.Calendar.after() method
java.util.Calendar.after() is a method in Calendar class of java.util package. The method returns true if the time represented by this Calendar is after the time represented by when Object. If it is not the case, false is returned. Syntax : public boolean after(Object when) Where, when is the Object that is to be compared. Below are some examples t
2 min read
Calendar.equals() method in Java
java.util.Calendar.equals() is a method in Calendar class of java.util package. The method compares this Calendar to the specified Object.The method returns true if this object is equal to object. If this is not the case, i.e, if there is any difference in the parameters between the two Calendars, then false is returned. Syntax : public boolean equ
2 min read
Calendar Class in Java with examples
Calendar class in Java is an abstract class that provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. It inherits Object class and implements the Comparable, Serializable, Cloneable interfaces. As it is an Abstract class, so we cannot use a constructor to create an inst
4 min read
Calendar clone() Method in Java with Examples
The clear() method in Calendar class is used to clone a calendar object. It basically creates a shallow copy of this object. Syntax: public Object clone() Parameters: The method does not take any parameters. Return Value: The method does not return any value. Below programs illustrate the working of clone() Method of Calendar class: Example 1: // J
2 min read
Calendar clear() Method in Java with Examples
The clear() method in Calendar class is used to set all the calendar field values and the time value of this Calendar undefined. Note: Implementation of calendar class may use default field values for date and time calculations. Syntax: public final void clear() Parameters: The method does not take any parameters. Return Value: The method does not
2 min read
Calendar add() Method in Java with Examples
The add() method of Calendar class present inside is used to add or subtract from the given calendar field(int field), a specific amount of time(int amt), based on the calendar's rules. Syntax: public abstract void add(int field, int amt) Parameters: The method takes two parameters: The field of the calendar on which the operation is to be performe
3 min read
Calendar toString() Method in Java with Examples
The toString() method in Calendar class is used to get the string representation of the Calendar object. This method in Calendar Class is just for debug process and not to be used as an operation. Syntax: public String toString() Parameters: The method does not take any parameters. Return Value: The method returns the String representation of the C
1 min read
Calendar compareTo() Method in Java with Examples
The add(Calendar Calendar2) method of Calendar class is used to compare the time values or the millisecond offsets of this Calendar object with the passed Calendar object. Syntax: public int compareTo(Calendar Calendar2) Parameters: The method takes one parameter Calendar2 of Calendar object type and refers to the object to be compared to this Cale
2 min read
Calendar complete() Method in Java with Examples
The complete() method in Calendar class is used to fill any unset fields of the calendar fields. It follows the following process of completion: At first, if the time value has not been calculated, the computeTime() method is called to calculate it from calendar field values. Second, to calculate all the calendar field values the computeFields() me
2 min read
Calendar computeTime() method in Java with Examples
The computeTime() method in Calendar class is used for the conversion operation of the current calendar field values in fields[] to the millisecond time value time. Syntax: protected abstract void computeTime() Parameters: The method does not take any parameters. Return Value: The method does not return any value. Below programs illustrate the work
2 min read
Calendar computeFields() method in Java with Examples
The computeFields() method in Calendar class is used for the conversion operation of the current millisecond time value time to calendar field values mentioned in the fields[]. Note: This allows syncing up the new time that is set for the calendar object with the calendar field values. Syntax: protected abstract void computeFields() Parameters: The
2 min read
Calendar get() method in Java with Examples
The get(int field_value) method of Calendar class is used to return the value of the given calendar field in the parameter. Syntax: public int get(int field) Parameters: The method takes one parameter field_value of integer type and refers to the calendar whose value is needed to be returned. Return Value: The method returns the value of the passed
2 min read
Calendar getActualMinimum() Method in Java with Examples
The getActualMinimum(int field_value) method of Calendar class is used to return the minimum value that the specified calendar field could have, based on the time value of this Calendar. Syntax: public int getActualMinimum(int field_value) Parameters: The method takes one parameter field_value of integer type and refers to the calendar whose minimu
2 min read
Calendar getActualMaximum() Method in Java with Examples
The getActualMaximum(int field_value) method of Calendar class is used to return the maximum value that the specified calendar field could have, based on the time value of this Calendar. Syntax: public int getActualMaximum(int field_value) Parameters: The method takes one parameter field_value of integer type and refers to the calendar whose maximu
2 min read
Calendar getAvailableLocales() Method in Java with Examples
The clear() method in Calendar class is used to return an array of all the locales available at the localized instances of the getInstance method of this class. Syntax: public static Locale[] getAvailableLocales() Parameters: The method does not take any parameters. Return Value: The method returns an array of all the locales for which the localize
2 min read
Calendar getDisplayName() Method in Java with Examples
The getDisplayName(int cal_field, int cal_style, Locale local) method of Calendar class is used to return the string representation of a calendar field(int cal_field) value in the given style(int cal_style) and locale(int local). Syntax: public String getDisplayName(int cal_field, int cal_style, Locale local) Parameters: The method takes three para
3 min read
Calendar getDisplayNames() Method in Java with Examples
The getDisplayNames(int cal_field, int cal_style, Locale local) method of Calendar class is used to return a map containing all the names of the calendar field(int cal_field) in the given style(int cal_style) and locale(Locale local) and their corresponding field values. Syntax: public Map getDisplayNames(int field, int style, Locale locale) Parame
2 min read
Calendar getFirstDayOfWeek() Method in Java with Examples
The getFirstDayOfWeek() method in Calendar class is used to return the first day of the week of this Calendar.Syntax: public int getFirstDayOfWeek() Parameters: The method does not take any parameters.Return Value: The method returns the first day of the week.Below programs illustrate the working of getFirstDayOfWeek() Method of Calendar class: Exa
1 min read