Java 8 - 期間と期間の例

Java 8の Duration 、` Period`、 `ChronoUnit`オブジェクトを使って日付の違いを調べる方法を示す例はほとんどありません。

  1. 継続時間 - 秒とナノ秒単位の時間を測定します.

  2. 期間 - 年、月、日で時間を測定します.

1.継続時間の例

2つの LocalDateTime`の差秒数を調べる java.time.Duration`の例

DurationExample.java

package com.mkyong.time;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoUnit;

public class DurationExample {

    public static void main(String[]args) {

       //Creating Durations
        System.out.println("--- Examples --- ");

        Duration oneHours = Duration.ofHours(1);
        System.out.println(oneHours.getSeconds() + " seconds");

        Duration oneHours2 = Duration.of(1, ChronoUnit.HOURS);
        System.out.println(oneHours2.getSeconds() + " seconds");

       //Test Duration.between
        System.out.println("\n--- Duration.between --- ");

        LocalDateTime oldDate = LocalDateTime.of(2016, Month.AUGUST, 31, 10, 20, 55);
        LocalDateTime newDate = LocalDateTime.of(2016, Month.NOVEMBER, 9, 10, 21, 56);

        System.out.println(oldDate);
        System.out.println(newDate);

       //count seconds between dates
        Duration duration = Duration.between(oldDate, newDate);

        System.out.println(duration.getSeconds() + " seconds");

    }
}

出力

--- Examples ---

3600秒3600秒

--- Duration.between ---
2016-08-31T10:20:55
2016-11-09T10:21:56
6048061 seconds

2.期間の例

2つの LocalDates`の間で(年、月、日)別々に調べる java.time.Period`の例

PeriodExample.java

package com.mkyong.time;

import java.time.LocalDate;
import java.time.Month;
import java.time.Period;

public class PeriodExample {

    public static void main(String[]args) {

        System.out.println("--- Examples --- ");

        Period tenDays = Period.ofDays(10);
        System.out.println(tenDays.getDays());//10

        Period oneYearTwoMonthsThreeDays = Period.of(1, 2, 3);
        System.out.println(oneYearTwoMonthsThreeDays.getYears());  //1
        System.out.println(oneYearTwoMonthsThreeDays.getMonths()); //2
        System.out.println(oneYearTwoMonthsThreeDays.getDays());   //3

        System.out.println("\n--- Period.between --- ");
        LocalDate oldDate = LocalDate.of(1982, Month.AUGUST, 31);
        LocalDate newDate = LocalDate.of(2016, Month.NOVEMBER, 9);

        System.out.println(oldDate);
        System.out.println(newDate);

       //check period between dates
        Period period = Period.between(oldDate, newDate);

        System.out.print(period.getYears() + " years,");
        System.out.print(period.getMonths() + " months,");
        System.out.print(period.getDays() + " days");

    }
}

出力

--- Examples ---

10 1 2 3

--- Period.between ---
1982-08-31
2016-11-09
34 years,2 months,9 days

3. ChronoUnitの例

あるいは、 `ChronoUnit。{unit} .between`を使って日付の違いを調べることができます。次の例を見てください:

ChronoUnitExample.java

package com.mkyong.time;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoUnit;

public class ChronoUnitExample {

    public static void main(String[]args) {

        LocalDateTime oldDate = LocalDateTime.of(1982, Month.AUGUST, 31, 10, 20, 55);
        LocalDateTime newDate = LocalDateTime.of(2016, Month.NOVEMBER, 9, 10, 21, 56);

        System.out.println(oldDate);
        System.out.println(newDate);

       //count between dates
        long years = ChronoUnit.YEARS.between(oldDate, newDate);
        long months = ChronoUnit.MONTHS.between(oldDate, newDate);
        long weeks = ChronoUnit.WEEKS.between(oldDate, newDate);
        long days = ChronoUnit.DAYS.between(oldDate, newDate);
        long hours = ChronoUnit.HOURS.between(oldDate, newDate);
        long minutes = ChronoUnit.MINUTES.between(oldDate, newDate);
        long seconds = ChronoUnit.SECONDS.between(oldDate, newDate);
        long milis = ChronoUnit.MILLIS.between(oldDate, newDate);
        long nano = ChronoUnit.NANOS.between(oldDate, newDate);

        System.out.println("\n--- Total --- ");
        System.out.println(years + " years");
        System.out.println(months + " months");
        System.out.println(weeks + " weeks");
        System.out.println(days + " days");
        System.out.println(hours + " hours");
        System.out.println(minutes + " minutes");
        System.out.println(seconds + " seconds");
        System.out.println(milis + " milis");
        System.out.println(nano + " nano");

    }
}

出力

1982-08-31T10:20:55
2016-11-09T10:21:56

--- Total ---

34歳
410ヶ月
1784週間
12489日
299736時間
17984161分
1079049661秒
1079049661000ミリス
1079049661000000000ナノ

===参考文献