Java Convert LocalDateTime to String

Tags: Java LocalDateTime String Java 8 DateTimeFormatter

In this Java core tutorial we learn how to format a java.time.LocalDateTime object to a date time String in Java programming language with different solutions and example Java code.

Table of contents

  1. Format LocalDateTime using DateTimeFormatter and Custom Date Time Pattern
  2. Format LocalDateTime to String in Localized Pattern
  3. Format LocalDateTime to String using Predefined DateTimeFormatter Constants
  4. Convert LocalDateTime to String using toString() method
  5. More Java Examples how to use DateTimeFormatter with Custom Pattern

Format LocalDateTime using DateTimeFormatter and Custom Date Time Pattern

In Java with a given LocalDateTime object we can use the LocalDateTime.format() method to format it to String in a custom date time pattern defined by DateTimeFormatter object. In the following Java program we learn how to convert a LocalDateTime object to String in format dd-MM-yyyy HH:mm:ss using DateTimeFormatter and LocalDateTime.format() method.

ConvertLocalDateTimeToStringExample1.java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalDateTimeToStringExample1 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        String dateTimeString = localDateTime.format(dateTimeFormatter);

        System.out.println(dateTimeString);
    }
}
The output as below.
23-04-2022 16:34:02

Format LocalDateTime to String in Localized Pattern

With DateTimeFormatter class we can get a specified localized date time format patterns in different style using the DateTimeFormatter.ofLocalizedDateTime() method.

Below are the list of format styles we can use for LocalDateTime format

  • FormatStyle.MEDIUM to format medium text style such as 23 Apr 2022, 4:34:27 pm
  • FormatStyle.SHORT to format short text style such as 23/4/22, 4:34 pm

In the following Java code we show how to use DateTimeFormatter.ofLocalizedDateTime() method with FormatStyle.MEDIUM

ConvertLocalDateTimeToStringExample2.java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class ConvertLocalDateTimeToStringExample2 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        String dateTimeString = localDateTime.format(dateTimeFormatter);

        System.out.println(dateTimeString);
    }
}
The output as below.
23 Apr 2022, 4:34:27 pm

In the following Java code we show how to use DateTimeFormatter.ofLocalizedDateTime() method with FormatStyle.SHORT

ConvertLocalDateTimeToStringExample3.java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class ConvertLocalDateTimeToStringExample3 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        String dateTimeString = localDateTime.format(dateTimeFormatter);

        System.out.println(dateTimeString);
    }
}
The output as below.
23/4/22, 4:34 pm

Format LocalDateTime to String using Predefined DateTimeFormatter Constants

The DateTimeFormatter class provides predefined constants which we can use to format a LocalDateTime object such as ISO_LOCAL_DATE, ISO_WEEK_DATE, ISO_DATE, ISO_DATE_TIME, ISO_LOCAL_DATE_TIME, BASIC_ISO_DATE, ISO_LOCAL_TIME, ISO_ORDINAL_DATE and ISO_TIME.

In the following Java program we use DateTimeFormatter.ISO_LOCAL_DATE to format a LocalDateTime object to String in ISO-8601 extended local date format.

ConvertLocalDateTimeToStringExample4.java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalDateTimeToStringExample4 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        String dateTimeString = localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);

        System.out.println(dateTimeString);
    }
}
The output as below.
2022-04-23

In the following Java program we use DateTimeFormatter.ISO_WEEK_DATE to format a LocalDateTime object to String in ISO-8601 extended week-based date format.

ConvertLocalDateTimeToStringExample5.java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalDateTimeToStringExample5 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        String dateTimeString = localDateTime.format(DateTimeFormatter.ISO_WEEK_DATE);

        System.out.println(dateTimeString);
    }
}
The output as below.
2022-W16-6

In the following Java program we use DateTimeFormatter.ISO_DATE to format a LocalDateTime object to String in ISO-8601 extended date format.

ConvertLocalDateTimeToStringExample6.java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalDateTimeToStringExample6 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        String dateTimeString = localDateTime.format(DateTimeFormatter.ISO_DATE);

        System.out.println(dateTimeString);
    }
}
The output as below.
2022-04-23

In the following Java program we use DateTimeFormatter.ISO_DATE_TIME to format a LocalDateTime object to String in ISO-8601 extended local or offset date-time format.

ConvertLocalDateTimeToStringExample7.java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalDateTimeToStringExample7 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        String dateTimeString = localDateTime.format(DateTimeFormatter.ISO_DATE_TIME);

        System.out.println(dateTimeString);
    }
}
The output as below.
2022-04-23T16:36:32.2190362

In the following Java program we use DateTimeFormatter.ISO_LOCAL_DATE_TIME to format a LocalDateTime object to String in ISO-8601 extended offset date-time format.

ConvertLocalDateTimeToStringExample8.java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalDateTimeToStringExample8 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        String dateTimeString = localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);

        System.out.println(dateTimeString);
    }
}
The output as below.
2022-04-23T16:36:58.1646769

In the following Java program we use DateTimeFormatter.BASIC_ISO_DATE to format a LocalDateTime object to String in ISO-8601 basic local date format.

