首頁  >  文章  >  Java  >  爪哇時期

爪哇時期

王林
王林原創
2024-08-30 15:52:511226瀏覽

java period 是一個用來測量年、月、日時間的類別。 java中Period類別的套件是java.time.Period。 period 類別物件指定時間段或用於確定年、月和日兩個時間之間的差異。 period 物件是不可變的且是線程安全的,就像Period 物件是不可變的一樣,因此一旦創建它我們就無法更改它的值。但是,我們可以基於另一個 period 物件建立新的 period 物件。 period 類別繼承一個物件類別(因為該物件是 java 中所有類別的超類別)並實作 ChronoPeriod 介面。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法:

java中Period類別宣告的語法如下:

public final class Period extends Object implements ChronoPeriod, Serializable
{
// variables and method of the class Period
}

解釋Java時期的方法

Period 類別方法清單在下面透過範例程式碼進行了解釋;範例程式碼可以進一步用於類似的方法(對於未給出的每個方法範例程式碼):

  • long get(TemporalUnit unit):傳回所請求單位的值。
  • 靜態週期(LocalDate startInclusive, LocalDate endExclusive):傳回Period物件是兩個日期之間的年、月、日數的週期。

代碼:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class PeriodClassDemo {
public static void main(String[] args) {
Period p = Period.between(LocalDate.ofYearDay(2017, 20), LocalDate.ofYearDay(2017, 30) );
System.out.println(p);
System.out.println(p.get(ChronoUnit.DAYS));
System.out.println(p.get(ChronoUnit.MONTHS));
System.out.println(p.get(ChronoUnit.YEARS));
}
}

輸出:

爪哇時期

  • Temporal addTo(Temporaltemporal):傳回Period對象,即temporal與thisPeriod物件相加。

代碼:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
public class PeriodClassDemo {
public static void main(String[] args) {
Period period = Period.of(1,1,1);
LocalDateTime d = LocalDateTime.now();
System.out.println(d);
d = (LocalDateTime)period.addTo(d);
System.out.println(d);
}
}

輸出:

爪哇時期

  • boolean equals(Object otherPeriod):檢查此Period與指定時間段並傳回布林值。
  • boolean isNegative():如果此週期為負數,則傳回 True。
  • boolean isZero():如果此週期為負數,則傳回 True。

代碼:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
public class PeriodClassDemo {
public static void main(String[] args) {
Period p1 = Period.of(1,1,1);
Period p2 = Period.of(10,5,2);
Period p3 = Period.of(10,5,2);
System.out.println(p1.equals(p2));
System.out.println(p2.equals(p3));
System.out.println(p2.isNegative());
System.out.println(p2.isZero());
}
}

輸出:

爪哇時期

  • static Duration from(TemporalAmount amount): 從時間量中取得週期的實例。

代碼:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
public class PeriodClassDemo {
public static void main(String[] args) {
Period p = Period.from(Period.of(10, 5, 2) );
System.out.println(p);
}
}

輸出:

爪哇時期

  • IsoChronology getChronology(): 傳回該時期的年表,屬於 ISO 日曆系統。
  • int getDays(): 返回週期(以天為單位)。
  • int getMonths(): 以月為單位返回週期。
  • 清單 getUnits(): 傳回此週期支援的單位集。
  • int hashCode(): 傳回此期間的雜湊碼。

代碼:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
public class PeriodClassDemo {
public static void main(String[] args) {
Period p = Period.from(Period.of(10, 5, 2) );
System.out.println(p.getChronology());
System.out.println(p.get(ChronoUnit.MONTHS));
System.out.println(p.getDays());
System.out.println(p.getMonths());
System.out.println(p.getYears());
System.out.println(p.getClass());
System.out.println(p.getUnits());
}
}

輸出:

爪哇時期

  • Period minus(TemporalAmount amountToSubtract): 傳回此週期減去指定週期所得到的物件。
  • Period minusDays(long daysToSubtract): 傳回指定天數減去的物件。
  • Period Mindsmonths(長月):傳回對象,該週期與指定月份相減的結果。
  • Period minusYears(longyears):傳回對象,它是指定年份減去這個週期的結果。
  • Period multipliedBy(long multiplicand): 傳回週期物件乘以標量。
  • Period neated(): 傳回一個週期對象,該物件是由該週期的長度取反而產生的。
  • Period normalized(): 傳回一個週期對象,該物件是由該週期產生的,並以年和月進行標準化。
  • 靜態週期(int 年、int 月、int 天): 傳回表示幾年、幾個月和幾天的 period 物件。
  • 靜態Period ofDays(int days):傳回幾天的Period物件。
  • 靜態Period ofMonths(int Months):傳回幾個月的Period物件。
  • 靜態Period ofWeeks(int week):傳回幾週的Period物件。
  • static period of Years(intyears):傳回幾週的Period物件。

代碼:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
public class PeriodClassDemo {
public static void main(String[] args) {
Period p1 = Period.of(10,5,2);
Period p2 = Period.of(20,5,2);
System.out.println(p1.getDays());
System.out.println(p2.getDays());
System.out.println(p1.minus(p2).getDays());
System.out.println(p1.minusDays(2).getDays());
System.out.println(p1.minusMonths(1).getDays());
System.out.println(p1.minusYears(1).getDays());
System.out.println(p1.multipliedBy(2).getDays());
System.out.println(p1.negated().getDays());
System.out.println(p1.normalized().getDays());
Period p3 = Period.ofDays(1);
System.out.println(p3.getDays( ));
Period p4 = Period.ofMonths(2);
System.out.println(p4.getMonths());
}
}

輸出:

爪哇時期

  • static Period parse(CharSequence text): Return Period object from a text, for example, PnYnMnD.
  • Period plus(TemporalAmount amountToAdd): Return the period object of this period with added the specified period.
  • Period plusDays(long daysToAdd ): Return a period object of this period with added the specified days.
  • Period plusMonths(long monthsToAdd): Return a period object of this period with added the specified months.
  • Period plusYears(long yearsToAdd): Return a period object of this period with added the specified years.

Code:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
public class PeriodClassDemo {
public static void main(String[] args) {
Period p1 = Period.of(10,5,2);
Period p2 = Period.parse("P1Y2M3D");
System.out.println(p2.getMonths());
Period p3 = p1.plus(Period.ofDays(5));
System.out.println(p3);
}
}

Output:

爪哇時期

  • Temporal subtractFrom(Temporal temporal): Return Subtraction of this period from a temporal object.
  • String toString(): Return this period in string representation, such as PT8H6M12.345S.
  • long toTotalMonths(): Return the total number of months in this period.
  • Period withDays(int days): Return a period object of this period with the specified amount of days.
  • Period withMonths(int months): Return a period object of this period with the specified amount of months.
  • Period withYears(int years): Return a period object of this period with the specified amount of Years.

Code:

import java.time.Period;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
public class PeriodClassDemo {
public static void main(String[] args) {
Period p1 = Period.of(10,5,2);
System.out.println(p1);
LocalDateTime d = LocalDateTime.now();
System.out.println(d);
d = (LocalDateTime)p1.subtractFrom(d);
System.out.println(d);
System.out.println(p1.toString());
System.out.println(p1.toTotalMonths());
System.out.println(p1.withDays(2));
System.out.println(p1.toString());
System.out.println(p1.withMonths(1));
System.out.println(p1.toString());
}
}

Output:

爪哇時期

Conclusion

The Period class is one of the built-in class in java, which is used to measure time in years, months, and days and add, subtract, and convert the period, or in simple words, the period class allows one to operate on day or month, or year period. The period class is available in java.time.Period package of java.

以上是爪哇時期的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java 星期幾下一篇:Java 星期幾