Skip to content

Commit 2f9a785

Browse files
committed
第12章示例代码
1 parent f7cd66b commit 2f9a785

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package com.wbs.java8lambda.chapter12;
2+
3+
import java.text.DateFormat;
4+
import java.text.SimpleDateFormat;
5+
import java.time.*;
6+
import java.time.chrono.JapaneseDate;
7+
import java.time.format.DateTimeFormatter;
8+
import java.time.format.DateTimeFormatterBuilder;
9+
import java.time.temporal.*;
10+
import java.util.Calendar;
11+
import java.util.Date;
12+
import java.util.Locale;
13+
14+
/**
15+
* TODO
16+
*
17+
* @author: wangbingshuai
18+
* @create: 2019-11-12 16:54
19+
**/
20+
public class DateTimeExample {
21+
22+
private static final ThreadLocal<DateFormat> formatters = new ThreadLocal<DateFormat>() {
23+
@Override
24+
protected DateFormat initialValue() {
25+
return new SimpleDateFormat("dd-MMM-yyyy");
26+
}
27+
};
28+
29+
public static void main(String[] args) {
30+
// useOldDate();
31+
// useLocalDate();
32+
// useTemporalAdjuster();
33+
useDateFormatter();
34+
}
35+
36+
private static void useOldDate() {
37+
Date date = new Date(119, 10, 18);
38+
System.out.println(date);
39+
40+
System.out.println(formatters.get().format(date));
41+
42+
Calendar instance = Calendar.getInstance();
43+
instance.set(2019, Calendar.FEBRUARY, 18);
44+
System.out.println(instance);
45+
}
46+
47+
private static void useLocalDate() {
48+
LocalDate date = LocalDate.of(2019, 11, 18);
49+
// System.out.println(date.getYear());
50+
// System.out.println(date.getMonth());
51+
// System.out.println(date.getDayOfMonth());
52+
// System.out.println(date.getDayOfWeek());
53+
// System.out.println(date.lengthOfMonth());
54+
// System.out.println(date.isLeapYear());
55+
System.out.println(date);
56+
57+
// System.out.println(date.get(ChronoField.YEAR));
58+
// System.out.println(date.get(ChronoField.MONTH_OF_YEAR));
59+
// System.out.println(date.get(ChronoField.DAY_OF_MONTH));
60+
61+
LocalTime time = LocalTime.of(12, 34, 56);
62+
// System.out.println(time.getHour());
63+
// System.out.println(time.getMinute());
64+
// System.out.println(time.getSecond());
65+
System.out.println(time);
66+
67+
LocalDateTime dt1 = LocalDateTime.of(2019, Month.DECEMBER, 18, 12, 34, 56);
68+
// System.out.println(LocalDateTime.of(date, time));
69+
// System.out.println(date.atTime(12, 34, 56));
70+
// System.out.println(date.atTime(time));
71+
// System.out.println(time.atDate(date));
72+
System.out.println(dt1);
73+
74+
LocalDate date1 = dt1.toLocalDate();
75+
System.out.println(date1);
76+
LocalTime time1 = dt1.toLocalTime();
77+
System.out.println(time1);
78+
79+
Instant instant = Instant.ofEpochSecond(60 * 60 * 24 * 365 * 44);
80+
Instant now = Instant.now();
81+
82+
Duration d1 = Duration.between(LocalTime.of(12, 34, 21), time);
83+
Duration d2 = Duration.between(instant, now);
84+
System.out.println(d1.getSeconds());
85+
System.out.println(d2.getSeconds());
86+
87+
Duration threeMinutes = Duration.of(3, ChronoUnit.MINUTES);
88+
System.out.println(threeMinutes);
89+
90+
JapaneseDate japaneseDate = JapaneseDate.from(date);
91+
System.out.println(japaneseDate);
92+
}
93+
94+
private static void useTemporalAdjuster() {
95+
LocalDate date = LocalDate.of(2019, 11, 18);
96+
date = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
97+
System.out.println(date);
98+
date = date.with(TemporalAdjusters.lastDayOfMonth());
99+
System.out.println(date);
100+
101+
date = date.with(new NextWorkingDay());
102+
System.out.println(date);
103+
date = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
104+
System.out.println(date);
105+
date = date.with(new NextWorkingDay());
106+
System.out.println(date);
107+
108+
date = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
109+
System.out.println(date);
110+
date = date.with(temporal -> {
111+
DayOfWeek dow = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
112+
int dayToAdd = 1;
113+
if (dow == DayOfWeek.FRIDAY) {
114+
dayToAdd = 3;
115+
}
116+
if (dow == DayOfWeek.SATURDAY) {
117+
dayToAdd = 2;
118+
}
119+
return temporal.plus(dayToAdd, ChronoUnit.DAYS);
120+
});
121+
System.out.println(date);
122+
}
123+
124+
private static class NextWorkingDay implements TemporalAdjuster {
125+
@Override
126+
public Temporal adjustInto(Temporal temporal) {
127+
DayOfWeek dow = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
128+
int dayToAdd = 1;
129+
if (dow == DayOfWeek.FRIDAY) {
130+
dayToAdd = 3;
131+
}
132+
if (dow == DayOfWeek.SATURDAY) {
133+
dayToAdd = 2;
134+
}
135+
return temporal.plus(dayToAdd, ChronoUnit.DAYS);
136+
}
137+
}
138+
139+
private static void useDateFormatter() {
140+
LocalDate date = LocalDate.of(2019, 11, 18);
141+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
142+
DateTimeFormatter chinaFormatter = DateTimeFormatter.ofPattern("d. MMMM yyyy", Locale.CHINESE);
143+
144+
System.out.println(date.format(DateTimeFormatter.ISO_LOCAL_DATE));
145+
System.out.println(date.format(formatter));
146+
System.out.println(date.format(chinaFormatter));
147+
148+
DateTimeFormatter complexFormatter = new DateTimeFormatterBuilder()
149+
.appendText(ChronoField.DAY_OF_MONTH)
150+
.appendLiteral(". ")
151+
.appendText(ChronoField.MONTH_OF_YEAR)
152+
.appendLiteral(" ")
153+
.appendText(ChronoField.YEAR)
154+
.parseCaseInsensitive()
155+
.toFormatter(Locale.CHINESE);
156+
System.out.println(date.format(complexFormatter));
157+
}
158+
}

0 commit comments

Comments
 (0)