Java Period クラス
PeriodPeriod クラスは Java 8 で導入された日時 API の一部であり、java.timeパッケージ内にあります。
Period クラスは主に2つの日付間の間隔を、年・月・日単位で表すために使用されます。一方、DurationDuration クラス(時間ベースの間隔(例:時、分、秒)を表す)とは異なり、Period日付レベルの差に焦点を当てています。
コア機能
1. 不変性
PeriodPeriod クラスは不変(immutable)です。つまり、一度Periodオブジェクトが生成されると、その値は変更できません。任意のPeriodPeriod の操作(例:加算・減算)は常に新しいPeriodインスタンスを返します。
2. 日付ベースの計算
Period年・月・日の計算を専門に処理し、誕生日や契約期間などのシナリオに適しています。
3. LocalDate との連携
Period通常はLocalDateLocalDate クラスと一緒に使用され、2つの日付間の差を計算したり、日付に期間を追加/削除したりします。
よく使用されるメソッド
Period オブジェクトの作成
1. 使用of()メソッド
例
Period period = Period.of(1, 2, 3); // 1年2か月3日
2. 使用between()メソッドで2つの日付の間隔を計算する
例
LocalDate startDate = LocalDate.of(2020, 1, 1);
LocalDate endDate = LocalDate.of(2021, 3, 4);
Period period = Period.between(startDate, endDate);
LocalDate endDate = LocalDate.of(2021, 3, 4);
Period period = Period.between(startDate, endDate);
メソッドで文字列から作成するparse()例
例
Period period = Period.parse("P1Y2M3D"); Period の各部分を取得する
Period の各部分を取得する
例
int years = period.getYears(); // 月数を取得する
int months = period.getMonths(); // 日数を取得する
int days = period.getDays(); 演算操作
int months = period.getMonths(); // 日数を取得する
int days = period.getDays(); 演算操作
演算操作
例
例
Period added = period.plusYears(1).plusMonths(2);
Period subtracted = period.minusDays(5);
Period subtracted = period.minusDays(5);
例
例
boolean isNegative = period.isNegative();
例
例
Period normalized = period.normalized(); 実際の応用例
実際の応用例
2つの日付の間隔を計算する
例
LocalDate birthDate = LocalDate.of(1990, 5, 15);
LocalDate currentDate = LocalDate.now();
Period age = Period.between(birthDate, currentDate);
System.out.printf(日付に Period を追加する,
age.getYears(), age.getMonths(), age.getDays());
LocalDate currentDate = LocalDate.now();
Period age = Period.between(birthDate, currentDate);
System.out.printf(日付に Period を追加する,
age.getYears(), age.getMonths(), age.getDays());
日付に Period を追加する
例
LocalDate today = LocalDate.now();
Period twoMonths = Period.ofMonths(2);
LocalDate futureDate = today.plus(twoMonths);
Period twoMonths = Period.ofMonths(2);
LocalDate futureDate = today.plus(twoMonths);
期間が空かどうかを確認する
例
Period zeroPeriod = Period.of(0, 0, 0);
if (zeroPeriod.isZero()) {
System.out.println(総合例);
}
if (zeroPeriod.isZero()) {
System.out.println(総合例);
}
総合例
例
import java.time.Period;
import java.time.LocalDate;
public class PeriodDemo {
public static void main(String[] args) {
// 1. of() メソッドを使用
// 1年2か月15日
Period period1 = Period.of(1, 2, 15); // 2. between() メソッドを使用して2つの日付間の期間を計算
System.out.println("Period 1: " + period1);
// 3. parse() メソッドを使用して文字列から作成
LocalDate startDate = LocalDate.of(2020, 1, 1);
LocalDate endDate = LocalDate.of(2023, 5, 15);
Period period2 = Period.between(startDate, endDate);
System.out.println("Period between dates: " + period2);
// ISO-8601形式
Period period3 = Period.parse("P1Y2M15D"); 出力結果は次のとおりです:
System.out.println("Parsed Period: " + period3);
}
}
import java.time.LocalDate;
public class PeriodDemo {
public static void main(String[] args) {
// 1. of() メソッドを使用
// 1年2か月15日
Period period1 = Period.of(1, 2, 15); // 2. between() メソッドを使用して2つの日付間の期間を計算
System.out.println("Period 1: " + period1);
// 3. parse() メソッドを使用して文字列から作成
LocalDate startDate = LocalDate.of(2020, 1, 1);
LocalDate endDate = LocalDate.of(2023, 5, 15);
Period period2 = Period.between(startDate, endDate);
System.out.println("Period between dates: " + period2);
// ISO-8601形式
Period period3 = Period.parse("P1Y2M15D"); 出力結果は次のとおりです:
System.out.println("Parsed Period: " + period3);
}
}
注意事項
Period 1: P1Y2M15D Period between dates: P3Y4M14D Parsed Period: P1Y2M15D
注意事項
- 年・月・日の差のみを計算し、時間(時、分など)の差は考慮しません。:
Period負の値の処理 - :終了日が開始日より前の場合、負の値が返されます。
Period.between()月の日数の違い - の計算では、月によって日数が異なることを考慮します(例:2月は28日または29日)。:
PeriodISO-8601 形式 - :使用メソッドを使用する場合、文字列は
parse()形式(例:"P1Y2M3D")に従う必要があります。PnYnMnDまとめ
まとめ
PeriodPeriod クラスの使用を習得すれば、日付処理コードをより簡潔で読みやすくすることができます。Periodその他の拡張