1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package _9time;

import java.text.*;
import java.time.*;
import java.time.format.*;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

public class Main {
static String timestampToString(long epochMilli, Locale lo, String zoneId) {
Instant ins = Instant.ofEpochMilli(epochMilli);
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);
return f.withLocale(lo).format(ZonedDateTime.ofInstant(ins, ZoneId.of(zoneId)));
}
public static void main(String[] args) {
//Date
int n = 123400;
// 123400
System.out.println(n);
// 1e208
System.out.println(Integer.toHexString(n));
// $123,400.00
System.out.println(NumberFormat.getCurrencyInstance(Locale.US).format(n));

// 获取当前时间:
Date date = new Date();
System.out.println(date.getYear() + 1900); // 必须加上1900
System.out.println(date.getMonth() + 1); // 0~11,必须加上1
System.out.println(date.getDate()); // 1~31,不能加1
// 转换为String:
System.out.println(date.toString());
// 转换为GMT时区:
System.out.println(date.toGMTString());
// 转换为本地时区:
System.out.println(date.toLocaleString());

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));

SimpleDateFormat sdf1 = new SimpleDateFormat("E MMM dd, yyyy");
System.out.println(sdf1.format(date));

//Calendar
// 获取当前时间:
Calendar c = Calendar.getInstance();
int y = c.get(Calendar.YEAR);
int m = 1 + c.get(Calendar.MONTH);
int d = c.get(Calendar.DAY_OF_MONTH);
int w = c.get(Calendar.DAY_OF_WEEK);
int hh = c.get(Calendar.HOUR_OF_DAY);
int mm = c.get(Calendar.MINUTE);
int ss = c.get(Calendar.SECOND);
int ms = c.get(Calendar.MILLISECOND);
System.out.println(y + "-" + m + "-" + d + " " + w + " " + hh + ":" + mm + ":" + ss + "." + ms);

// 清除所有:
c.clear();
// 设置2019年:
c.set(Calendar.YEAR, 2019);
// 设置9月:注意8表示9月:
c.set(Calendar.MONTH, 8);
// 设置2日:
c.set(Calendar.DATE, 2);
// 设置时间:
c.set(Calendar.HOUR_OF_DAY, 21);
c.set(Calendar.MINUTE, 22);
c.set(Calendar.SECOND, 23);
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime()));
// 2019-09-02 21:22:23

//TimeZone
TimeZone tzDefault = TimeZone.getDefault(); // 当前时区
TimeZone tzGMT9 = TimeZone.getTimeZone("GMT+09:00"); // GMT+9:00时区
TimeZone tzNY = TimeZone.getTimeZone("America/New_York"); // 纽约时区
System.out.println(tzDefault.getID()); // Asia/Shanghai
System.out.println(tzGMT9.getID()); // GMT+09:00
System.out.println(tzNY.getID()); // America/New_York
System.out.println(TimeZone.getAvailableIDs().toString());


//北京时间转为纽约时间
// 清除所有:
c.clear();
// 设置为北京时区:
c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
// 设置年月日时分秒:
c.set(2019, 10 /* 11月 */, 20, 8, 15, 0);
// 显示时间:
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf2.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(sdf2.format(c.getTime()));
// 2019-11-19 19:15:00


//时间加减
// 清除所有:
c.clear();
// 设置年月日时分秒:
c.set(2019, 10 /* 11月 */, 20, 8, 15, 0);
// 加5天并减去2小时:
c.add(Calendar.DAY_OF_MONTH, 5);
c.add(Calendar.HOUR_OF_DAY, -2);
// 显示时间:
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d3 = c.getTime();
System.out.println(sdf3.format(d3));
// 2019-11-25 6:15:00

//LocalDateTime
LocalDate d4 = LocalDate.now(); // 当前日期
LocalTime t4 = LocalTime.now(); // 当前时间
LocalDateTime dt4 = LocalDateTime.now(); // 当前日期和时间
System.out.println(d4); // 严格按照ISO 8601格式打印
System.out.println(t4); // 严格按照ISO 8601格式打印
System.out.println(dt4); // 严格按照ISO 8601格式打印

// 指定日期和时间:
LocalDate d5 = LocalDate.of(2019, 11, 30); // 2019-11-30, 注意11=11月
LocalTime t5 = LocalTime.of(15, 16, 17); // 15:16:17
LocalDateTime dt5 = LocalDateTime.of(2019, 11, 30, 15, 16, 17);
LocalDateTime dt55 = LocalDateTime.of(d5, t5);
System.out.println(d5);
System.out.println(t5);
System.out.println(dt5);
System.out.println(dt55);


LocalDateTime dt6 = LocalDateTime.parse("2019-11-19T15:16:17");
LocalDate d6 = LocalDate.parse("2019-11-19");
LocalTime t6 = LocalTime.parse("15:16:17");
System.out.println(dt6);
System.out.println(d6);
System.out.println(t6);

//DateTimeFormatter
// 自定义格式化:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
System.out.println(dtf.format(LocalDateTime.now()));

// 用自定义格式解析:
LocalDateTime dt2 = LocalDateTime.parse("2019/11/30 15:16:17", dtf);
System.out.println(dt2);

//时间加减
LocalDateTime dt = LocalDateTime.of(2019, 10, 26, 20, 30, 59);
System.out.println(dt);
// 加5天减3小时:
LocalDateTime dt7 = dt.plusDays(5).minusHours(3);
System.out.println(dt7); // 2019-10-31T17:30:59
// 减1月:
LocalDateTime dt8 = dt7.minusMonths(1);
System.out.println(dt8); // 2019-09-30T17:30:59


