Javaで面倒くさい日付の扱いを簡単にするライブラリ「Joda-Time」 期間編
Joda-Time日付編に続き期間編です。
1記事にまとめようとしましたが、長くなりそうなので分割しました。
日付編はこちらです。shinsuke789.hatenablog.jp
日付処理では時々必要になる期間の操作についてまとめています。
日付の差分や2つの期間の差分を取得することができます。
触っていると各クラスの違いが微妙にわかりにくい点があったので、現時点で使えそうな機能のみ上げています。
Jodaの期間クラス
クラス名 | 機能 |
---|---|
Duration | 日時から日時分秒の差を扱うクラス |
Period | 単純に年月日時分秒毎に日時の差を扱うクラス |
Interval | 2つの期間の差を扱うクラス |
使い方
Duration
// 1日違いで日時を定義 DateTime start = new DateTime("2013-08-01T12:30:50.666"); DateTime end = new DateTime("2013-08-02T12:30:50.666"); // Durationで期間を定義 Duration duration = new Duration(start, end); // --- 期間の差を取得 // ミリ秒で取得 => 86400000 duration.getMilles(); // 日数で取得 => 1 duration.getStandardDays(); // 時数で取得 => 24 duration.getStandardHours(); // 分数で取得 => 1440 duration.getStandardMinutes(); // 秒数で取得 => 86400 duration.getStandardSeconds();
Period
// 日時の定義 DateTime start = new DateTime("2013-08-01T12:30:50.666"); DateTime end = new DateTime("2014-09-08T23:40:00.999"); // Periodで期間を定義 Period period = new Period(start, end); // --- 単純にそれぞれの差を取得する // 年 => 1 p.getYears(); // 月 => 1 p.getMonths(); // 週 => 1 p.getWeeks(); // 日(1周間内の日数) => 0 p.getDays(); // 時 => 11 p.getHours(); // 分 => 9 p.getMinutes(); // 秒 => 10 p.getSeconds(); // ミリ秒 => 333 p.getMillis();
Interval
// 日時と期間の定義1 DateTime start1 = new DateTime("2013-08-01T12:30:30.100"); DateTime end1 = new DateTime("2013-08-10T14:30:50.300"); Interval interval1 = new Interval(start1, end1); // 日時と期間の定義2 DateTime start2 = new DateTime("2013-08-10T12:30:30.100"); DateTime end2 = new DateTime("2013-08-20T14:30:50.300"); Interval interval2 = new Interval(start2, end2); // 2つの期間が連続するか? => true interval1.abuts(interval2); // 2つの期間の差を期間で取得 interval1.gaps(interval2); // 2つの期間の重なった期間を取得 interval1.overlap(interval2); // 期間内に指定の日付が含まれるか => true interval1.contains(new DateTime("2013-08-15")); // 期間内に指定の期間が含まれるか => true interval1.contains(interval2); // 期間内に現在が含まれているか => true interval1.containsNow(); // Durationに変換 interval1.toDuration(); // Periodに変換 interval1.toPeriod();
Intervalのcontains、overlapの時間の有効範囲
有効範囲が微妙に異なるようなのでJodaのJavadocから抜粋しました。
・containsの引数にDateTimeのときの有効範囲
[09:00 to 10:00) contains 08:59 = false (before start)
[09:00 to 10:00) contains 09:00 = true
[09:00 to 10:00) contains 09:59 = true
[09:00 to 10:00) contains 10:00 = false (equals end)
[09:00 to 10:00) contains 10:01 = false (after end)[14:00 to 14:00) contains 14:00 = false (zero duration contains nothing)
・containsの引数にIntervalのときの有効範囲
[09:00 to 10:00) contains [09:00 to 10:00) = true
[09:00 to 10:00) contains [09:00 to 09:30) = true
[09:00 to 10:00) contains [09:30 to 10:00) = true
[09:00 to 10:00) contains [09:15 to 09:45) = true
[09:00 to 10:00) contains [09:00 to 09:00) = true[09:00 to 10:00) contains [08:59 to 10:00) = false (otherStart before thisStart)
[09:00 to 10:00) contains [09:00 to 10:01) = false (otherEnd after thisEnd)
[09:00 to 10:00) contains [10:00 to 10:00) = false (otherStart equals thisEnd)[14:00 to 14:00) contains [14:00 to 14:00) = false (zero duration contains nothing)
・overlapの有効範囲
[09:00 to 10:00) overlaps [08:00 to 08:30) = false (completely before)
[09:00 to 10:00) overlaps [08:00 to 09:00) = false (abuts before)
[09:00 to 10:00) overlaps [08:00 to 09:30) = true
[09:00 to 10:00) overlaps [08:00 to 10:00) = true
[09:00 to 10:00) overlaps [08:00 to 11:00) = true[09:00 to 10:00) overlaps [09:00 to 09:00) = false (abuts before)
[09:00 to 10:00) overlaps [09:00 to 09:30) = true
[09:00 to 10:00) overlaps [09:00 to 10:00) = true
[09:00 to 10:00) overlaps [09:00 to 11:00) = true[09:00 to 10:00) overlaps [09:30 to 09:30) = true
[09:00 to 10:00) overlaps [09:30 to 10:00) = true
[09:00 to 10:00) overlaps [09:30 to 11:00) = true[09:00 to 10:00) overlaps [10:00 to 10:00) = false (abuts after)
[09:00 to 10:00) overlaps [10:00 to 11:00) = false (abuts after)[09:00 to 10:00) overlaps [10:30 to 11:00) = false (completely after)
[14:00 to 14:00) overlaps [14:00 to 14:00) = false (abuts before and after)
[14:00 to 14:00) overlaps [13:00 to 15:00) = true
Javaライブラリの解説を含む本
- 作者: 竹添直樹,島本多可子,小津美夕紀,亀井隆司
- 出版社/メーカー: 翔泳社
- 発売日: 2011/07/16
- メディア: 大型本
- 購入: 6人 クリック: 217回
- この商品を含むブログ (25件) を見る