Created
May 19, 2015 23:18
-
-
Save tag1216/3a50a69bf0a84af8ea55 to your computer and use it in GitHub Desktop.
Java8日時APIのちょっと特殊なクラスたち ref: http://qiita.com/tag1216/items/9cb33a39a6666983491d
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
Clock clock = Clock.fixed( | |
ZonedDateTime.of(2015, 12, 15, 23, 30, 59, 999999999, ZoneId.systemDefault()).toInstant(), | |
ZoneId.systemDefault()); | |
System.out.println(LocalDateTime.now(clock)); //2015-12-15T23: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
LocalDateTime from = LocalDateTime.of(2015, 12, 15, 0, 0); | |
LocalDateTime to = LocalDateTime.of(2015, 12, 16, 12, 30, 59); | |
Duration duration = Duration.between(from, to); | |
System.out.println(duration); //PT36H30M59S -> 36時間30分59秒という意味 | |
System.out.println(duration.toDays()); //1 | |
System.out.println(duration.toHours()); //36 | |
System.out.println(duration.toMinutes()); //2190 | |
System.out.println(duration.getSeconds()); //131459 |
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
LocalDate from = LocalDate.of(2014, 1, 1); | |
LocalDate to = LocalDate.of(2015, 3, 10); | |
Period period = Period.between(from, to); | |
System.out.println(period); // P1Y2M9D -> 1年2ヶ月9日の意味 | |
System.out.println(period.getYears()); // 1 | |
System.out.println(period.getMonths()); // 2 | |
System.out.println(period.getDays()); // 9 | |
System.out.println(period.toTotalMonths()); // 14 |
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
System.out.println(ChronoUnit.DAYS.between(from, to)); // 433 |
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
JanapeseDate d = JapaneseDate.now(); //現在時刻 | |
JapaneseDate d = JapaneseDate.of(2015, 12, 15); //西暦で指定 | |
JapaneseDate d = JapaneseDate.of(JapaneseEra.HEISEI, 27, 12, 15); //和暦で指定 |
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
System.out.println(JapaneseDate.of(2015, 12, 15)); | |
//Japanese Heisei 27-12-15 |
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("Gy年M月d日") | |
.withChronology(JapaneseChronology.INSTANCE); | |
JapaneseDate d = JapaneseDate.of(2015, 12, 15); | |
//書式化 | |
System.out.println(f.format(d)); //平成27年12月15日 | |
System.out.println(d.format(f)); //平成27年12月15日 | |
//パース | |
JapaneseDate d2 = JapaneseDate.from(f.parse("平成27年5月16日")); | |
System.out.println(LocalDate.from(d2)); //2015-05-16 |
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
Map<Long, String> map = new LinkedHashMap<>(); | |
map.put(1L, "冬"); | |
map.put(2L, "春"); | |
map.put(3L, "夏"); | |
map.put(4L, "秋"); | |
DateTimeFormatter season = new DateTimeFormatterBuilder() | |
.appendText(IsoFields.QUARTER_OF_YEAR, map) | |
.toFormatter(); | |
DateTimeFormatter f = new DateTimeFormatterBuilder() | |
.appendValue(ChronoField.YEAR) | |
.appendLiteral("年") | |
.append(season) | |
.toFormatter(); | |
Stream.iterate(YearMonth.of(2015, 1), ym -> ym.plusMonths(1)) | |
.limit(15) | |
.map(ym -> ym.toString() + " -> " + ym.format(f)) | |
.forEach(System.out::println); | |
//結果 | |
// 2015-01 -> 2015年冬 | |
// 2015-02 -> 2015年冬 | |
// 2015-03 -> 2015年冬 | |
// 2015-04 -> 2015年春 | |
// : | |
// 2016-03 -> 2016年冬 |
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
WeekFields wf = WeekFields.of(DayOfWeek.MONDAY, 4); | |
DateTimeFormatter f = new DateTimeFormatterBuilder() | |
.appendPattern("yyyy/MM/dd(E) ") | |
.appendLiteral("第") | |
.appendValue(wf.weekOfMonth()) | |
.appendLiteral("週") | |
.toFormatter(); | |
Stream.iterate(LocalDate.of(2015, 5, 1), d -> d.plusDays(1)) | |
.limit(10) | |
.map(d -> d.format(f)) | |
.forEach(System.out::println); | |
//2015/05/01(金) 第0週 | |
//2015/05/02(土) 第0週 | |
//2015/05/03(日) 第0週 | |
//2015/05/04(月) 第1週 | |
//2015/05/05(火) 第1週 | |
//2015/05/06(水) 第1週 | |
//2015/05/07(木) 第1週 | |
//2015/05/08(金) 第1週 | |
//2015/05/09(土) 第1週 | |
//2015/05/10(日) 第1週 |
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
//2015/05/01(金) 第1週 | |
//2015/05/02(土) 第1週 | |
//2015/05/03(日) 第1週 | |
//2015/05/04(月) 第2週 | |
//2015/05/05(火) 第2週 | |
//2015/05/06(水) 第2週 | |
//2015/05/07(木) 第2週 | |
//2015/05/08(金) 第2週 | |
//2015/05/09(土) 第2週 | |
//2015/05/10(日) 第2週 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment