🎓 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
The format() method in Java, part of the java.time.format.DateTimeFormatter class, is used to format a temporal object into a string representation based on a specified pattern. This method is useful for converting date-time objects into human-readable strings.
Table of Contents
- Introduction
format()Method Syntax- Understanding
format() - Examples
- Basic Usage
- Using
format()in Conditional Statements
- Real-World Use Case
- Conclusion
Introduction
The format() method allows you to format a date-time object into a string according to a specified pattern. This is particularly useful when you need to display dates and times in a specific format.
format() Method Syntax
The syntax for the format() method is as follows:
public String format(TemporalAccessor temporal)
Parameters:
temporal: The temporal object to format, not null.
Returns:
- A formatted date-time string.
Throws:
DateTimeExceptionif an error occurs during formatting.
Understanding format()
The format() method takes a temporal object, such as LocalDate, LocalTime, or LocalDateTime, and formats it into a string based on the pattern specified by the DateTimeFormatter.
Examples
Basic Usage
To demonstrate the basic usage of format(), we will format a LocalDate using a specified pattern.
Example
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterExample {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2023, 6, 15);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String formattedDate = date.format(formatter);
System.out.println("Formatted Date: " + formattedDate);
}
}
Output:
Formatted Date: 15-06-2023
Using format() in Conditional Statements
This example shows how to use the format() method in conditional statements to check the formatted date string.
Example
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateConditionalExample {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2023, 12, 25);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM dd");
String formattedDate = date.format(formatter);
if ("December 25".equals(formattedDate)) {
System.out.println("It's Christmas!");
} else {
System.out.println("It's not Christmas.");
}
}
}
Output:
It's Christmas!
Real-World Use Case
Displaying Dates in Different Formats
In real-world applications, the format() method can be used to display dates in various formats based on user preferences or locale settings.
Example
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class DateDisplayExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy", Locale.US);
String formattedDate = date.format(formatter);
System.out.println("Today's Date: " + formattedDate);
}
}
Output:
Today's Date: Sunday, July 07, 2024
Conclusion
The DateTimeFormatter.format() method is used to format a temporal object into a string representation based on a specified pattern. This method is particularly useful for converting date-time objects into formatted strings for display purposes. By understanding and using the format() method, you can effectively manage and display date-time data 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