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

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

Java中的 JapaneseChronology dateNow(ZoneId) 方法与示例

Java.time.chrono.JapaneseChronology类的dateNow()方法用于从指定时区的系统时钟中获取根据日本日历系统的当前日本日期。

句法:

public JapaneseDate dateNow(ZoneId zone)

参数:该方法以zone id的对象为参数。

返回值:该方法从指定的时钟对象返回根据日本日历系统的日本日期。

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

示例 1:

// Java program to demonstrate
// dateNow() 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();
  
            // creating and initializing ZoneId object
            ZoneId id = ZoneId.systemDefault();
  
            // getting JapaneseDate for the
            // given zoneid object
            // by using dateNow() method
            JapaneseDate date = crono.dateNow(id);
  
            // 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);
        }
    }
}
输出:
JapaneseDate is: Japanese Heisei 32-04-13

示例 2:

// Java program to demonstrate
// dateNow() 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();
  
            // creating and initializing ZoneId object
            ZoneId id = ZoneId.of("Europe/Paris");
  
            // getting JapaneseDate for the
            // given zoneid object
            // by using dateNow() method
            JapaneseDate date = crono.dateNow(id);
  
            // 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);
        }
    }
}
输出:
JapaneseDate is: Japanese Heisei 32-04-13

参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/JapaneseChronology.html#dateNow-java.time.ZoneId-