Java中的 JapaneseDate now(Clock) 方法和示例
Java.time.chrono.JapaneseDate类的now()方法用于从指定的时钟根据日本日历系统获取当前日本日期。
句法:
public static JapaneseDate now(Clock clock)
参数:此方法采用时钟对象,在此基础上将形成日本日期。
返回值:该方法根据日本日历系统从指定时钟返回当前日本日期。
下面是说明now()方法的示例:
示例 1:
Java
// Java program to demonstrate now() 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 clock
Clock clock = Clock.systemUTC();
// Creating and initializing
// JapaneseDate Object
JapaneseDate hidate
= JapaneseDate.now(clock);
// Display the result
System.out.println("JapaneseDate: "
+ 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 now() 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 clock
Clock clock = Clock.systemDefaultZone();
// Creating and initializing
// JapaneseDate Object
JapaneseDate hidate
= JapaneseDate.now(clock);
// Display the result
System.out.println("JapaneseDate: "
+ hidate);
}
catch (DateTimeException e) {
System.out.println("passed parameter can"
+ " not form a date");
System.out.println("Exception thrown: "
+ e);
}
}
}
输出:
JapaneseDate: Japanese Heisei 32-03-23
示例 2:
Java
// Java program to demonstrate now() 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 clock
Clock clock = Clock.systemDefaultZone();
// Creating and initializing
// JapaneseDate Object
JapaneseDate hidate
= JapaneseDate.now(clock);
// Display the result
System.out.println("JapaneseDate: "
+ hidate);
}
catch (DateTimeException e) {
System.out.println("passed parameter can"
+ " not form a date");
System.out.println("Exception thrown: "
+ e);
}
}
}
输出:
JapaneseDate: Japanese Heisei 32-03-23
参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/JapaneseDate.html#now-java.time.Clock-