Java Instant truncatedTo()方法及示例

Java Instant truncatedTo()方法及示例

Instant类的 truncatedTo() 方法是用来获取这个Instant在指定单位中的值。这个方法需要一个参数Unit,它是这个Instant要被截断的单位。它返回一个截断的不可变的 Instant,其值为指定单位。

语法

public Instant truncatedTo(TemporalUnit unit)

参数: 这个方法需要一个参数单位,这个单位是这个瞬间被截断的单位。它不应该是空的。

返回: 该方法返回一个不可变的截断的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)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程