📜  Java的.time.Period类在Java中

📅  最后修改于: 2022-05-13 01:55:26.029000             🧑  作者: Mango

Java的.time.Period类在Java中

Java类中的Period类获取以年、月和日为单位的数量或时间量。获得的时间是 ISO-8601 日历系统中基于日期的时间量,例如“4 年、5 个月和 6 天”。一段时间内支持的单位是年、月和天。所有这三个字段始终存在,但可以设置为零。

语法:类声明

public final class Period 
extends Object 
implements ChronoPeriod, Serializable

下面所有方法及其执行的动作如下,以表格形式如下:

MethodDescription
addTo(Temporal temporal)This method adds this period to the specified temporal object.
between(LocalDate startDateInclusive, LocalDate endDateExclusive)This method obtains a Period consisting of the number of years, months, and days between two dates.
equals(Object obj)This method checks if this period is equal to another period.
from(TemporalAmount amount)This method obtains an instance of Period from a temporal amount.
get(TemporalUnit unit)This method gets the value of the requested unit.
getChronology()This method gets the chronology of this period, which is the ISO calendar system.
getDays()This method gets the number of days of this period.
getMonths()This method gets the number of months of this period.
getUnits()This method gets the set of units supported by this period.
getYears()This method gets the number of years of this period.
hashCode()This method returns a hash code for this period.
isNegative()This method checks if any of the three units of this period are negative.
isZero()This method checks if all three units of this period are zero.
minus(TemporalAmount amountToSubtract)This method returns a copy of this period with the specified period subtracted.
minusDays(long daysToSubtract)This method returns a copy of this period with the specified days subtracted.
minusMonths(long monthsToSubtract)This method returns a copy of this period with the specified months subtracted.
minusYears(long yearsToSubtract)This method returns a copy of this period with the specified years subtracted.
multipliedBy(int scalar)This method returns a new instance with each element in this period multiplied by the specified scalar.
negated()This method returns a new instance with each amount in this period negated.
normalized()This method returns a copy of this period with the years and months normalized.
of(int years, int months, int days)This method obtains a Period representing a number of years, months, and days.
ofDays(int days)This method obtains a Period representing a number of days.
ofMonths(int months)This method obtains a Period representing a number of months.
ofWeeks(int weeks)This method obtains a Period representing a number of weeks.
ofYears(int years)This method obtains a Period representing a number of years.
parse(CharSequence text)This method obtains a Period from a text string such as PnYnMnD.
plus(TemporalAmount amountToAdd)This method returns a copy of this period with the specified period added.
plusDays(long daysToAdd)This method returns a copy of this period with the specified days added.
plusMonths(long monthsToAdd)This method returns a copy of this period with the specified months added.
plusYears(long yearsToAdd)This method returns a copy of this period with the specified years added.
subtractFrom(Temporal temporal)This method subtracts this period from the specified temporal object.
toString()This method outputs this period as a String, such as P6Y3M1D.
toTotalMonths()This method gets the total number of months in this period.
withDays(int days)This method returns a copy of this period with the specified amount of days. 
withMonths(int months)This method returns a copy of this period with the specified amount of months.
withYears(int years)This method returns a copy of this period with the specified amount of years.

让我们实现这个类的一些方法。

执行:



示例 1

Java
// Java program to illustrate Period class
// demonstrate the methods of this class
// Methods - minus() and ofMonths()
  
// Importing Period class from
// java.time package
import java.time.Period;
  
// Main class
public class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Obtaining period representing noumer of months
        // using ofmonhs() method by
        // creating object of period class
        Period p1 = Period.ofMonths(6);
  
        // minus() will return a copy of this period
        // with the specified period subtracted.
        Period p2 = p1.minus(Period.ofMonths(2));
  
        // Print and display on the console
        System.out.println(p2);
    }
}


Java
// Java program to illustrate Period class
// demonstrate the methods of this class
// Methods like ofDays() and addTo()
  
// Importing all classes from java.time package
import java.time.*;
import java.time.temporal.Temporal;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Getting a period representing number of days
        // using ofDays() method
        Period p = Period.ofDays(24);
  
        // Adding this period to the
        // temporal object i.e. temp
        Temporal temp = p.addTo(LocalDate.now());
  
        // Print and display on the console
        System.out.println(temp);
    }
}


输出
P4M

让我们再举一个例子来讨论更多的方法,即如下:

方法一:该类的ofDays()方法用于从给定的天数中获取一个周期作为参数。

句法:

public static Period ofDays(int numberOfDays)

参数:此方法接受单个参数天数,即要解析为 Period 对象的天数。

返回:此函数返回周期,它是用给定天数解析的 Period 对象。

方法二:该类的addTo()方法将这个Period添加到具体的时间对象中。



句法:

public Temporal addTo(Temporal temporal)

参数:要调整的时间对象不应为空。

返回类型:进行了调整的相同类型的对象。

异常:此方法抛出 DateTime 和 Arithmetic 异常。

示例 2

Java

// Java program to illustrate Period class
// demonstrate the methods of this class
// Methods like ofDays() and addTo()
  
// Importing all classes from java.time package
import java.time.*;
import java.time.temporal.Temporal;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Getting a period representing number of days
        // using ofDays() method
        Period p = Period.ofDays(24);
  
        // Adding this period to the
        // temporal object i.e. temp
        Temporal temp = p.addTo(LocalDate.now());
  
        // Print and display on the console
        System.out.println(temp);
    }
}
输出
2021-03-29