Java中Date、LocalDateTime与Calendar易混淆点梳理

Date与时区有关

Date最好取系统时区,否则容易混淆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Date now = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());  //取系统时区
Date thDate = Date.from(LocalDateTime.now().atZone(TimeZone.getTimeZone("GMT+8").toZoneId()).toInstant()); //与时区有关
System.out.println(thDate);
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+0")); //与时区无关
calendar.setTime(thDate);
calendar.add(Calendar.HOUR, +1);
Date cnDate = calendar.getTime();
System.out.println(cnDate);

String thday = DateUtil.formatDateTime(thDate);
System.out.println(thday);

String cnday = DateUtil.formatDateTime(cnDate);
System.out.println(cnday);
1
2
3
4
5
public static String formatDateTime(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+7"));
return sdf.format(date);
}
1
2
3
4
Fri Oct 09 16:57:58 CST 2020
Fri Oct 09 17:57:58 CST 2020
2020-10-09 15:57:58
2020-10-09 16:57:58

LocalDateTime日期加减

1
2
3
4
5
6
7
8
9
10
Date thDate = Date.from(LocalDateTime.now().atZone(TimeZone.getTimeZone("GMT+8").toZoneId()).toInstant());
System.out.println(thDate);
Date cnDate = DateUtil.addHours(thDate, 1);
System.out.println(cnDate);

String thday = DateUtil.formatDateTime(thDate);
System.out.println(thday);

String cnday = DateUtil.formatDateTime(cnDate);
System.out.println(cnday);
1
2
3
4
5
6
7
8
9
public static Date addHours(Date date, int hours) {
LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
return Date.from(dateTime.plusHours(hours).atZone(ZoneId.systemDefault()).toInstant());
}

public static Date addDays(Date date, int days) {
LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
return Date.from(dateTime.minusDays(-days).atZone(ZoneId.systemDefault()).toInstant());
}
1
2
3
4
Fri Oct 09 17:11:02 CST 2020
Fri Oct 09 18:11:02 CST 2020
2020-10-09 16:11:02
2020-10-09 17:11:02

Calendar判断周末与时区有关

1
2
3
4
5
6
7
8
9
10
Date date = new Date();
System.out.println(date);
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT-2"));
Date date2 = calendar.getTime();
System.out.println(date2);
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
System.out.println("周末");
} else {
System.out.println("非周末");
}
1
2
3
Fri Oct 09 16:56:10 CST 2020
Fri Oct 09 16:56:10 CST 2020
非周末

Calendar获取时间与时区无关

1
2
3
4
5
6
7
8
9
10
11
12
13
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
Date cnDate = calendar.getTime();
System.out.println(cnDate);
calendar.setTimeZone(TimeZone.getTimeZone("GMT+7"));
Date thDate = calendar.getTime();
System.out.println(thDate);

String thday = DateUtil.formatDateTime(thDate);
System.out.println(thday);

String cnday = DateUtil.formatDateTime(cnDate);
System.out.println(cnday);
1
2
3
4
Fri Oct 09 17:22:58 CST 2020
Fri Oct 09 17:22:58 CST 2020
2020-10-09 16:22:58
2020-10-09 16:22:58

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :