📜  Java中的即时 truncatedTo() 方法和示例

📅  最后修改于: 2022-05-13 01:54:46.632000             🧑  作者: Mango

Java中的即时 truncatedTo() 方法和示例

Instant 类truncatedTo()方法用于获取此 Instant 的指定单位的值。此方法接受一个参数 Unit,即此 Instant 将被截断为的 Unit。它返回一个截断的不可变 Instant,其值采用指定单位。

句法:

public Instant truncatedTo(TemporalUnit unit)

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

返回:此方法返回一个不可变的截断 Instant ,其值采用指定单位。

异常:此方法抛出以下异常:

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

下面的程序说明了 Instant.truncatedTo() 方法:

方案一:

// 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

方案二:

// 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: Java Java.time.temporal.TemporalUnit)