Converting a date into a string

Java:
Converting a date into a string

How to:

Java makes date-to-string conversion straightforward. The java.time.format.DateTimeFormatter class is your go-to. Here’s a code example:

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

public class DateToStringExample {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now(); // Today's date
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        String dateString = date.format(formatter);
        System.out.println(dateString); // Output could be: 20/03/2023, for instance
    }
}

Deep Dive

Historically, Java used SimpleDateFormat from the java.text package, but it wasn’t thread-safe and led to bugs. With Java 8, the java.time package brought thread-safe and immutable date-time classes. The DateTimeFormatter is part of this modern package.

There are alternatives like FastDateFormat from Apache Commons and DateUtils from various libraries. Yet, most Java devs stick with the standard library, which is robust and versatile.

When formatting, DateTimeFormatter uses patterns yyyy for the year, MM for the month, and dd for the day. It can handle pretty complex patterns, even locale-specific ones, with its ofPattern method. It’s also worth noting that DateTimeFormatter is immutable and thread-safe, so you can use the same formatter instance across multiple threads without any synchronization headache.

See Also