この文書の現在のバージョンと選択したバージョンの差分を表示します。
| 両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
|
プログラミング:java:api:日時 [2018/06/10 13:38] sotoyama |
プログラミング:java:api:日時 [2018/06/10 14:00] (現在) sotoyama |
||
|---|---|---|---|
| ライン 66: | ライン 66: | ||
| - | ==== 時刻を表すクラス一覧 ==== | + | ==== 時刻を表すクラスと保持情報 ==== |
| ※Java8以降 | ※Java8以降 | ||
| - | ^ ^ ^ | + | ^ クラス ^ 情報 ||||| |
| - | | | | | + | | ::: ^ 年 ^ 月 ^ 日 ^ 時刻 ^ ゾーン ^ |
| + | | ZonedDateTime | 〇 | 〇 | 〇 | 〇 | 〇 | | ||
| + | | LocalDateTime | 〇 | 〇 | 〇 | 〇 | - | | ||
| + | | LocalDate | 〇 | 〇 | 〇 | - | - | | ||
| + | | LocalTime | - | - | - | 〇 | - | | ||
| + | | Year | 〇 | - | - | - | - | | ||
| + | | YearMonth | 〇 | 〇 | - | - | - | | ||
| + | | Month | - | 〇 | - | - | - | | ||
| + | | MonthDay | - | 〇 | 〇 | - | - | | ||
| + | |||
| + | |||
| + | |||
| + | ==== 時間の計算 ==== | ||
| + | |||
| + | === Periodクラス === | ||
| + | |||
| + | <code java> | ||
| + | import java.time.*; | ||
| + | import java.time.format.* | ||
| + | |||
| + | ・・・ | ||
| + | |||
| + | LocalDate from = LocalDate.of(2017, 4, 1); | ||
| + | LocalDate to = LocalDate.of(2018, 6, 10); | ||
| + | |||
| + | Period period = Period.between(from, to); | ||
| + | |||
| + | System.out.println(period); | ||
| + | System.out.println(period.getYears()); | ||
| + | System.out.println(period.getMonths()); | ||
| + | System.out.println(period.getDays()); | ||
| + | System.out.println(period.toTotalMonths()); | ||
| + | </code> | ||
| + | |||
| + | <code prompt> | ||
| + | P1Y2M9D ※1年2か月9日の意味 | ||
| + | 1 | ||
| + | 2 | ||
| + | 9 | ||
| + | 14 | ||
| + | </code> | ||
| + | |||
| + | === Durationクラス === | ||
| + | |||
| + | <code java> | ||
| + | import java.time.*; | ||
| + | |||
| + | ・・・ | ||
| + | |||
| + | LocalDateTime from = LocalDateTime.of(2018, 6, 10, 10, 0, 0); | ||
| + | LocalDateTime to = LocalDateTime.of(2018, 6, 10, 23, 59, 59); | ||
| + | |||
| + | Duration duration = Duration.between(from, to); | ||
| + | |||
| + | System.out.println(duration); | ||
| + | System.out.println(duration.toDays()); | ||
| + | System.out.println(duration.toHours()); | ||
| + | System.out.println(duration.toMinutes()); | ||
| + | System.out.println(duration.getSeconds()); | ||
| + | </code> | ||
| + | |||
| + | <code prompt> | ||
| + | PT13H59M59S ※13時間59分59秒という意味 | ||
| + | 0 | ||
| + | 13 | ||
| + | 839 | ||
| + | 50399 | ||
| + | </code> | ||