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

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

Java中的 IsoChronology dateNow(Clock) 方法与示例

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

句法:

public LocalDate dateNow(Clock clock)

参数:该方法以时钟对象为参数。

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

以下是说明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 LocalDate Object
            LocalDate hidate = LocalDate.now();
  
            // getting IsoChronology used in localDate
            IsoChronology crono = hidate.getChronology();
  
            // creating and initializing clock object
            Clock clock = Clock.systemUTC();
  
            // getting LocalDate for the
            // given clock object
            // by using dateNow() method
            LocalDate date = crono.dateNow(clock);
  
            // display the result
            System.out.println("LocalDate is: " + date);
        }
        catch (DateTimeException e) {
            System.out.println("passed parameter can "
                               + "not form a date");
            System.out.println("Exception thrown: " + e);
        }
    }
}
输出:
LocalDate is: 2020-03-08

示例 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 LocalDate Object
            LocalDate hidate = LocalDate.now();
  
            // getting IsoChronology used in localDate
            IsoChronology crono = hidate.getChronology();
  
            // creating and initializing clock object
            Clock clock = Clock.systemUTC();
  
            // setting clock
            clock = Clock.tick(clock,
                               Duration.ofDays(100));
  
            // getting LocalDate for the
            // given clock object
            // by using dateNow() method
            LocalDate date = crono.dateNow(clock);
  
            // display the result
            System.out.println("LocalDate is: " + date);
        }
        catch (DateTimeException e) {
            System.out.println("passed parameter can "
                               + "not form a date");
            System.out.println("Exception thrown: " + e);
        }
    }
}
输出:
LocalDate is: 2020-02-08

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