Java LocalDateTime truncatedTo() Method

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

Definition

In Java, dt.truncatedTo(unit) returns a new LocalDateTime in which everything smaller than the unit is set to zero. For example, truncatedTo(ChronoUnit.HOURS) turns 14:30:45 into 14:00.

The date is kept, so it is a good way to remove the seconds, the milliseconds, or the whole time of day.

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.

Syntax

Pass a ChronoUnit. Everything smaller than that unit is cleared.

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

LocalDateTime hour = dt.truncatedTo(ChronoUnit.HOURS);   // 2026-03-15T14:00
LocalDateTime day = dt.truncatedTo(ChronoUnit.DAYS);     // 2026-03-15T00:00
CallWhat it does
dt.truncatedTo(ChronoUnit.DAYS)Midnight at the start of the day.
dt.truncatedTo(ChronoUnit.HOURS)The start of the hour.
dt.truncatedTo(ChronoUnit.MINUTES)Removes the seconds and the fraction.
dt.truncatedTo(ChronoUnit.SECONDS)Removes only the fraction of a second.

truncatedTo(DAYS) gives the same result as toLocalDate().atStartOfDay().

Library declarations
Java
// java.time.LocalDateTime
public LocalDateTime truncatedTo(TemporalUnit unit)

Parameters

ParameterDescription
unit (TemporalUnit)The unit to keep, such as ChronoUnit.HOURS. Smaller parts are set to zero.

Return Value

Return ValueDescription
LocalDateTimeA new date-time with the smaller parts cleared. The original is not changed.

Compare with Similar Methods

Related methods:

MethodHow it differs
withHour(h)Sets the hour to a value you choose, and keeps the minutes and seconds.
toLocalDate()Drops the time completely, and gives a LocalDate.
withNano(n)Sets only the nanosecond part.

Java Examples

Example 1 — Cut to the Day, the Hour, and the Minute

Take one date-time with a long fraction, and cut it to three different units.

Java
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class LdtTruncatedToDemo {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 30, 45, 123_456_789);

        System.out.println("original: " + dt);
        System.out.println("DAYS:     " + dt.truncatedTo(ChronoUnit.DAYS));
        System.out.println("HOURS:    " + dt.truncatedTo(ChronoUnit.HOURS));
        System.out.println("MINUTES:  " + dt.truncatedTo(ChronoUnit.MINUTES));
    }
}

How It Works

  1. A very exact value. It has seconds and a 9-digit fraction.
  2. DAYS. The whole time of day is cleared, so the result is midnight, 00:00.
  3. HOURS. The minutes, seconds, and fraction are cleared, which gives 14:00.
  4. MINUTES. The seconds and the fraction are cleared, which gives 14:30.

Example 2 — Keep Seconds or Milliseconds

Cut only the tiny parts, and keep the rest.

Java
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class LdtTruncatedToFraction {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 30, 45, 123_456_789);

        System.out.println("SECONDS: " + dt.truncatedTo(ChronoUnit.SECONDS));
        System.out.println("MILLIS:  " + dt.truncatedTo(ChronoUnit.MILLIS));
        System.out.println("MICROS:  " + dt.truncatedTo(ChronoUnit.MICROS));
        System.out.println("NANOS:   " + dt.truncatedTo(ChronoUnit.NANOS));
    }
}

How It Works

  1. SECONDS. The fraction is cleared. The seconds stay, so 14:30:45 is printed.
  2. MILLIS. Only the first 3 digits of the fraction are kept: .123.
  3. MICROS. The first 6 digits are kept: .123456.
  4. NANOS. This is the smallest unit, so nothing is cut.

Example 3 — Use HALF_DAYS and See Which Units Fail

Cut to a half day, and try a unit that is longer than a day.

Java
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.UnsupportedTemporalTypeException;

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

        System.out.println("HALF_DAYS: " + dt.truncatedTo(ChronoUnit.HALF_DAYS));

        try {
            dt.truncatedTo(ChronoUnit.WEEKS);
        } catch (UnsupportedTemporalTypeException e) {
            System.out.println("WEEKS -> " + e.getClass().getSimpleName());
        }

        LocalDateTime startOfMonth = dt.withDayOfMonth(1).truncatedTo(ChronoUnit.DAYS);
        System.out.println("start of month: " + startOfMonth);
    }
}

How It Works

  1. HALF_DAYS. The day has two halves. 14:30 is in the second half, which starts at 12:00.
  2. WEEKS is not allowed. Units longer than a day have no fixed length, so the method throws an exception.
  3. Catch it by type. The code prints only the name of the exception class.
  4. A workaround for months. Move to day 1 with withDayOfMonth(1), and then cut to DAYS.

Frequently Asked Questions

Use dt.truncatedTo(ChronoUnit.MINUTES). It clears the seconds and the fraction.
Use dt.truncatedTo(ChronoUnit.DAYS), or dt.toLocalDate().atStartOfDay().
It always cuts down. 14:59:59 truncated to HOURS is 14:00, not 15:00.
Only units of one day or less are allowed. Longer units do not have a fixed length.
Use dt.truncatedTo(ChronoUnit.SECONDS), or dt.withNano(0).

Next: until()

Count the complete units between two date-times.

until() 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.

13 people found this page helpful