
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Display Dates of Calendar Year in Different Format
In this article, we will understand how to display dates of a calendar year in different formats. Java has a built-in Date class, but it is recommended to use the java.time package, to work with the modern date and time API. The package includes many date and time classes.
Following are the ways to display dates of a calendar year in different formats:
Using DateFormat Class
The DateFormat class is part of the java.text package. We can use it to format dates in a locale-sensitive manner. The DateFormat class provides methods to format and parse dates.
We will use the Calendar class to set the date and then use the DateFormat class to format it.
Example
In the following example, we will instantiate a Calendar instance, set the date to a specific day in a year, and then format it using DateFormat.
import java.text.DateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; public class DateFormatEx { public static void main(String[] args){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, 2025); calendar.set(Calendar.MONTH, Calendar.JUNE); calendar.set(Calendar.DAY_OF_MONTH, 25); Date date = calendar.getTime(); DateFormat full = DateFormat.getDateInstance(DateFormat.FULL, Locale.US); DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.US); DateFormat medium = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US); DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US); System.out.println("Full format: " + full.format(date)); System.out.println("Long format: " + longFormat.format(date)); System.out.println("Medium format: " + medium.format(date)); System.out.println("Short format: " + shortFormat.format(date)); } }
Output
Following is the output of the above code:
Full format: Tuesday, June 25, 2025 Long format: June 25, 2025 Medium format: Jun 25, 2025 Short format: 6/25/25
Using SimpleDateFormat Class
The SimpleDateFormat class is a subclass of DateFormat that allows us to format and parse dates in a locale-sensitive manner. It provides a way to format dates using custom patterns.
We will instantiate a SimpleDateFormat with a specific pattern and then format the date using the created object.
Example
In the following example, we will create a SimpleDateFormat instance with a custom pattern and format the date accordingly.
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class SimpleDateFormatEx { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, 2025); calendar.set(Calendar.MONTH, Calendar.JUNE); calendar.set(Calendar.DAY_OF_MONTH, 25); Date date = calendar.getTime(); SimpleDateFormat sdf1 = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); SimpleDateFormat sdf2 = new SimpleDateFormat("MMMM dd, yyyy"); SimpleDateFormat sdf3 = new SimpleDateFormat("MMM dd, yyyy"); SimpleDateFormat sdf4 = new SimpleDateFormat("MM/dd/yy"); System.out.println("Full format: " + sdf1.format(date)); System.out.println("Long format: " + sdf2.format(date)); System.out.println("Medium format: " + sdf3.format(date)); System.out.println("Short format: " + sdf4.format(date)); } }
Output
Following is the output of the above code:
Full format: Tuesday, June 25, 2025 Long format: June 25, 2025 Medium format: Jun 25, 2025 Short format: 06/25/25
Using LocalDate Class and DateTimeFormatter
The LocalDate class is part of the java.time package was introduced in Java 8. It represents a date without time-zone in the ISO-8601 calendar system. We can use it to get the current date and format it according to different locales.
Example
We will create a LocalDate instance for a specific date and then format it using DateTimeFormatter.
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Locale; public class LocalDateEx { public static void main(String[] args) { LocalDate date = LocalDate.of(2025, 6, 25); DateTimeFormatter fullFormatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy", Locale.US); DateTimeFormatter longFormatter = DateTimeFormatter.ofPattern("MMMM dd, yyyy", Locale.US); DateTimeFormatter mediumFormatter = DateTimeFormatter.ofPattern("MMM dd, yyyy", Locale.US); DateTimeFormatter shortFormatter = DateTimeFormatter.ofPattern("MM/dd/yy", Locale.US); System.out.println("Full format: " + date.format(fullFormatter)); System.out.println("Long format: " + date.format(longFormatter)); System.out.println("Medium format: " + date.format(mediumFormatter)); System.out.println("Short format: " + date.format(shortFormatter)); } }
Output
Following is the output of the above code:
Full format: Tuesday, June 25, 2025 Long format: June 25, 2025 Medium format: Jun 25, 2025 Short format: 06/25/25