//时间日期调整
LocalDateTime dt9 = LocalDateTime.of(2019, 10, 26, 20, 30, 59);
System.out.println(dt9);
// 日期变为31日:
LocalDateTime dt10 = dt9.withDayOfMonth(31);
System.out.println(dt10); // 2019-10-31T20:30:59
// 月份变为9:
LocalDateTime dt11 = dt10.withMonth(9);
System.out.println(dt11); // 2019-09-30T20:30:59

//with()
// 本月第一天0:00时刻:
LocalDateTime firstDay = LocalDate.now().withDayOfMonth(1).atStartOfDay();
System.out.println("本月第一天0:00时刻:"+firstDay);

// 本月最后1天:
LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
System.out.println("本月最后1天:"+lastDay);

// 下月第1天:
LocalDate nextMonthFirstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println("下月第1天:"+nextMonthFirstDay);

// 本月第1个周一:
LocalDate firstWeekday = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println("本月第1个周一:"+firstWeekday);


//判断时间先后
LocalDateTime now = LocalDateTime.now();
LocalDateTime target = LocalDateTime.of(2019, 11, 19, 8, 15, 0);
System.out.println(now.isBefore(target));
System.out.println(LocalDate.now().isBefore(LocalDate.of(2019, 11, 19)));
System.out.println(LocalTime.now().isAfter(LocalTime.parse("08:15:00")));

//Duration和Period 时间间隔,日期间隔
LocalDateTime start = LocalDateTime.of(2019, 11, 19, 8, 15, 0);
LocalDateTime end = LocalDateTime.of(2020, 1, 9, 19, 25, 30);
Duration d2 = Duration.between(start, end);
Duration d21 = Duration.ofHours(10); // 10 hours
Duration d22 = Duration.parse("P1DT2H3M"); // 1 day, 2 hours, 3 minutes
System.out.println(d2); // PT1235H10M30S
System.out.println(d21); // PT1235H10M30S
System.out.println(d22); // PT1235H10M30S

Period p = LocalDate.of(2019, 11, 19).until(LocalDate.of(2020, 1, 9));
System.out.println(p); // P1M21D

/**ZonedDateTime*/
ZonedDateTime zbj = ZonedDateTime.now(); // 默认时区
ZonedDateTime zny = ZonedDateTime.now(ZoneId.of("America/New_York")); // 用指定时区获取当前时间
System.out.println(zbj);
System.out.println(zny);


// 以中国时区获取当前时间:
ZonedDateTime zbj1 = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
// 转换为纽约时间:
ZonedDateTime zny1 = zbj1.withZoneSameInstant(ZoneId.of("America/New_York"));
System.out.println(zbj1);
System.out.println(zny1);

ZonedDateTime zdt = ZonedDateTime.now();
LocalDateTime ldt = zdt.toLocalDateTime();
System.out.println(ldt);

/**DateTimeFormatter*/
ZonedDateTime zdt1 = ZonedDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm ZZZZ");
System.out.println(formatter.format(zdt1));

DateTimeFormatter zhFormatter = DateTimeFormatter.ofPattern("yyyy MMM dd EE HH:mm", Locale.CHINA);
System.out.println(zhFormatter.format(zdt1));

DateTimeFormatter usFormatter = DateTimeFormatter.ofPattern("E, MMMM/dd/yyyy HH:mm", Locale.US);
System.out.println(usFormatter.format(zdt1));

LocalDateTime ldt1 = LocalDateTime.now();
System.out.println(DateTimeFormatter.ISO_DATE.format(ldt1));
System.out.println(DateTimeFormatter.ISO_DATE_TIME.format(ldt1));


//Instant 当前时间戳

Instant instant = Instant.now();
System.out.println(instant.getEpochSecond());
System.out.println(instant.toEpochMilli());


// 以指定时间戳创建Instant:
Instant ins = Instant.ofEpochSecond(1568568760);
ZonedDateTime zdt2 = ins.atZone(ZoneId.systemDefault());
System.out.println(zdt2); // 2019-09-16T01:32:40+08:00[Asia/Shanghai]

/**旧API转新API*/
// Date -> Instant:
Instant ins1 = new Date().toInstant();

// Calendar -> Instant -> ZonedDateTime:
Calendar calendar = Calendar.getInstance();
Instant ins2 = Calendar.getInstance().toInstant();
ZonedDateTime zdt3 = ins2.atZone(calendar.getTimeZone().toZoneId());
System.out.println(zdt3);

/**新API转旧API*/
// ZonedDateTime -> long:
ZonedDateTime zdt4 = ZonedDateTime.now();
long ts = zdt4.toEpochSecond() * 1000;

// long -> Date:
Date date1 = new Date(ts);

// long -> Calendar:
Calendar calendar1 = Calendar.getInstance();
calendar1.clear();
calendar1.setTimeZone(TimeZone.getTimeZone(zdt.getZone().getId()));
calendar1.setTimeInMillis(zdt4.toEpochSecond() * 1000);


//数据库时间
long ts1 = 1574208900000L;
System.out.println(timestampToString(ts1, Locale.CHINA, "Asia/Shanghai"));
System.out.println(timestampToString(ts1, Locale.US, "America/New_York"));
}
}