📌  相关文章
📜  带有示例的Java中的 HijrahDate from() 方法

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

带有示例的Java中的 HijrahDate from() 方法

Java.time.chrono.HijrahDate类的from()方法用于根据特定时间对象的回历系统获取回历日期。

句法:

public static HijrahDate from(
      TemporalAccessor temporal)

参数:此方法采用任何时间访问器的对象,在此基础上将形成 Hijrah 日期。

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

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

示例 1:

Java
// Java program to demonstrate
// from() 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
            // TemporalAccessor object
            ZonedDateTime zonedate
                = ZonedDateTime
                      .parse(
                          "2018-10-25T23:12:31."
                          + "123+02:00[Europe/Paris]");
  
            // Creating and initializing
            // HijrahDate Object
            HijrahDate hidate
                = HijrahDate.from(zonedate);
  
            // Display the result
            System.out.println("HijrahDate: "
                               + 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
// from() 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
            // TemporalAccessor object
            LocalDateTime localdate
                = LocalDateTime.parse(
                    "2018-12-30T19:34:50.63");
  
            // Creating and initializing
            // HijrahDate Object
            HijrahDate hidate
                = HijrahDate.from(localdate);
  
            // Display the result
            System.out.println("HijrahDate: "
                               + hidate);
        }
        catch (DateTimeException e) {
            System.out.println(
                "passed parameter can"
                + " not form a date");
            System.out.println(
                "Exception thrown: "
                + e);
        }
    }
}


输出:
HijrahDate: Hijrah-umalqura AH 1440-02-16

示例 2:

Java

// Java program to demonstrate
// from() 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
            // TemporalAccessor object
            LocalDateTime localdate
                = LocalDateTime.parse(
                    "2018-12-30T19:34:50.63");
  
            // Creating and initializing
            // HijrahDate Object
            HijrahDate hidate
                = HijrahDate.from(localdate);
  
            // Display the result
            System.out.println("HijrahDate: "
                               + hidate);
        }
        catch (DateTimeException e) {
            System.out.println(
                "passed parameter can"
                + " not form a date");
            System.out.println(
                "Exception thrown: "
                + e);
        }
    }
}
输出:
HijrahDate: Hijrah-umalqura AH 1440-04-23

参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/HijrahDate.html#from-java.time.temporal.TemporalAccessor-