📜  Java中的 JapaneseDate of(JapaneseEra,int, int, int) 方法与示例

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

Java中的 JapaneseDate of(JapaneseEra,int, int, int) 方法与示例

Java.time.chrono.JapaneseDate类的of()方法用于使用传入的预测年、月、日和JapaneseEra,根据日本日历系统生成日期。

句法:

public static JapaneseDate of(JapaneseEra era,
                              int yearOfEra,
                              int month,
                              int dayOfMonth)

参数:此方法将以下参数作为参数:

  • 时代:日本时代的对象,代表日本日期中的时代领域。
  • year:表示日语日期中年份字段的年份整数值。
  • 月:是月的整数值,表示日文日期中的月份字段。
  • 日:日的整数值,表示日文中的日字段。

返回值:该方法使用传入的预知年、月、日、纪元,按照日本历法系统返回日期

异常:如果传递的参数无法形成日期,则此方法抛出DateTimeException

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

示例 1:

Java
// Java program to demonstrate of() method
  
import java.util.*;
import java.io.*;
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
  
            // Creating and initializing
            // JapaneseDate Object
            // By using of() method
            JapaneseDate hidate
                = JapaneseDate.of(
                    JapaneseEra.HEISEI,
                    2008, 04, 24);
  
            // Display the result
            System.out.println("JapaneseDate: "
                               + hidate);
        }
        catch (DateTimeException e) {
            System.out.println("passed parameter can"
                               + " not form a date");
            System.out.println("Exception thrown: "
                               + e);
        }
    }
}


Java
// Java program to demonstrate of() method
  
import java.util.*;
import java.io.*;
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
  
            // Creating and initializing
            // JapaneseDate Object
            // By using of() method
            JapaneseDate hidate
                = JapaneseDate.of(
                    JapaneseEra.TAISHO,
                    2008, 04, 24);
  
            // Display the result
            System.out.println("JapaneseDate: "
                               + hidate);
        }
        catch (DateTimeException e) {
            System.out.println("passed parameter can"
                               + " not form a date");
            System.out.println("Exception thrown: "
                               + e);
        }
    }
}


输出:
JapaneseDate: Japanese Heisei 2008-04-24

示例 2:

Java

// Java program to demonstrate of() method
  
import java.util.*;
import java.io.*;
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
  
            // Creating and initializing
            // JapaneseDate Object
            // By using of() method
            JapaneseDate hidate
                = JapaneseDate.of(
                    JapaneseEra.TAISHO,
                    2008, 04, 24);
  
            // Display the result
            System.out.println("JapaneseDate: "
                               + hidate);
        }
        catch (DateTimeException e) {
            System.out.println("passed parameter can"
                               + " not form a date");
            System.out.println("Exception thrown: "
                               + e);
        }
    }
}
输出:
passed parameter can not form a date
Exception thrown: java.time.DateTimeException: year, month, and day not valid for Era

参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/JapaneseDate.html#of-java.time.chrono.JapaneseEra-int-int-int-