0
0
JavaHow-ToBeginner · 2 min read

Java How to Convert String to Date with Example

In Java, convert a string to a date by using DateTimeFormatter to define the format and LocalDate.parse(yourString, formatter) to get the date object.
📋

Examples

Input"2024-06-15"
OutputLocalDate object representing June 15, 2024
Input"15/06/2024"
OutputLocalDate object representing June 15, 2024 using pattern dd/MM/yyyy
Input"2024-02-30"
OutputException because date is invalid
🧠

How to Think About It

To convert a string to a date, first understand the format of the string date. Then create a formatter that matches this format. Finally, use the formatter to parse the string into a date object.
📐

Algorithm

1
Get the input string representing the date.
2
Create a date formatter matching the string's date pattern.
3
Use the formatter to parse the string into a date object.
4
Return or use the date object as needed.
💻

Code

java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class StringToDate {
    public static void main(String[] args) {
        String dateString = "2024-06-15";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(dateString, formatter);
        System.out.println(date);
    }
}
Output
2024-06-15
🔍

Dry Run

Let's trace converting "2024-06-15" to a LocalDate object.

1

Input string

dateString = "2024-06-15"

2

Create formatter

formatter pattern = "yyyy-MM-dd"

3

Parse string

LocalDate.parse("2024-06-15", formatter) returns LocalDate object for 2024-06-15

StepActionValue
1Input string2024-06-15
2Formatter patternyyyy-MM-dd
3Parsed date2024-06-15 (LocalDate)
💡

Why This Works

Step 1: Define the format

The DateTimeFormatter tells Java how the string date is structured so it can read it correctly.

Step 2: Parse the string

Using LocalDate.parse() with the formatter converts the string into a date object.

Step 3: Use the date

The resulting LocalDate object can be used for date calculations or formatting.

🔄

Alternative Approaches

Using java.util.Date and SimpleDateFormat
java
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDateLegacy {
    public static void main(String[] args) throws Exception {
        String dateString = "15/06/2024";
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        Date date = formatter.parse(dateString);
        System.out.println(date);
    }
}
Older approach, less recommended because it's mutable and less thread-safe.
Using Instant for timestamp strings
java
import java.time.Instant;

public class StringToInstant {
    public static void main(String[] args) {
        String timestamp = "2024-06-15T10:15:30Z";
        Instant instant = Instant.parse(timestamp);
        System.out.println(instant);
    }
}
Use this for ISO-8601 timestamp strings with time and zone info.

Complexity: O(1) time, O(1) space

Time Complexity

Parsing a fixed-length date string is a constant time operation with no loops.

Space Complexity

Only a few objects are created for the formatter and date, so space is constant.

Which Approach is Fastest?

Using LocalDate.parse() with DateTimeFormatter is modern, fast, and thread-safe compared to legacy SimpleDateFormat.

ApproachTimeSpaceBest For
LocalDate + DateTimeFormatterO(1)O(1)Modern date parsing, thread-safe
SimpleDateFormat + DateO(1)O(1)Legacy code, older Java versions
Instant.parseO(1)O(1)Parsing ISO-8601 timestamps with time zone
💡
Always match the formatter pattern exactly to your string date format to avoid parsing errors.
⚠️
Using a formatter pattern that does not match the string format causes runtime exceptions.