
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
Format Date with DateFormat Long in Java
In this article, we will learn to use DateFormat.LONG to format dates in Java. Handling dates and times efficiently is crucial for most applications. The Java Date and DateFormat classes provide a flexible and simple way to format dates and times.
What is DateFormat.LONG?
DateFormat.LONG is a constant provided by the DateFormat class in Java. It represents a predefined style for formatting dates in a long, readable format. When you use DateFormat.LONG, Java will format the date in a verbose format, typically displaying the day, month, and year in a comprehensive, localized form.DateFormat.LONG is a constant for long-style patterns.
For example, it might display the date as "January 15, 2025" in the United States locale.
Formatting the Date Using DateFormat.LONG
To format a date using the LONG style, follow these steps ?
Import Necessary Classes: Import the Date and Locale classes from java.util package ?
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
Create a Date Object: Instantiate a Date object representing the current date and time ?
Date dt = new Date();
Obtain a DateFormat Instance for Each Locale: Use DateFormat.getDateInstance(DateFormat.LONG, Locale) to get a DateFormat object for the desired locale. You can specify different locales such as Locale.FRENCH, Locale.GERMANY, Locale.CANADA, and Locale.ITALY to display the date in localized formats ?
DateFormat dateFormat;dateFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRENCH);
Format the Date: Use the format() method of the DateFormat class to format the Date object into a string according to the specified locale's long date format ?
System.out.println("Locale FRENCH = " + dateFormat.format(dt));
Example
Below is an example of formatting date with DateFormat.LONG in Java?
import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Demo { public static void main(String args[]) { Date dt = new Date(); DateFormat dateFormat; // Date Format LONG constant dateFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRENCH); System.out.println("Locale FRENCH = " + dateFormat.format(dt)); dateFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.GERMANY); System.out.println("Locale GERMANY = " + dateFormat.format(dt)); dateFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.CANADA); System.out.println("Locale CANADA = " + dateFormat.format(dt)); dateFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.ITALY); System.out.println("Locale ITALY = " + dateFormat.format(dt)); } }
Output
Locale FRENCH = 15 janvier 2025
Locale GERMANY = 15. Januar 2025
Locale CANADA = January 15, 2025
Locale ITALY = 15 gennaio 2025
Time Complexity: The time complexity is O(1) since the format() method performs a fixed set of operations.
Space Complexity: The space complexity is O(1), as the method generates a formatted string of fixed size based on the date.
Conclusion
In Java, formatting dates with DateFormat.LONG is an excellent choice when you need a human-readable, comprehensive date format. It offers a simple, localized approach to formatting, making it ideal for user-facing applications.