Java ChronoPeriod negated()方法及示例
Java中 ChronoPeriod接口 的 normalized() 方法用于在规范化年份和月份后返回ChronoPeriod的一个新实例。
语法
ChronoPeriod normalized()
参数: 该函数不接受任何参数。
返回值: 该函数在对年份和月份进行规范化处理后返回一个新的ChronoPeriod实例。
异常: 它抛出一个 ArithmeticException。 如果发生数字溢出,这个异常将被捕获。
下面的程序说明了上述方法。
程序1 :
// Java code to show the function to normalize// months and years of the period import java.time.*;import java.time.chrono.*;import java.time.temporal.ChronoUnit; public class ChronoPeriodClass { // Function to normalize given periods static void toNormalize(ChronoPeriod p1) { System.out.println(p1.normalized()); } // Driver Code public static void main(String[] args) { // Defining period int year = 4; int months = 15; int days = 10; ChronoPeriod p1 = Period.of(year, months, days); toNormalize(p1); }}
输出:
P5Y3M10D
程序2 :这不会使天数正常化。
// Java code to show the function to normalize// months and years of the period import java.time.*;import java.time.chrono.*;import java.time.temporal.ChronoUnit; public class ChronoPeriodClass { // Function to normalize given periods static void toNormalize(ChronoPeriod p1) { System.out.println(p1.normalized()); } // Driver Code public static void main(String[] args) { // Defining period int year = 10; int months = 25; int days = 366; ChronoPeriod p1 = Period.of(year, months, days); toNormalize(p1); }}输出:
P12Y1M366D
参考资料 : https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoPeriod.html#normalized-
