🎓 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 parse() method in Java, part of the java.time.LocalDateTime class, is used to obtain an instance of LocalDateTime from a string representation using a specified formatter. This method is useful for converting date-time strings into LocalDateTime instances.
Table of Contents
- Introduction
parse()Method Syntax- Understanding
parse() - Examples
- Basic Usage
- Using
parse()with Custom Formatters
- Real-World Use Case
- Conclusion
Introduction
The parse() method allows you to convert a string representation of a date-time into a LocalDateTime instance. This is particularly useful for reading date-time values from text input or files and converting them into LocalDateTime objects for further processing.
parse() Method Syntax
The parse() method has two overloads:
Overload 1: Using Default Formatter
public static LocalDateTime parse(CharSequence text)
Overload 2: Using Custom Formatter
public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)
Parameters:
text: The text to parse, not null.formatter: The formatter to use, not null (for the second overload).
Returns:
- A
LocalDateTimeparsed from the text, not null.
Throws:
DateTimeParseExceptionif the text cannot be parsed.
Understanding parse()
The parse() method parses a text string to produce a LocalDateTime instance. The method can use either the default ISO date-time format or a custom format specified by a DateTimeFormatter.
Examples
Basic Usage
To demonstrate the basic usage of parse(), we will parse a date-time string using the default formatter.
Example
import java.time.LocalDateTime;
public class LocalDateTimeParseExample {
public static void main(String[] args) {
String dateTimeString = "2023-06-15T10:30";
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString);
System.out.println("Parsed DateTime: " + dateTime);
}
}
Output:
Parsed DateTime: 2023-06-15T10:30
Using parse() with Custom Formatters
This example shows how to use the parse() method with a custom formatter to parse a date-time string.
Example
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeParseCustomFormatterExample {
public static void main(String[] args) {
String dateTimeString = "15-06-2023 10:30:45";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
System.out.println("Parsed DateTime: " + dateTime);
}
}
Output:
Parsed DateTime: 2023-06-15T10:30:45
Real-World Use Case
Reading Date-Time from Input
In real-world applications, the parse() method can be used to read date-time values from user input or text files and convert them into LocalDateTime instances for further processing.
Example
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
public class DateTimeInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter date and time in format dd-MM-yyyy HH:mm:ss:");
String input = scanner.nextLine();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(input, formatter);
System.out.println("Parsed DateTime: " + dateTime);
}
}
Output:
Enter date and time in format dd-MM-yyyy HH:mm:ss:
15-06-2023 10:30:45
Parsed DateTime: 2023-06-15T10:30:45
Conclusion
The LocalDateTime.parse() method is used to convert a string representation of a date-time into a LocalDateTime instance. This method is particularly useful for reading date-time values from text input or files and converting them into LocalDateTime objects for further processing. By understanding and using the parse() method, you can effectively manage and manipulate 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