java.time.ZonedDateTime.truncatedTo() 方法

描述

java.time.ZonedDateTime.truncatedTo(TemporalUnit unit) 方法返回此 ZonedDateTime 的副本,其中时间被截断。


声明

以下是 java.time.ZonedDateTime.truncatedTo(TemporalUnit unit) 方法的声明。

public ZonedDateTime truncatedTo(TemporalUnit unit)

参数

unit − 要截断的单位,不为空。


返回值

基于此日期时间的 ZonedDateTime,时间被截断,不为空。


异常

  • DateTimeException − 如果无法截断。

  • UnsupportedTemporalTypeException − 如果不支持该单位。


示例

下面的例子展示了 java.time.ZonedDateTime.truncatedTo(TemporalUnit unit) 方法的使用。

package com.tutorialspoint;

import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

public class ZonedDateTimeDemo {
   public static void main(String[] args) {
 
      ZonedDateTime date = ZonedDateTime.now();
      System.out.println(date.truncatedTo(ChronoUnit.DAYS));  
   }
}

让我们编译并运行上面的程序,这将产生下面的结果 −

2017-03-28T00:00+05:30[Asia/Calcutta]