In Java 8 and later versions, you can use the DateTimeFormatter class with the LocalDate class from the java.time package to convert a String to a LocalDate object. Here are some examples:
Example 1:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class StringToLocalDateExample1 {
public static void main(String[] args) {
// String containing date in format yyyy-MM-dd
String str = "2023-03-26";
// Creating DateTimeFormatter instance with pattern
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// Parsing the date String into LocalDate object using formatter
LocalDate date = LocalDate.parse(str, formatter);
System.out.println("LocalDate object: " + date);
}
}
Example 2:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class StringToDateTimeExample2 {
public static void main(String[] args) {
String strDate = "Sunday, March 26, 2023 11:00:30 AM";
String dateFormat = "EEEE, MMMM d, yyyy hh:mm:ss a";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormat);
LocalDateTime localDateTime = LocalDateTime.parse(strDate, dateTimeFormatter);
System.out.println(localDateTime);
}
}
Output: 2023-03-26T11:00:30
Have Questions? Post them here!
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Fail to connect to camera service Android java RuntimeException - Android
- Run DynamoDB Local on Docker Container - Docker
- [Fix] Microsoft teams error code 503 - Failed to reach https - Teams
- [Fix] Microsoft Remote Desktop Error Code: 0x204 - Microsoft
- Error : Invalid key hash.The key hash does not match any stored key hashes - Android
- View in File Explorer option missing in SharePoint Online Edge browser - SharePoint
- Sublime Text 3 spell check shortcut - Sublime
- How to Kill service running on port using terminal command - HowTos