Java - How to convert String to Date

In this section, we will show you how to convert String to java.util.Date in Java.

Refer to table below for some of the common date and time patterns used in java.text.SimpleDateFormat, refer to this JavaDoc

1. String = 11-July-2023

Main.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
public static void main(String[] argv) {

SimpleDateFormat formatter =
new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "11-July-2023";

try {

Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));

} catch (ParseException e) {
e.printStackTrace();
}

}
}


Console Output:
Tue Jul 11 00:00:00 IST 2023
11-Jul-2023

 

2. String = 11/09/2023

Main.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
public static void main(String[] argv) {

SimpleDateFormat formatter =
new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "11/09/2023";

try {

Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));

} catch (ParseException e) {
e.printStackTrace();
}
}
}


Console Output:
Mon Sep 11 00:00:00 IST 2023
11/09/2023

3. String = Mon, July 17 2023

Main.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
public static void main(String[] argv) {

SimpleDateFormat formatter =
new SimpleDateFormat("E, MMM dd yyyy");
String dateInString = "Mon, July 17 2023";

try {

Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));

} catch (ParseException e) {
e.printStackTrace();
}
}
}

Console Output:
Mon Jul 17 00:00:00 IST 2023
Mon, Jul 17 2023

4. String = Saturday, Jun 17, 2023 11:03:46 AM

Main.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
public static void main(String[] argv) {

SimpleDateFormat formatter =
new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
String dateInString = "Saturday, Jun 17, 2023 11:03:46 AM";

try {

Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));

} catch (ParseException e) {
e.printStackTrace();
}
}
}


Console Output:
Sat Jun 17 11:03:46 IST 2023
Saturday, Jun 17, 2023 11:03:46 am

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - Blowfish Encryption and decryption Example

Java - DES Encryption and Decryption example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String

Top 5 Java ORM tools - 2024