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

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

Java中的 JapaneseChronology dateEpochDay() 方法与示例

Java.time.chrono.JapaneseChronology类的dateEpochDay()方法用于从大纪元获取根据日本日历系统的日本日期。
句法:

public JapaneseDate dateEpochDay(long epochDay)

参数:该方法以long类型的epochDay作为参数。
返回值:此方法根据日本日历系统从另一个 TemporalAccessor 对象返回本地日期。
异常:如果此纪元日无法创建日本日期,此方法将引发DateTimeException
以下是说明dateEpochDay()方法的示例:
示例 1:

Java
// Java program to demonstrate
// dateEpochDay() 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();
 
            // display the result
            System.out.println("current JapaneseDate is: "
                               + hidate);
 
            // getting JapaneseDate for the
            // given TemporalAccessor object
            // by using date() method
            hidate = crono.dateEpochDay(23456);
 
            // display the result
            System.out.println("\nJapaneseDate(according "
                               + "to ephochday) is: "
                               + 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
// dateEpochDay() 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();
 
            // display the result
            System.out.println("current JapaneseDate is: "
                               + hidate);
 
            // getting JapaneseDate for the
            // given TemporalAccessor object
            // by using date() method
            hidate = crono.dateEpochDay(-999999999);
 
            // display the result
            System.out.println("\nJapaneseDate(according "
                               + "to ephochday) is: "
                               + hidate);
        }
        catch (DateTimeException e) {
            System.out.println("passed parameter can"
                               + " not form a date");
            System.out.println("Exception thrown: " + e);
        }
    }
}


输出
current JapaneseDate is: Japanese Reiwa 3-06-22

JapaneseDate(according to ephochday) is: Japanese Reiwa 16-03-22

示例 2:

Java

// Java program to demonstrate
// dateEpochDay() 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();
 
            // display the result
            System.out.println("current JapaneseDate is: "
                               + hidate);
 
            // getting JapaneseDate for the
            // given TemporalAccessor object
            // by using date() method
            hidate = crono.dateEpochDay(-999999999);
 
            // display the result
            System.out.println("\nJapaneseDate(according "
                               + "to ephochday) is: "
                               + hidate);
        }
        catch (DateTimeException e) {
            System.out.println("passed parameter can"
                               + " not form a date");
            System.out.println("Exception thrown: " + e);
        }
    }
}
输出:
current JapaneseDate is: Japanese Heisei 32-04-13
passed parameter can not form a date
Exception thrown: java.time.DateTimeException: JapaneseDate before Meiji 6 is not supported

参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/JapaneseChronology.html#dateEpochDay-long-