Java中的 MinguoChronology dateNow(Clock) 方法与示例
Java.time.chrono.MinguoChronology类的dateNow()方法用于根据民国日历系统从指定的时钟对象中获取当前民国日期。
句法:
public MinguoDate dateNow(Clock clock)
参数:该方法以时钟对象为参数,用于根据民国日历系统从指定的时钟对象中获取当前民国日期。
返回值:该方法根据民国日历系统从指定的时钟对象中返回民国日期。
以下是说明dateNow()方法的示例:
示例 1:
// Java program to demonstrate
// MinguoNow() 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
// MinguoDate Object
MinguoDate hidate
= MinguoDate.now();
// getting MinguoChronology
// used in MinguoDate
MinguoChronology crono
= hidate.getChronology();
// creating and initializing
// clock object
Clock clock = Clock.systemUTC();
// getting MinguoDate for the
// given clock object
// by using dateNow() method
MinguoDate date
= crono.dateNow(clock);
// display the result
System.out.println(
"MinguoDate is: "
+ date);
}
catch (DateTimeException e) {
System.out.println(
"passed parameter can "
+ "not form a date");
System.out.println(
"Exception thrown: " + e);
}
}
}
输出:
MinguoDate is: Minguo ROC 109-04-15
示例 2:
// Java program to demonstrate
// MinguoNow() 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
// MinguoDate Object
MinguoDate hidate
= MinguoDate.now();
// getting MinguoChronology
// used in MinguoDate
MinguoChronology crono
= hidate.getChronology();
// creating and initializing
// clock object
Clock clock = Clock.systemUTC();
// setting clock
clock = Clock.tick(
clock,
Duration.ofDays(100));
// getting MinguoDate for the
// given clock object
// by using dateNow() method
MinguoDate date
= crono.dateNow(clock);
// display the result
System.out.println(
"MinguoDate is: "
+ date);
}
catch (DateTimeException e) {
System.out.println(
"passed parameter can "
+ "not form a date");
System.out.println(
"Exception thrown: " + e);
}
}
}
输出:
MinguoDate is: Minguo ROC 109-02-08
参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/MinguoChronology.html#dateNow-java.time.Clock-