Last active
August 29, 2015 14:21
-
-
Save tag1216/17e262cf60e0a4f07a3e to your computer and use it in GitHub Desktop.
Java8の日時APIはとりあえずこれだけ覚えとけ ref: http://qiita.com/tag1216/items/91a471b33f383981bfaa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LocalDateTime d = LocalDateTime.now(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//年月日時分秒を指定 | |
LocalDateTime d1 = LocalDateTime.of(2015, 12, 15, 23, 30, 59); | |
//年月日時分秒と1秒未満のナノ秒を指定 | |
LocalDateTime d2 = LocalDateTime.of(2015, 12, 15, 23, 30, 59, 999999999); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Date -> *DateTime | |
Date date = new Date(); | |
ZonedDateTime zdt = date.toInstant().atZone(ZoneId.systemDefault()); | |
LocalDateTime ldt = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); | |
OffsetDateTime odt = OffsetDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); | |
//ZonedDateTime -> Date | |
ZonedDateTime zdt = ZonedDateTime.now(); | |
Date date = Date.from(zdt.toInstant()); | |
//OffsetDateTime -> Date | |
OffsetDateTime odt = OffsetDateTime.now(); | |
Date date = Date.from(odt.toInstant()); | |
//LocalDateTime -> Date | |
LocalDateTime ldt = LocalDateTime.now(); | |
Date date = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LocalDateTime d = LocalDateTime.now(); | |
System.out.println(d.getYear()); | |
System.out.println(d.getMonth()); | |
System.out.println(d.getDayOfMonth()); | |
System.out.println(d.getHour()); | |
System.out.println(d.getMinute()); | |
System.out.println(d.getSecond()); | |
System.out.println(d.getNano()); | |
System.out.println(d.get(ChronoField.YEAR)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LocalDateTime d = LocalDateTime.of(2015, 12, 15, 23, 30, 59); | |
System.out.println(d.plusDays(20)); //2016-01-04T23:30:59 | |
System.out.println(d.minusDays(20)); //2015-11-25T23:30:59 | |
System.out.println(d.withDayOfMonth(20)); //2015-12-20T23:30:59 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LocalDateTime.of(2015, 12, 15, 23, 30, 59).truncatedTo(ChronoUnit.HOURS); | |
// 2015-12-15T23:00 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LocalDateTime d = | |
LocalDateTime.now() | |
.plusMonths(1) | |
.withDayOfMonth(1) | |
.withHour(12) | |
.truncatedTo(ChronoUnit.HOURS); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ZoneId zoneId = ZoneId.systemDefault(); | |
ZoneId zoneId = ZoneId.of("Asia/Tokyo"); | |
ZoneOffset offset = ZoneOffset.UTC; | |
ZoneOffset offset = ZoneOffset.ofHours(9); | |
ZoneOffset offset = ZoneId.systemDefault().getRules().getOffset(Instant.now()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LocalDateTime ldt = LocalDateTime.now(); | |
//LocalDateTime -> ZoneDateTime | |
ZonedDateTime zdt = ldt.atZone(ZoneId.systemDefault()); | |
//LocalDateTime -> OffsetDateTime | |
OffsetDateTime odt = ldt.atOffset(ZoneOffset.ofHours(9)); | |
//LocalDateTime -> Instant | |
Instant instant = ldt.toInstant(ZoneOffset.UTC); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LocalDateTime.parse("2015-12-15T23:30"); | |
LocalDateTime.parse("2015-12-15T23:30:59"); | |
LocalDateTime.parse("2015-12-15T23:30:59.999"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); | |
LocalDateTime d = LocalDateTime.parse("2015/12/15 23:30:59", f); | |
System.out.println(d.format(f)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment