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)
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- macOS Ventura XCode Command Line Tools Installation - MacOS
- [javaScript] Convert text case to lowercase - JavaScript
- Install Java Runtime Environment (Oracle or open JRE) on Ubuntu - Linux
- Setting $JAVA_HOME Environment Variable in macOS - MacOS
- Adding Jackson dependency to Java Gradle Project - Gradle
- auth_client_using_bad_version_title : Error Android Lint - Android
- How to take a screenshot of android emulator (AVD) screen - Android
- Fix: Windows 10/11 Update Install Error - 0x80070103 - Windows