We can make use of SimpleDateFormat and the parse() from it to convert String to Date Object.
Example:
package org.code2care.examples;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExampleStringToDateJava {
public static void main(String... args) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
try {
String dateString = "18/10/2023";
Date date = simpleDateFormat.parse(dateString);
System.out.println(date);
} catch (ParseException e) {
System.out.println("Error occurred while converting String to Date!");
e.printStackTrace();
}
}
}
Output:
| Common Date Formats | Example |
|---|---|
| MM/dd/yyyy | 10/21/2023 |
| dd/MM/yyyy | 21/10/2023 |
| yyyy-MM-dd | 2023-10-21 |
| MMM dd, yyyy | Oct 21, 2023 |
| EEE, MMM dd, yyyy | Fri, Oct 21, 2023 |
| EEE, dd MMM yyyy | Fri, 21 Oct 2023 |
| MMMM dd, yyyy | October 21, 2023 |
| EEEE, dd MMMM yyyy | Friday, 21 October 2023 |
| yyyy-MM-dd HH:mm:ss | 2023-10-21 14:30:45 |
| dd/MM/yyyy HH:mm:ss | 21/10/2023 14:30:45 |
| EEE, dd MMM yyyy HH:mm:ss | Fri, 21 Oct 2023 14:30:45 |
| EEEE, dd MMMM yyyy HH:mm:ss | Friday, 21 October 2023 14:30:45 |
| MM/dd/yyyy h:mm a | 10/21/2023 2:30 PM |
| dd/MM/yyyy h:mm a | 21/10/2023 2:30 PM |
| yyyy-MM-dd'T'HH:mm:ss | 2023-10-21T14:30:45 |
| EEE, dd MMM yyyy HH:mm:ss 'GMT' | Fri, 21 Oct 2023 14:30:45 GMT |
| EEEE, dd MMMM yyyy 'at' HH:mm:ss | Friday, 21 October 2023 at 14:30:45 |
| MMM dd, yyyy 'by' h:mm a | Oct 21, 2023 by 2:30 PM |
| EEE, dd MMM yyyy 'at' h:mm a | Fri, 21 Oct 2023 at 2:30 PM |
SimpleDateFormat: Documentation Link (Java 17)ParseException(for error handling): Documentation Link (Java 17)Date: Documentation Link (Java 17)
This is not an AI-generated article but is demonstrated by a human with Java 8 on M1 Mac running macOS Sonoma 14.0.
Please support independent contributors like Code2care by donating a coffee.
Buy me a coffee!

Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!