📜  Java中的即时 get() 方法及示例

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

Java中的即时 get() 方法及示例

Instant 类get()方法有助于从该瞬间获取作为参数传递的指定字段的值作为整数值。此方法在此瞬间查询字段的值,返回的值将始终在该字段的有效值范围内。当该字段不受支持且方法无法返回 int 值时,将引发异常。

句法:

public int get(TemporalField field)

参数:此方法接受一个参数字段,即要获取的字段。
返回:此方法返回字段的int值。

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

  • DateTimeException :如果无法获取该字段的值或该值超出该字段的有效值范围。
  • UnsupportedTemporalTypeException :如果该字段不受支持或值的范围超过一个 int。
  • ArithmeticException : 如果发生数值溢出。

下面的程序说明了 get() 方法:

方案一:

Java
// Java program to demonstrate
// Instant.get() method
 
import java.time.*;
import java.time.temporal.ChronoField;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create a Instant object
        Instant instant
            = Instant.parse("2018-12-30T19:34:50.63Z");
 
        // get Milli of Second value from instant
        // using get method
        int secondvalue
            = instant.get(ChronoField.MILLI_OF_SECOND);
 
        // print result
        System.out.println("MilliSecond Field: "
                           + secondvalue);
    }
}


Java
// Java program to demonstrate
// Instant.get() method
 
import java.time.*;
import java.time.temporal.ChronoField;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create a Instant object
        Instant instant
            = Instant.parse("2018-12-30T01:34:50.93Z");
 
        // get Nano of Second value from instant
        // using get method
        int secondvalue
            = instant.get(ChronoField.NANO_OF_SECOND);
 
        // print result
        System.out.println("Nano of Second: "
                           + secondvalue);
    }
}


Java
// Java program to demonstrate
// Instant.get() method
 
import java.time.*;
import java.time.temporal.ChronoField;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create a Instant object
        Instant instant
            = Instant.parse("2018-12-30T01:34:50.93Z");
 
        // try to find era using ChronoField
        try {
 
            int secondvalue
                = instant.get(ChronoField.ERA);
        }
        catch (Exception e) {
 
            // print exception
            System.out.println("Exception: " + e);
        }
    }
}


输出
MilliSecond Field: 630

方案二:

Java

// Java program to demonstrate
// Instant.get() method
 
import java.time.*;
import java.time.temporal.ChronoField;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create a Instant object
        Instant instant
            = Instant.parse("2018-12-30T01:34:50.93Z");
 
        // get Nano of Second value from instant
        // using get method
        int secondvalue
            = instant.get(ChronoField.NANO_OF_SECOND);
 
        // print result
        System.out.println("Nano of Second: "
                           + secondvalue);
    }
}
输出:
Nano of Second: 930000000

程序 3:获取 UnsupportedTemporalTypeException

Java

// Java program to demonstrate
// Instant.get() method
 
import java.time.*;
import java.time.temporal.ChronoField;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create a Instant object
        Instant instant
            = Instant.parse("2018-12-30T01:34:50.93Z");
 
        // try to find era using ChronoField
        try {
 
            int secondvalue
                = instant.get(ChronoField.ERA);
        }
        catch (Exception e) {
 
            // print exception
            System.out.println("Exception: " + e);
        }
    }
}
输出:
Exception:
 java.time.temporal.UnsupportedTemporalTypeException:
 Unsupported field: Era

参考资料: https: Java Java)