■ 日付 / 時間 / 日時 Java8版
* java.time.LocalDateTime * java.time.LocalDate * java.time.LocalTime
フォーマッタ
* java.time.fomat.DateTimeFormatter
■ 日付 / 時間 / 日時あれこれ
加算/減算
* plusXxxx() の戻り値から取得する
サンプル
DateTimeFormatter datetimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); LocalDateTime currenyDateTime = LocalDateTime.now(); // ★注目★ LocalDateTime plusHourDateTime = currenyDateTime.plusHours(2L); LocalDateTime minusHourDateTime = currenyDateTime.plusHours(-2L); System.out.println(currenyDateTime.format(datetimeFormatter)); System.out.println(plusHourDateTime.format(datetimeFormatter)); System.out.println(minusHourDateTime.format(datetimeFormatter));
出力結果
2018/03/26 22:19:10 2018/03/27 00:19:10 2018/03/26 20:19:10
■ サンプル
import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; public class Main { public static void main(String[] args) { DateTimeFormatter datetimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); // 現在日時 LocalDateTime currenyDateTime = LocalDateTime.now(); System.out.println(currenyDateTime.format(datetimeFormatter)); // 内部にLocalDateとLocalTimeのインスタンスを持っている LocalDate currenyDate = currenyDateTime.toLocalDate(); LocalTime currenyTime = currenyDateTime.toLocalTime(); DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); System.out.println(currenyDate.format(dateFormatter)); DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); System.out.println(currenyTime.format(timeFormatter)); LocalDateTime targetDateTime1 = LocalDateTime.of(2018, 2, 24, 12, 11, 59); System.out.println(targetDateTime1.format(datetimeFormatter)); LocalDateTime targetDateTime2 = LocalDateTime.of(2018, 2, 24, 2, 13, 32, 0); System.out.println(targetDateTime2.format(datetimeFormatter)); LocalDateTime targetDateTime3 = LocalDateTime.parse("2018/02/24 01:10:19", datetimeFormatter); System.out.println(targetDateTime3.format(datetimeFormatter)); try { LocalDateTime targetDateTimeError = LocalDateTime.parse("2018/2/24 1:10:19", datetimeFormatter); System.out.println(targetDateTimeError.format(datetimeFormatter)); } catch (Exception ex) { ex.printStackTrace(); } DateTimeFormatter datetimeFormatterEx = DateTimeFormatter.ofPattern("yyyy/[]M/[]d []H:[]m:[]s"); LocalDateTime targetDateTimeOK = LocalDateTime.parse("2018/2/24 1:10:9", datetimeFormatterEx); System.out.println(targetDateTimeOK.format(datetimeFormatterEx)); // LocalDateTime -> Date LocalDateTime targetDate = LocalDateTime.of(2018, 2, 24, 12, 11, 59); Date date = Date.from(targetDate.atZone(ZoneId.systemDefault()).toInstant()); try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(simpleDateFormat.format(date)); } catch (Exception ex) { ex.printStackTrace(); } // Date -> LocalDateTime LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); System.out.println(localDateTime.format(datetimeFormatter)); } }
出力結果
2018/02/24 01:55:53 2018/02/24 01:55:53 2018/02/24 12:11:59 2018/02/24 02:13:32 2018/02/24 01:10:19 java.time.format.DateTimeParseException: Text '2018/2/24 1:10:19' could not be parsed at index 5 at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source) at java.time.format.DateTimeFormatter.parse(Unknown Source) at java.time.LocalDateTime.parse(Unknown Source) at com.sample.datetime.Main.main(Main.java:39) 2018/2/24 1:10:9 2018-02-24 12:11:59 2018/02/24 12:11:59
■ 注意
* 決まった形式ではない文字を使用してDateTimeFormatterにかけると 「IllegalArgumentException: Unknown pattern letter」で怒られる。
ダメな例
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { // ★「hello_world_yyyyMMdd_HHmmss.csv」の部分★ DateTimeFormatter datetimeFormatter = DateTimeFormatter.ofPattern("hello_world_yyyyMMdd_HHmmss.csv"); LocalDateTime currenyDateTime = LocalDateTime.now(); System.out.println(currenyDateTime.format(datetimeFormatter)); } }
エラー結果
Exception in thread "main" java.lang.IllegalArgumentException: Unknown pattern letter: l at java.time.format.DateTimeFormatterBuilder.parsePattern(Unknown Source) at java.time.format.DateTimeFormatterBuilder.appendPattern(Unknown Source) at java.time.format.DateTimeFormatter.ofPattern(Unknown Source) at com.sample.date.Main.main(Main.java:9)
参考文献
https://qiita.com/tag1216/items/91a471b33f383981bfaa
http://blog.y-yuki.net/entry/2016/09/15/003000
http://news.mynavi.jp/special/2014/java8/008.html
http://www.atmarkit.co.jp/ait/articles/1412/16/news041_3.html
パースエラー
https://qiita.com/ricoirico/items/ec6be9cd9f7ce6e88e8d
関連記事
日付・時間 について ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2014/11/18/235600
日付・時間 について ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2016/10/12/235711
日付・時間 について ~ System.currentTimeMillis / System.nanoTime ~
https://dk521123.hatenablog.com/entry/2017/04/25/223122