In order to convert a String to Java Date Object, we can make use of the SimpleDateFormat, let us see some examples,
Example 1: Convert 2022-08-26 to Java Date
public static void main(String[] args) throws ParseException {
String dateStr = "2022-08-26";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = simpleDateFormat.parse(dateStr);
System.out.println(date);
}
Output:
Fri Aug 26 00:00:00 CST 2022
Note if you do not provide the correct format, you will get an ParseException. Make sure you know what is the date and month fields, if you have a date like 2022-01-02 its hard to say which is month and which is a year unless you have complete information!
Example:Exception in thread "main" java.text.ParseException: Unparseable date: "202208-26"
at java.text.DateFormat.parse(DateFormat.java:366)
at com.example.demo.JavaStreamFiltersExamples.main(JavaStreamFiltersExamples.java:18)
✏️ You can follow this page to see a comprehensive list of all Date formats: List of Simple Date Formats

More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Android Launch! The connection to adb is down, and a severe error has occured - Android
- AWS SNS CLI Command Publish Message Attributes - AWS
- How to customize SharePoint Modern list form using JSON formatting - SharePoint
- Python: Pandas Merge Indicator (Left, Right and Both) Example - Python
- Duplicate id @+id/textView1, already defined earlier in this layout Android Error - Android
- Java get day of the week as an int using DayOfWeek - Java
- List of Java Keywords - Java
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException - Android