Java 中的 Japanese 年表 dateYearDay(Era,int,int)方法,示例

原文:https://www . geeksforgeeks . org/Japanese 年表-datyeardayera-int-int-method-in-Java-with-example/

Java . time . chrono . Japanese 年表类的 dateYearDay() 方法用于根据特定时代的日本日历检索日本日期。

语法:

public JapaneseDate dateYearDay(
  Era era, int yearOfEra, int dayOfYear)

参数:该方法以下列参数为参数:

  • 时代:哪个是类型时代的对象
  • 年:是日本日期年字段的整数优先年
  • 日:是日本日期的日字段的整数日

返回值:此方法根据特定年份和日期的日本日历返回日本日期

异常:该方法抛出日期时间异常-如果给定的数据无法形成日期。

以下是举例说明 dateYearDay() 方法的例子:

例 1:

Java 语言(一种计算机语言,尤用于创建网站)

// Java program to demonstrate
// dateYearDay() 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
            // JapaneseDate Object
            JapaneseDate hidate
                = JapaneseDate.now();

            // getting JapaneseChronology
            // used in JapaneseDate
            JapaneseChronology crono
                = hidate.getChronology();

            // getting JapaneseDate for the
            // given date and year field
            // by using dateYearDay() method
            JapaneseDate date
                = crono.dateYearDay(
                    JapaneseEra.HEISEI,
                    2018,
                    24);

            // display the result
            System.out.println(
                "JapaneseDate is: "
                + date);
        }
        catch (DateTimeException e) {
            System.out.println(
                "passed parameter can "
                + "not form a date");
            System.out.println(
                "Exception thrown: " + e);
        }
    }
}

Output:

JapaneseDate is: Japanese Heisei 2018-01-24

例 2:

Java 语言(一种计算机语言,尤用于创建网站)

// Java program to demonstrate
// dateYearDay() 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
            // JapaneseDate Object
            JapaneseDate hidate
                = JapaneseDate.now();

            // getting JapaneseChronology
            // used in JapaneseDate
            JapaneseChronology crono
                = hidate.getChronology();

            // getting JapaneseDate for the
            // given date and year field
            // by using dateYearDay() method
            JapaneseDate date
                = crono.dateYearDay(
                    JapaneseEra.MEIJI,
                    2018,
                    24);

            // display the result
            System.out.println(
                "JapaneseDate is: "
                + date);
        }
        catch (DateTimeException e) {
            System.out.println(
                "passed parameter can "
                + "not form a date");
            System.out.println(
                "Exception thrown: " + e);
        }
    }
}

Output:

passed parameter can not form a date
Exception thrown:
 java.time.DateTimeException:
 Invalid parameters

参考:https://docs . Oracle . com/javase/9/docs/API/Java/time/chrono/Japanese seriogram . html # date year day-int-int-