Java Date Time - LocalDateTime truncatedTo(TemporalUnit unit) example








LocalDateTime truncatedTo(TemporalUnit unit) returns a copy of this LocalDateTime with the time truncated.

Syntax

truncatedTo has the following syntax.

public LocalDateTime truncatedTo(TemporalUnit unit)

Example

The following example shows how to use truncatedTo.

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
//ww  w . j a v a2 s  .  c  o  m
public class Main {
  public static void main(String[] args) {
    LocalDateTime a = LocalDateTime.of(2014, 6, 30, 12, 01);
    
    LocalDateTime t = a.truncatedTo(ChronoUnit.HOURS);
    
    System.out.println(t);
  }
}

The code above generates the following result.