ホームページ  >  記事  >  Java  >  ジャワ時代

ジャワ時代

王林
王林オリジナル
2024-08-30 15:52:511220ブラウズ

Java の期間は、時間を年、月、日で測定するために使用されるクラスです。 Java の Period クラスのパッケージは java.time.Period です。 Period クラス オブジェクトは、期間を指定するか、年、月、日の 2 つの時間の差を決定するために使用されます。 Period オブジェクトは不変であり、Period オブジェクトが不変であるのと同様にスレッドセーフであるため、作成後にその値を変更することはできません。ただし、別の Period オブジェクトに基づいて新しい Period オブジェクトを作成することはできます。 Period クラスはオブジェクト クラスを継承し (オブジェクトは Java のすべてのクラスのスーパークラスであるため)、ChronoPeriod インターフェイスを実装します。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文:

Java での Period クラス宣言の構文は次のとおりです。

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

Java 期間を説明する方法

Period クラスのメソッドのリストをコード例とともに以下に説明します。サンプルコードは同様のメソッドにさらに使用できます (各メソッドのサンプルコードは示されていません):

  • long get(TemporalUnit Unit): 要求された単位の値を返します。
  • The static period between(LocalDate startInclusive, LocalDate endExclusive): Return Period オブジェクトは、年、月、日の 2 つの日付の間の期間です。

コード:

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(Temporal Temporal): Temporal オブジェクトとこの Period オブジェクトの加算である 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 period = Period.of(1,1,1);
LocalDateTime d = LocalDateTime.now();
System.out.println(d);
d = (LocalDateTime)period.addTo(d);
System.out.println(d);
}
}

出力:

ジャワ時代

  • booleanquals(Object otherPeriod): この期間を指定された期間でチェックし、ブール値を返します。
  • 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());
}
}

出力:

ジャワ時代

  • 期間マイナス(TemporalAmount amountToSubtract): この期間から指定された期間を減算した結果のオブジェクトを返します。
  • Period marginsDays(long daysToSubtract): 指定された Days で減算されたオブジェクトを返します。
  • 期間 Mindsmonths(long months): この期間の結果を指定された月で減算したオブジェクトを返します。
  • Period negativeyears(long years): 指定された年からこの期間を引いた結果であるオブジェクトを返します。
  • Period multipliedBy(long multiplicand): スカラーを乗算した期間オブジェクトを返します。
  • Period negated(): この期間から得られる長さを否定した期間オブジェクトを返します。
  • Periodnormalized(): この期間から得られる、年と月で正規化された期間オブジェクトを返します。
  • static period of(int years, int months, int days): 複数の年、月、日を表す Period オブジェクトを返します。
  • static Period ofDays(int days): 数日間の Period オブジェクトを返します。
  • static Period ofMonths(int months): 数か月の Period オブジェクトを返します。
  • static Period ofWeeks(int months): 数週間の Period オブジェクトを返します。
  • static Period ofyears(int years): 数週間の 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:Java 曜日次の記事:Java 曜日