0
0
JavaHow-ToBeginner · 2 min read

Java How to Convert Date to String with Example

In Java, convert a Date to a String by using SimpleDateFormat like this: new SimpleDateFormat("yyyy-MM-dd").format(date).
📋

Examples

InputDate representing 2024-06-01
Output2024-06-01
InputDate representing 1999-12-31
Output1999-12-31
InputDate representing 1970-01-01
Output1970-01-01
🧠

How to Think About It

To convert a date to a string, think about how you want the date to look as text. You pick a pattern like year-month-day, then use a formatter to turn the date into that text format.
📐

Algorithm

1
Get the Date object you want to convert.
2
Create a SimpleDateFormat object with the desired date pattern.
3
Use the format method of SimpleDateFormat to convert the Date to a String.
4
Return or print the formatted String.
💻

Code

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

public class DateToStringExample {
    public static void main(String[] args) {
        Date date = new Date(124, 5, 1); // June 1, 2024 (year starts at 1900)
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        System.out.println(dateString);
    }
}
Output
2024-06-01
🔍

Dry Run

Let's trace converting June 1, 2024 to a string.

1

Create Date object

Date date = new Date(124, 5, 1); // represents 2024-06-01

2

Create formatter

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

3

Format date

String dateString = formatter.format(date); // "2024-06-01"

StepActionValue
1Date object2024-06-01
2Formatter patternyyyy-MM-dd
3Formatted string2024-06-01
💡

Why This Works

Step 1: Date object holds the date

The Date object stores the date internally as milliseconds since 1970.

Step 2: Formatter defines output style

The SimpleDateFormat object uses a pattern like yyyy-MM-dd to decide how the date looks as text.

Step 3: Format method converts date to string

Calling format(date) returns the date as a string in the chosen pattern.

🔄

Alternative Approaches

Using java.time.LocalDate and DateTimeFormatter (Java 8+)
java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DateToStringJava8 {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2024, 6, 1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dateString = date.format(formatter);
        System.out.println(dateString);
    }
}
This modern approach is thread-safe and preferred in Java 8 and later.
Using String.format with Calendar
java
import java.util.Calendar;

public class DateToStringCalendar {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        cal.set(2024, Calendar.JUNE, 1);
        String dateString = String.format("%04d-%02d-%02d", cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH));
        System.out.println(dateString);
    }
}
This manual method formats date parts but is more verbose and error-prone.

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

Time Complexity

Formatting a date is a fixed-time operation without loops, so it runs in constant time O(1).

Space Complexity

Only a small fixed amount of memory is used for the formatter and output string, so space is O(1).

Which Approach is Fastest?

Both SimpleDateFormat and DateTimeFormatter run in constant time; however, DateTimeFormatter is thread-safe and preferred in modern Java.

ApproachTimeSpaceBest For
SimpleDateFormatO(1)O(1)Java versions before 8, simple formatting
DateTimeFormatterO(1)O(1)Java 8+, thread-safe, modern code
String.format with CalendarO(1)O(1)Manual control, but verbose and error-prone
💡
Use SimpleDateFormat or DateTimeFormatter with a clear pattern to get consistent date strings.
⚠️
Forgetting that months start at 0 in Date constructor, causing off-by-one month errors.