How to Convert String to Date in Java

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:

Wed Oct 18 00:00:00 CDT 2023



Common Date FormatsExample
MM/dd/yyyy10/21/2023
dd/MM/yyyy21/10/2023
yyyy-MM-dd2023-10-21
MMM dd, yyyyOct 21, 2023
EEE, MMM dd, yyyyFri, Oct 21, 2023
EEE, dd MMM yyyyFri, 21 Oct 2023
MMMM dd, yyyyOctober 21, 2023
EEEE, dd MMMM yyyyFriday, 21 October 2023
yyyy-MM-dd HH:mm:ss2023-10-21 14:30:45
dd/MM/yyyy HH:mm:ss21/10/2023 14:30:45
EEE, dd MMM yyyy HH:mm:ssFri, 21 Oct 2023 14:30:45
EEEE, dd MMMM yyyy HH:mm:ssFriday, 21 October 2023 14:30:45
MM/dd/yyyy h:mm a10/21/2023 2:30 PM
dd/MM/yyyy h:mm a21/10/2023 2:30 PM
yyyy-MM-dd'T'HH:mm:ss2023-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:ssFriday, 21 October 2023 at 14:30:45
MMM dd, yyyy 'by' h:mm aOct 21, 2023 by 2:30 PM
EEE, dd MMM yyyy 'at' h:mm aFri, 21 Oct 2023 at 2:30 PM


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!

Buy Code2care a Coffee!

Comments & Discussion

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