🎓 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 (178K+ 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
DateTimeFormatterInstances - 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment