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 }
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)); } }
輸出:
代碼:
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); } }
輸出:
代碼:
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()); } }
輸出:
代碼:
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); } }
輸出:
代碼:
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()); } }
輸出:
代碼:
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()); } }
輸出:
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:
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:
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中文網其他相關文章!