java.time.ZonedDateTime.truncatedTo()方法示例



說明

java.time.ZonedDateTime.truncatedTo(TemporalUnit unit) 方法使用已截斷的時間返回此 ZonedDateTime 的副本。

宣告

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

public ZonedDateTime truncatedTo(TemporalUnit unit)

引數

unit - 要截斷的單位,非 null。

返回值

基於此日期時間(時間已截斷)的 ZonedDateTime,非 null。

異常

  • 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]
廣告
© . All rights reserved.