この文書の現在のバージョンと選択したバージョンの差分を表示します。
| 両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
|
プログラミング:java:api:日時 [2018/06/10 05:35] sotoyama |
プログラミング:java:api:日時 [2018/06/10 14:00] (現在) sotoyama |
||
|---|---|---|---|
| ライン 4: | ライン 4: | ||
| === Dateクラス === | === Dateクラス === | ||
| + | |||
| <code java> | <code java> | ||
| import java.util.Date; | import java.util.Date; | ||
| + | import java.text.SimpleDateFormat; | ||
| ・・・ | ・・・ | ||
| ライン 11: | ライン 13: | ||
| Date now = new Date(); | Date now = new Date(); | ||
| System.out.println(now); | System.out.println(now); | ||
| + | |||
| + | SimpleDateFormat sdf = new SimpleDateFormat("yyyy'/'MM'/'dd' 'HH':'mm':'ss"); | ||
| + | System.out.println(sdf.format(now)); | ||
| </code> | </code> | ||
| - | <code class="prompt"> | + | <code prompt> |
| - | # ああああ | + | Sun Jun 10 15:16:09 JST 2018 |
| + | 2018/06/10 15:16:09 | ||
| </code> | </code> | ||
| + | === Instantクラス、ZonedDateTimeクラス === | ||
| + | |||
| + | ※Java8以降 | ||
| - | === 特定の日時取得 === | ||
| <code java> | <code java> | ||
| - | import java.util.Calendar; | + | import java.time.*; |
| - | import java.util.Date; | + | |
| ・・・ | ・・・ | ||
| - | Calendar calendar = Calendar.getInstance(); | + | Instant i = Instant.now(); |
| - | calendar.set(2018, 6, 10, 13, 10, 0); // 2018/06/10 13:10:00 | + | ZonedDateTime z1 = i.atZone(ZoneId.of("Asia/Tokyo")); |
| - | Date date = calendar.getTime(); | + | ZonedDateTime z2 = ZonedDateTime.now(); |
| + | |||
| + | System.out.println(z1); | ||
| + | System.out.println(z2); | ||
| </code> | </code> | ||
| + | <code prompt> | ||
| + | 2018-06-10T22:33:08.600+09:00[Asia/Tokyo] | ||
| + | 2018-06-10T22:33:08.728+09:00[Asia/Tokyo] | ||
| + | </code> | ||
| + | |||
| + | === LocalDateTimeクラス === | ||
| + | |||
| + | ※Java8以降 | ||
| + | |||
| + | <code java> | ||
| + | import java.time.*; | ||
| + | |||
| + | ・・・ | ||
| + | |||
| + | LocalDateTime t = LocalDateTime.now(); | ||
| + | |||
| + | System.out.println(t); | ||
| + | </code> | ||
| + | |||
| + | <code prompt> | ||
| + | 2018-06-10T22:36:11.303 | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ==== 時刻を表すクラスと保持情報 ==== | ||
| + | |||
| + | ※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> | ||