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–