ConvertLocalDateTimeToStringExample9.java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalDateTimeToStringExample9 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        String dateTimeString = localDateTime.format(DateTimeFormatter.BASIC_ISO_DATE);

        System.out.println(dateTimeString);
    }
}
The output as below.
20220423

In the following Java program we use DateTimeFormatter.ISO_LOCAL_TIME to format a LocalDateTime object to String in ISO-8601 extended local time format.

ConvertLocalDateTimeToStringExample10.java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalDateTimeToStringExample10 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        String dateTimeString = localDateTime.format(DateTimeFormatter.ISO_LOCAL_TIME);

        System.out.println(dateTimeString);
    }
}
The output as below.
16:37:52.2274156

In the following Java program we use DateTimeFormatter.ISO_ORDINAL_DATE to format a LocalDateTime object to String in ISO-8601 extended ordinal date format.

ConvertLocalDateTimeToStringExample11.java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalDateTimeToStringExample11 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        String dateTimeString = localDateTime.format(DateTimeFormatter.ISO_ORDINAL_DATE);

        System.out.println(dateTimeString);
    }
}
The output as below.
2022-113

In the following Java program we use DateTimeFormatter.ISO_TIME to format a LocalDateTime object to String in ISO-8601 extended offset time format.

ConvertLocalDateTimeToStringExample12.java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalDateTimeToStringExample12 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        String dateTimeString = localDateTime.format(DateTimeFormatter.ISO_TIME);

        System.out.println(dateTimeString);
    }
}
The output as below.
16:38:38.0885583

Convert LocalDateTime to String using toString() method

In Java we can simply use the LocalDateTime.toString() method to convert a LocalDateTime object to String as Java code below.

ConvertLocalDateTimeToStringExample13.java

import java.time.LocalDateTime;

public class ConvertLocalDateTimeToStringExample13 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        String dateTimeString = localDateTime.toString();

        System.out.println(dateTimeString);
    }
}
The output as below.
2022-04-23T16:39:01.222016100

More Java Examples how to use DateTimeFormatter with Custom Pattern

In the following Java program we show more example Java code of how to use custom date time format pattern to convert LocalDateTime object to date time String.

ConvertLocalDateTimeToStringExample14.java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalDateTimeToStringExample14 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String dateTimeString1 = localDateTime.format(formatter1);

        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd-M-yyyy hh:mm:ss a");
        String dateTimeString2 = localDateTime.format(formatter2);

        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("E, MMM dd yyyy HH:mm:ss");
        String dateTimeString3 = localDateTime.format(formatter3);

        DateTimeFormatter formatter4 = DateTimeFormatter.ofPattern("EEEE, MMM dd yyyy HH:mm:ss");
        String dateTimeString4 = localDateTime.format(formatter4);

        DateTimeFormatter formatter5 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        String dateTimeString5 = localDateTime.format(formatter5);

        DateTimeFormatter formatter6 = DateTimeFormatter.ofPattern("hh:mm:ss.SSSSSSS a");
        String dateTimeString6 = localDateTime.format(formatter6);

        DateTimeFormatter formatter7 = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss-SSSSSSS");
        String dateTimeString7 = localDateTime.format(formatter7);

        System.out.println("yyyy-MM-dd HH:mm:ss -> " + dateTimeString1);
        System.out.println("dd-M-yyyy hh:mm:ss a -> " + dateTimeString2);
        System.out.println("E, MMM dd yyyy HH:mm:ss -> " + dateTimeString3);
        System.out.println("EEEE, MMM dd yyyy HH:mm:ss -> " + dateTimeString4);
        System.out.println("dd/MM/yyyy -> " + dateTimeString5);
        System.out.println("hh:mm:ss.SSSSSSS -> " + dateTimeString6);
        System.out.println("yyyy-MM-dd-HH-mm-ss-SSSSSSS -> " + dateTimeString7);
    }
}
The output as below.
yyyy-MM-dd HH:mm:ss -> 2022-04-23 16:39:21
dd-M-yyyy hh:mm:ss a -> 23-4-2022 04:39:21 pm
E, MMM dd yyyy HH:mm:ss -> Sat, Apr 23 2022 16:39:21
EEEE, MMM dd yyyy HH:mm:ss -> Saturday, Apr 23 2022 16:39:21
dd/MM/yyyy -> 23/04/2022
hh:mm:ss.SSSSSSS -> 04:39:21.5744122 pm
yyyy-MM-dd-HH-mm-ss-SSSSSSS -> 2022-04-23-16-39-21-5744122

Happy Coding 😊

Java Convert LocalDateTime to LocalDate

Java Convert LocalDateTime to LocalTime

Java Convert LocalDateTime to OffsetDateTime

Java Convert LocalDateTime to ZonedDateTime

Java Convert LocalDateTime to Calendar

Java Convert LocalDateTime to Date

Java Convert LocalDateTime to Instant

Java Convert LocalDateTime to Epoch Seconds

Java Convert Epoch Seconds to LocalDateTime

Java Compare two LocalDateTime Values

Java Compare two LocalTime Values

Java LocalTime by Examples

Java LocalDate by Examples

Java LocalDateTime by Examples

Java Date by Examples

Java Calendar by Examples

Java Calendar using Calendar.Builder by Examples