Java中的 IsoChronology eraOf() 方法与示例
Java.time.chrono.IsoChronology类的eraOf()方法用于使用数值 0 和 1 检索 Iso Era,因为 IsoChronology 仅包含两个 HijrahEra。句法:
public IsoEra eraOf(int eraValue)
参数:此方法将eraValue整数作为要为其生成 IsoEra 的参数。返回值:此方法使用数值 0 和 1 返回Iso Era ,因为 IsoChronology 仅包含两个 HijrahEra 。下面是说明eraOf()方法的示例:示例 1:
Java
// Java program to demonstrate
// eraOf() 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 IsoEra for the
// given integer value
// by using eraOf() method
IsoEra era = crono.eraOf(0);
// display the result
System.out.println("IsoEra is: "
+ era);
}
catch (DateTimeException e) {
System.out.println("HijrahEra is invalid");
System.out.println("Exception thrown: " + e);
}
}
}
Java
// Java program to demonstrate
// eraOf() 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 IsoEra for the
// given integer value
// by using eraOf() method
IsoEra era = crono.eraOf(1);
// display the result
System.out.println("IsoEra is: "
+ era);
}
catch (DateTimeException e) {
System.out.println("HijrahEra is invalid");
System.out.println("Exception thrown: " + e);
}
}
}
输出:
IsoEra is: BCE
示例 2:
Java
// Java program to demonstrate
// eraOf() 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 IsoEra for the
// given integer value
// by using eraOf() method
IsoEra era = crono.eraOf(1);
// display the result
System.out.println("IsoEra is: "
+ era);
}
catch (DateTimeException e) {
System.out.println("HijrahEra is invalid");
System.out.println("Exception thrown: " + e);
}
}
}
输出:
IsoEra is: CE
参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/IsoChronology.html#eraOf-int-