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

Popular posts from this blog

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

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

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

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

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