Java LocalDateTime toString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, toString() returns the date-time as text in the ISO-8601 style, for example 2026-03-15T10:30. The date and the time are separated by the letter T.

Java calls it for you when you print a date-time or join it to a string with +. The text is short: zero seconds are left out, and so is a zero fraction.

An instance method of java.time.LocalDateTime (since Java 8): call it on a LocalDateTime object. Add import java.time.LocalDateTime; at the top of your file. It overrides Object.toString().

Syntax

Call it directly, or let println() and string joining call it.

Java
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30);

String text = dt.toString();                    // "2026-03-15T10:30"
System.out.println("Meeting at " + dt);         // toString() is called for you
CallWhat it does
dt.toString()Returns the ISO-8601 text.
"" + dtCalls toString() automatically.
String.valueOf(dt)Calls toString(), and gives "null" for a null value.
LocalDateTime.parse(text)Reads this text back into a date-time.

For another look, such as 15/03/2026, use format() with a pattern.

Library declarations
Java
// java.time.LocalDateTime
public String toString()

Parameters

ParameterDescription
NonetoString() takes no parameters.

Return Value

Return ValueDescription
StringThe ISO-8601 text, such as 2026-03-15T10:30 or 2026-03-15T10:30:45.123.

Compare with Similar Methods

Other ways to get text from a date-time:

MethodHow it differs
format(formatter)Lets you choose the exact pattern, and always the same length.
LocalDateTime.parse(text)The opposite direction: text to date-time.
equals(obj)Compares date-times by value. You do not need text for that.

Java Examples

Example 1 — See When Seconds Are Printed

Print date-times with no seconds, with seconds, and with an explicit zero seconds.

Java
import java.time.LocalDateTime;

public class LdtToStringSeconds {
    public static void main(String[] args) {
        LocalDateTime noSeconds = LocalDateTime.of(2026, 3, 15, 10, 30);
        LocalDateTime withSeconds = LocalDateTime.of(2026, 3, 15, 10, 30, 45);
        LocalDateTime explicitZero = LocalDateTime.of(2026, 3, 15, 10, 30, 0);

        System.out.println(noSeconds);
        System.out.println(withSeconds);
        System.out.println(explicitZero);
        System.out.println("meeting at " + noSeconds.toString());
    }
}

How It Works

  1. No seconds. When the seconds and the fraction are zero, the text ends after the minutes.
  2. With seconds. A non-zero second is shown, so the text ends with :45.
  3. An explicit zero changes nothing. 10:30:00 is the same value as 10:30, so it prints in the short form.
  4. Joining text. The last line shows that toString() is what + uses.

Example 2 — See How Many Fraction Digits Are Printed

Print date-times with a half-second, a millisecond-sized fraction, and a full nanosecond fraction.

Java
import java.time.LocalDateTime;

public class LdtToStringFraction {
    public static void main(String[] args) {
        LocalDateTime half = LocalDateTime.of(2026, 3, 15, 10, 30, 45, 500_000_000);
        LocalDateTime micro = LocalDateTime.of(2026, 3, 15, 10, 30, 45, 1_500_000);
        LocalDateTime nano = LocalDateTime.of(2026, 3, 15, 10, 30, 45, 123_456_789);
        LocalDateTime onlyNano = LocalDateTime.of(2026, 3, 15, 10, 30, 0, 1);

        System.out.println(half);
        System.out.println(micro);
        System.out.println(nano);
        System.out.println(onlyNano);
    }
}

How It Works

  1. Half a second. 500,000,000 nanoseconds fit in 3 digits, so the text is .500.
  2. A tenth of a millisecond. 1,500,000 nanoseconds do not fit in 3 digits, so Java uses 6 and prints .001500.
  3. A full fraction. 123,456,789 needs all 9 digits.
  4. Zero seconds, but a fraction. When there is a fraction, the seconds are printed even if they are zero, so the last line ends with :00.000000001.

Example 3 — Compare toString with the ISO Formatter

Check that toString text can be parsed back, and see how the ISO formatter differs.

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

public class LdtToStringIso {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30);

        String shortText = dt.toString();
        String isoText = dt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);

        System.out.println("toString:  " + shortText);
        System.out.println("ISO format: " + isoText);
        System.out.println("same text: " + shortText.equals(isoText));
        System.out.println("parse back: " + LocalDateTime.parse(shortText).equals(dt));
        System.out.println("parse ISO:  " + LocalDateTime.parse(isoText).equals(dt));
    }
}

How It Works

  1. Two texts for one value. toString() hides the zero seconds, but the ISO formatter always prints them.
  2. They are different strings. The third line prints false.
  3. Both parse back. LocalDateTime.parse() understands both forms, and the two lines equal the original.
  4. What to learn. Do not compare the text of date-times. Compare the objects with equals() or isEqual().

Frequently Asked Questions

ISO-8601: yyyy-MM-ddTHH:mm, with the seconds and the fraction added only when they are not zero.
When the seconds and the fraction are both zero, toString() leaves them out. Use format() to always show them.
Use dt.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")), or any pattern you like.
Use LocalDateTime.parse(text). For another format, pass a DateTimeFormatter as the second argument.
Three, six, or nine, depending on how precise the value is.

Next: truncatedTo()

Cut off the parts of the time that are smaller than a unit.

truncatedTo() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful