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

Date与时区有关

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

//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);
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);
}
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日期加减

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);
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());
}
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判断周末与时区有关

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("非周末");
}
Fri Oct 09 16:56:10 CST 2020
Fri Oct 09 16:56:10 CST 2020
非周末

Calendar获取时间与时区无关

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);
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

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/26/sort-out-confusing-points-of-date-localdatetime-and-calendar-in-java/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Java中Date、LocalDateTime与Calendar易混淆点梳理
Date与时区有关 Date最好取系统时区,否则容易混淆 //Date now = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()); //取系统……
<<上一篇
下一篇>>
文章目录
关闭
目 录