当前位置: 首页>>代码示例>>用法及示例精选>>正文


Java Instant truncatedTo()用法及代码示例

Instant类的truncatedTo()方法用于以指定单位获取此Instant的值。此方法采用参数Unit,即此Instant被截断为的Unit。它返回带有指定单位的值的截断的不可变Instant。

用法:

public Instant truncatedTo(TemporalUnit unit)

参数:此方法采用参数uint,该参数uint是要将此Instant截断的单位。它不能为空。


返回值:此方法以指定的单位返回不可变的截断的Instant。

异常:此方法引发以下异常:

  • DateTimeException:如果单位对于截断无效。
  • UnsupportedTemporalTypeException:如果不支持该设备。

以下示例程序旨在说明Instant.truncatedTo()方法:

程序1:

// Java program to demonstrate 
// Instant.truncatedTo() method 
  
import java.time.*; 
import java.time.temporal.ChronoUnit; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a Instant object 
        Instant instant 
            = Instant.parse("2018-12-30T09:24:54.63Z"); 
  
        // print instance 
        System.out.println("Instant before"
                           + " truncate: "
                           + instant); 
  
        // truncate to ChronoUnit.HOURS 
        // means unit smaller than Hour 
        // will be Zero 
        Instant returnvalue 
            = instant.truncatedTo(ChronoUnit.HOURS); 
  
        // print result 
        System.out.println("Instant after "
                           + " truncate: "
                           + returnvalue); 
    } 
}
输出:
Instant before truncate: 2018-12-30T09:24:54.630Z
Instant after  truncate: 2018-12-30T09:00:00Z

程序2:

// Java program to demonstrate 
// Instant.truncatedTo() method 
  
import java.time.*; 
import java.time.temporal.ChronoUnit; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a Instant object 
        Instant instant 
            = Instant.parse("2018-12-30T09:24:54.63Z"); 
  
        // print instance 
        System.out.println("Instant before"
                           + " truncate: "
                           + instant); 
  
        // truncate to ChronoUnit.DAYS 
        // means unit smaller than DAY 
        // will be Zero 
        Instant returnvalue 
            = instant.truncatedTo(ChronoUnit.DAYS); 
  
        // print result 
        System.out.println("Instant after "
                           + " truncate: "
                           + returnvalue); 
    } 
}
输出:
Instant before truncate: 2018-12-30T09:24:54.630Z
Instant after  truncate: 2018-12-30T00:00:00Z

程序3:显示异常:

// Java program to demonstrate 
// Instant.truncatedTo() method 
  
import java.time.*; 
import java.time.temporal.ChronoUnit; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a Instant object 
        Instant instant 
            = Instant.parse("2018-12-30T09:24:54.63Z"); 
  
        try { 
            instant.truncatedTo(ChronoUnit.ERAS); 
        } 
        catch (Exception e) { 
  
            // print result 
            System.out.println("Exception: " + e); 
        } 
    } 
}
输出:
Exception:
 java.time.temporal.UnsupportedTemporalTypeException:
 Unit is too large to be used for truncation

参考:https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#truncatedTo(java.time.temporal.TemporalUnit)



相关用法

注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Instant truncatedTo() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。