📌  相关文章
📜  Java中的 IsoChronology eras() 方法与示例

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

Java中的 IsoChronology eras() 方法与示例

Java.time.chrono.IsoChronology类的 eras ()方法用于检索此特定 Iso 年表下的所有时代。

句法:

public List eras()

参数:此方法不接受任何参数作为参数。

返回值:此方法返回此特定 Iso 年表下的所有时代。

以下是说明eras()方法的示例:

示例 1:

// Java program to demonstrate
// eras() method
  
import java.util.*;
import java.io.*;
import java.time.*;
import java.time.chrono.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating and initializing
            // LocalDate Object
            LocalDate hidate = LocalDate.now();
  
            // getting IsoChronology
            // used in LocalDate
            IsoChronology crono
                = hidate.getChronology();
  
            // getting all IsoEras present
            // by using eraOf() method
            List list = crono.eras();
  
            // display the result
            System.out.println("IsoEra is: "
                               + (list.iterator()).next());
        }
        catch (DateTimeException e) {
            System.out.println("IsoEra is invalid");
            System.out.println("Exception thrown: " + e);
        }
    }
}
输出:
IsoEra is: BCE

示例 2:

// Java program to demonstrate
// eras() method
  
import java.util.*;
import java.io.*;
import java.time.*;
import java.time.chrono.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating and initializing
            // LocalDate Object
            LocalDate hidate = LocalDate.now();
  
            // getting IsoChronology
            // used in LocalDate
            IsoChronology crono
                = hidate.getChronology();
  
            // getting all IsoEras present
            // by using eraOf() method
            List list = crono.eras();
  
            // getting list Iterator
            Iterator iter = list.iterator();
  
            // display the result
            System.out.println("List of IsoEra : "
                               + "\n"
                               + iter.next() + "\n"
                               + iter.next());
        }
        catch (DateTimeException e) {
            System.out.println("IsoEra is invalid");
            System.out.println("Exception thrown: " + e);
        }
    }
}
输出:
List of IsoEra : 
BCE
CE

参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/IsoChronology.html#eras–