|
| 1 | +package com.brianway.learning.java8.effective.time; |
| 2 | + |
| 3 | +import static java.time.temporal.TemporalAdjusters.lastDayOfMonth; |
| 4 | +import static java.time.temporal.TemporalAdjusters.nextOrSame; |
| 5 | + |
| 6 | +import java.text.DateFormat; |
| 7 | +import java.text.SimpleDateFormat; |
| 8 | +import java.time.DayOfWeek; |
| 9 | +import java.time.Duration; |
| 10 | +import java.time.Instant; |
| 11 | +import java.time.LocalDate; |
| 12 | +import java.time.LocalDateTime; |
| 13 | +import java.time.LocalTime; |
| 14 | +import java.time.Month; |
| 15 | +import java.time.chrono.JapaneseDate; |
| 16 | +import java.time.format.DateTimeFormatter; |
| 17 | +import java.time.format.DateTimeFormatterBuilder; |
| 18 | +import java.time.temporal.ChronoField; |
| 19 | +import java.time.temporal.ChronoUnit; |
| 20 | +import java.time.temporal.Temporal; |
| 21 | +import java.time.temporal.TemporalAdjuster; |
| 22 | +import java.util.Calendar; |
| 23 | +import java.util.Date; |
| 24 | +import java.util.Locale; |
| 25 | + |
| 26 | +/** |
| 27 | + * 新的时间和日期 API 示例 |
| 28 | + */ |
| 29 | +public class DateTimeExamples { |
| 30 | + |
| 31 | + private static final ThreadLocal<DateFormat> formatters = new ThreadLocal<DateFormat>() { |
| 32 | + protected DateFormat initialValue() { |
| 33 | + return new SimpleDateFormat("dd-MMM-yyyy"); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + public static void main(String[] args) { |
| 38 | + System.out.println("---------------useOldDate---------------------"); |
| 39 | + useOldDate(); |
| 40 | + System.out.println("---------------useLocalDate-----------------"); |
| 41 | + useLocalDate(); |
| 42 | + System.out.println("-------------useTemporalAdjuster---------------"); |
| 43 | + useTemporalAdjuster(); |
| 44 | + System.out.println("--------------useDateFormatter---------------"); |
| 45 | + useDateFormatter(); |
| 46 | + } |
| 47 | + |
| 48 | + private static void useOldDate() { |
| 49 | + Date date = new Date(114, 2, 18); |
| 50 | + System.out.println(date); |
| 51 | + |
| 52 | + System.out.println(formatters.get().format(date)); |
| 53 | + |
| 54 | + Calendar calendar = Calendar.getInstance(); |
| 55 | + calendar.set(2014, Calendar.FEBRUARY, 18); |
| 56 | + System.out.println(calendar); |
| 57 | + } |
| 58 | + |
| 59 | + private static void useLocalDate() { |
| 60 | + LocalDate date = LocalDate.of(2014, 3, 18); |
| 61 | + int year = date.getYear(); // 2014 |
| 62 | + Month month = date.getMonth(); // MARCH |
| 63 | + int day = date.getDayOfMonth(); // 18 |
| 64 | + DayOfWeek dow = date.getDayOfWeek(); // TUESDAY |
| 65 | + int len = date.lengthOfMonth(); // 31 (days in March) |
| 66 | + boolean leap = date.isLeapYear(); // false (not a leap year) |
| 67 | + System.out.println(date); |
| 68 | + |
| 69 | + int y = date.get(ChronoField.YEAR); |
| 70 | + int m = date.get(ChronoField.MONTH_OF_YEAR); |
| 71 | + int d = date.get(ChronoField.DAY_OF_MONTH); |
| 72 | + |
| 73 | + LocalTime time = LocalTime.of(13, 45, 20); // 13:45:20 |
| 74 | + int hour = time.getHour(); // 13 |
| 75 | + int minute = time.getMinute(); // 45 |
| 76 | + int second = time.getSecond(); // 20 |
| 77 | + System.out.println(time); |
| 78 | + |
| 79 | + LocalDateTime dt1 = LocalDateTime.of(2014, Month.MARCH, 18, 13, 45, 20); // 2014-03-18T13:45 |
| 80 | + LocalDateTime dt2 = LocalDateTime.of(date, time); |
| 81 | + LocalDateTime dt3 = date.atTime(13, 45, 20); |
| 82 | + LocalDateTime dt4 = date.atTime(time); |
| 83 | + LocalDateTime dt5 = time.atDate(date); |
| 84 | + System.out.println(dt1); |
| 85 | + |
| 86 | + LocalDate date1 = dt1.toLocalDate(); |
| 87 | + System.out.println(date1); |
| 88 | + LocalTime time1 = dt1.toLocalTime(); |
| 89 | + System.out.println(time1); |
| 90 | + |
| 91 | + Instant instant = Instant.ofEpochSecond(44 * 365 * 86400); |
| 92 | + Instant now = Instant.now(); |
| 93 | + |
| 94 | + Duration d1 = Duration.between(LocalTime.of(13, 45, 10), time); |
| 95 | + Duration d2 = Duration.between(instant, now); |
| 96 | + System.out.println(d1.getSeconds()); |
| 97 | + System.out.println(d2.getSeconds()); |
| 98 | + |
| 99 | + Duration threeMinutes = Duration.of(3, ChronoUnit.MINUTES); |
| 100 | + System.out.println(threeMinutes); |
| 101 | + |
| 102 | + JapaneseDate japaneseDate = JapaneseDate.from(date); |
| 103 | + System.out.println(japaneseDate); |
| 104 | + } |
| 105 | + |
| 106 | + private static void useTemporalAdjuster() { |
| 107 | + LocalDate date = LocalDate.of(2014, 3, 18); |
| 108 | + date = date.with(nextOrSame(DayOfWeek.SUNDAY)); |
| 109 | + System.out.println(date); |
| 110 | + date = date.with(lastDayOfMonth()); |
| 111 | + System.out.println(date); |
| 112 | + |
| 113 | + date = date.with(new NextWorkingDay()); |
| 114 | + System.out.println(date); |
| 115 | + date = date.with(nextOrSame(DayOfWeek.FRIDAY)); |
| 116 | + System.out.println(date); |
| 117 | + date = date.with(new NextWorkingDay()); |
| 118 | + System.out.println(date); |
| 119 | + |
| 120 | + date = date.with(nextOrSame(DayOfWeek.FRIDAY)); |
| 121 | + System.out.println(date); |
| 122 | + date = date.with(DateTimeExamples::nextWorkingDay); |
| 123 | + System.out.println(date); |
| 124 | + } |
| 125 | + |
| 126 | + private static class NextWorkingDay implements TemporalAdjuster { |
| 127 | + @Override |
| 128 | + public Temporal adjustInto(Temporal temporal) { |
| 129 | + return nextWorkingDay(temporal); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private static Temporal nextWorkingDay(Temporal temporal) { |
| 134 | + DayOfWeek dow = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK)); |
| 135 | + int dayToAdd = 1; |
| 136 | + if (dow == DayOfWeek.FRIDAY) dayToAdd = 3; |
| 137 | + if (dow == DayOfWeek.SATURDAY) dayToAdd = 2; |
| 138 | + return temporal.plus(dayToAdd, ChronoUnit.DAYS); |
| 139 | + } |
| 140 | + |
| 141 | + private static void useDateFormatter() { |
| 142 | + LocalDate date = LocalDate.of(2014, 3, 18); |
| 143 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); |
| 144 | + DateTimeFormatter italianFormatter = DateTimeFormatter.ofPattern("d. MMMM yyyy", Locale.ITALIAN); |
| 145 | + |
| 146 | + System.out.println(date.format(DateTimeFormatter.ISO_LOCAL_DATE)); |
| 147 | + System.out.println(date.format(formatter)); |
| 148 | + System.out.println(date.format(italianFormatter)); |
| 149 | + |
| 150 | + DateTimeFormatter complexFormatter = new DateTimeFormatterBuilder() |
| 151 | + .appendText(ChronoField.DAY_OF_MONTH) |
| 152 | + .appendLiteral(". ") |
| 153 | + .appendText(ChronoField.MONTH_OF_YEAR) |
| 154 | + .appendLiteral(" ") |
| 155 | + .appendText(ChronoField.YEAR) |
| 156 | + .parseCaseInsensitive() |
| 157 | + .toFormatter(Locale.ITALIAN); |
| 158 | + |
| 159 | + System.out.println(date.format(complexFormatter)); |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments