Home Java LocalDateTime Methods toString() Java LocalDateTime toString() Method 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.
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 Call What 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.time.LocalDateTime
public String toString() Parameters Parameter Description None toString() takes no parameters.
Return Value Return Value Description StringThe ISO-8601 text, such as 2026-03-15T10:30 or 2026-03-15T10:30:45.123.
Important
The length of the text changes with the value. Zero seconds with no fraction print as HH:mm. A non-zero second prints as HH:mm:ss. A fraction adds 3, 6, or 9 digits , whichever is the shortest that fits. If you need a fixed length, such as always showing the seconds, use format() with a pattern.
Compare with Similar Methods Other ways to get text from a date-time:
Java Examples 1. Hidden seconds 2. Fraction digits 3. ISO and parse Example 1 — See When Seconds Are Printed Print date-times with no seconds, with seconds, and with an explicit zero seconds.
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());
}
} 2026-03-15T10:30
2026-03-15T10:30:45
2026-03-15T10:30
meeting at 2026-03-15T10:30 How It Works No seconds. When the seconds and the fraction are zero, the text ends after the minutes.With seconds. A non-zero second is shown, so the text ends with :45.An explicit zero changes nothing. 10:30:00 is the same value as 10:30, so it prints in the short form.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.
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);
}
} 2026-03-15T10:30:45.500
2026-03-15T10:30:45.001500
2026-03-15T10:30:45.123456789
2026-03-15T10:30:00.000000001 How It Works Half a second. 500,000,000 nanoseconds fit in 3 digits, so the text is .500.A tenth of a millisecond. 1,500,000 nanoseconds do not fit in 3 digits, so Java uses 6 and prints .001500.A full fraction. 123,456,789 needs all 9 digits.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.
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));
}
} toString: 2026-03-15T10:30
ISO format: 2026-03-15T10:30:00
same text: false
parse back: true
parse ISO: true How It Works Two texts for one value. toString() hides the zero seconds, but the ISO formatter always prints them.They are different strings. The third line prints false.Both parse back. LocalDateTime.parse() understands both forms, and the two lines equal the original.What to learn. Do not compare the text of date-times. Compare the objects with equals() or isEqual().Frequently Asked Questions What format does LocalDateTime toString() use? ISO-8601: yyyy-MM-ddTHH:mm, with the seconds and the fraction added only when they are not zero.
Why are the seconds missing from the output? When the seconds and the fraction are both zero, toString() leaves them out. Use format() to always show them.
How do I print a LocalDateTime in another format? Use dt.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")), or any pattern you like.
How do I convert a String back to a LocalDateTime? Use LocalDateTime.parse(text). For another format, pass a DateTimeFormatter as the second argument.
How many digits can the fraction have? Three, six, or nine, depending on how precise the value is.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion