Java 中的 hijrah 年表 dateNow(ZoneId)方法,示例

原文:https://www . geeksforgeeks . org/hijrah 年表-datenowzoneid-method-in-Java-with-example/

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

语法:

public HijrahDate dateNow(ZoneId zone)

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

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

以下是举例说明 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  HijrahDate Object
            HijrahDate hidate = HijrahDate.now();

            // getting HijrahChronology used in HijrahDate
            HijrahChronology crono = hidate.getChronology();

            // creating and initializing ZoneId object
            ZoneId id = ZoneId.systemDefault();

            // getting HijrahDate for the
            // given zoneid object
            // by using dateNow() method
            HijrahDate date = crono.dateNow(id);

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

输出:

HijrahDate is: Hijrah-umalqura AH 1441-05-27

例 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  HijrahDate Object
            HijrahDate hidate = HijrahDate.now();

            // getting HijrahChronology used in HijrahDate
            HijrahChronology crono = hidate.getChronology();

            // creating and initializing ZoneId object
            ZoneId id = ZoneId.of("Europe/Paris");
            ;

            // getting HijrahDate for the
            // given zoneid object
            // by using dateNow() method
            HijrahDate date = crono.dateNow(id);

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

输出:

HijrahDate is: Hijrah-umalqura AH 1441-05-27

参考:https://docs . Oracle . com/javase/9/docs/API/Java/time/chrono/HijRahEr 年表. html # dateNow-Java . time . zoneid-