MinguoDate of(int, int, int) Java方法及示例
Java.time.chrono.MinguoDate类的of()方法用于根据民国日历系统使用传递的预测年月日生成日期。
句法:
public static MinguoDate of(int Year,
int month, int dayOfMonth)
参数:此方法将以下参数作为参数:
- year:是年的整数值,代表民国日期中的年份字段。
- 月:是月的整数值,代表民国日期中的月份字段。
- day:是民国日期中代表day字段的day的整数值。
返回值:该方法根据民国历系统,使用传入的预知年月日,返回日期。
异常:如果传递的参数无法形成日期,则此方法抛出DateTimeException 。
以下是说明of()方法的示例:
示例 1:
Java
// Java program to demonstrate of() 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
// MinguoDate Object
// By using of() method
MinguoDate hidate
= MinguoDate.of(1444, 04, 24);
// Display the result
System.out.println("MinguoDate: "
+ 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 of() 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
// MinguoDate Object
// By using of() method
MinguoDate hidate
= MinguoDate.of(1911, 04, -24);
// Display the result
System.out.println("MinguoDate: "
+ hidate);
}
catch (DateTimeException e) {
System.out.println("passed parameter can"
+ " not form a date");
System.out.println("Exception thrown: "
+ e);
}
}
}
输出:
MinguoDate: Minguo ROC 1444-04-24
示例 2:
Java
// Java program to demonstrate of() 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
// MinguoDate Object
// By using of() method
MinguoDate hidate
= MinguoDate.of(1911, 04, -24);
// Display the result
System.out.println("MinguoDate: "
+ hidate);
}
catch (DateTimeException e) {
System.out.println("passed parameter can"
+ " not form a date");
System.out.println("Exception thrown: "
+ e);
}
}
}
输出:
passed parameter can not form a date
Exception thrown:
java.time.DateTimeException:
Invalid value for DayOfMonth (valid values 1 - 28/31): -24
参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/MinguoDate.html#of-int-int-int-