Skip to content
Related Articles

Related Articles

Java Program to Display Dates of a Calendar Year in Different Format
  • Last Updated : 05 Nov, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

2020-11-03

Format 2: DD/MM/YY

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Display Dates in Different Format
  
// Importing generic Classses/Files
import java.io.*;
// Importing specific Date and Time Classses/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);
    }
}

chevron_right


Output:

 03/11/2020

Format 3: Day, dd MMM yyyy

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

 Tuesday, 03 Nov 2020

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :