📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
DateTimeFormatter
in Java, part of the java.time.format
package, is used for formatting and parsing date-time objects. It provides a flexible way to handle date and time representations.
Table of Contents
- What is
DateTimeFormatter
? - Creating
DateTimeFormatter
Instances - Common Methods
- Examples of
DateTimeFormatter
- Conclusion
1. What is DateTimeFormatter?
DateTimeFormatter
is an immutable and thread-safe class used for formatting and parsing date-time objects. It supports various predefined and custom formats.
2. Creating DateTimeFormatter Instances
You can create DateTimeFormatter
instances in several ways:
DateTimeFormatter.ofPattern(String pattern)
: Creates a formatter using a custom pattern.DateTimeFormatter.ISO_LOCAL_DATE
: A predefined formatter for the ISO local date format.DateTimeFormatter.ISO_LOCAL_DATE_TIME
: A predefined formatter for the ISO local date-time format.
3. Common Methods
format(TemporalAccessor temporal)
: Formats a date-time object.parse(CharSequence text)
: Parses a string to a date-time object.withLocale(Locale locale)
: Returns a copy of this formatter with a new locale.
4. Examples of DateTimeFormatter
Example 1: Formatting a LocalDate
This example demonstrates how to format a LocalDate
using a custom pattern.
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class FormatLocalDateExample { public static void main(String[] args) { LocalDate date = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); String formattedDate = date.format(formatter); System.out.println("Formatted Date: " + formattedDate); } }
Output:
Formatted Date: 30-06-2024
Example 2: Parsing a Date String
Here, we parse a date string into a LocalDate
using a custom pattern.
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class ParseDateStringExample { public static void main(String[] args) { String dateString = "30-06-2023"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); LocalDate date = LocalDate.parse(dateString, formatter); System.out.println("Parsed Date: " + date); } }
Output:
Parsed Date: 2023-06-30
Example 3: Using Predefined Formatters
This example shows how to use a predefined formatter to format a LocalDateTime
.
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class PredefinedFormatterExample { public static void main(String[] args) { LocalDateTime dateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; String formattedDateTime = dateTime.format(formatter); System.out.println("Formatted Date-Time: " + formattedDateTime); } }
Output:
Formatted Date-Time: 2024-06-30T13:16:38.42381
Example 4: Formatting with Locale
In this example, we demonstrate how to format a LocalDate
with a specific locale.
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Locale; public class LocaleFormattingExample { public static void main(String[] args) { LocalDate date = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy", Locale.FRANCE); String formattedDate = date.format(formatter); System.out.println("Formatted Date with Locale: " + formattedDate); } }
Output:
Formatted Date with Locale: 30 juin 2024
Conclusion
The DateTimeFormatter
class in Java is a versatile tool for formatting and parsing date-time objects. It supports both predefined and custom formats, allowing for flexible date and time handling. Using DateTimeFormatter
can lead to more precise and readable date-time representations in your Java applications.
Comments
Post a Comment
Leave Comment