时间工具类

1.LocalDateTime,LocalDate,LocalTime:

time1.plusYears(1l);//加一年
time1.plus(2l, ChronoUnit.YEARS);//加两年

time1.plusYears(-1l);//减一年
time1.plus(-2l, ChronoUnit.YEARS);//减两年

2.LocalDateTime类中方法,可以直接使用方法isBeforeisAfterisEqual来判定时间的先后。

Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-04-01 10:20:30");
Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-04-01 11:25:55");


if (date1.before(date2)) {

    System.out.println("date1 is before date2");

} else if (date1.after(date2)) {

    System.out.println("date1 is after date2");

} else {

    System.out.println("date1 is equal to date2");

}

3.使用Date类比较,Java Date 类提供了before、after和equals方法来比较两个日期。

Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-04-01 10:20:30");
Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-04-01 11:25:55");


if (date1.before(date2)) {

    System.out.println("date1 is before date2");

} else if (date1.after(date2)) {

    System.out.println("date1 is after date2");

} else {

    System.out.println("date1 is equal to date2");

}

4.使用 Calendar 类比较,Calendar 类也可以用来比较日期,通过将 Date 对象转换成 Calendar 对象来比较时间。

Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();

Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-04-01 10:20:30");
Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-04-01 11:25:55");



cal1.setTime(date1);


cal2.setTime(date2);


if (cal1.before(cal2)) {

    System.out.println("cal1 is before cal2");

} else if (cal1.after(cal2)) {

    System.out.println("cal1 is after cal2");

} else {

    System.out.println("cal1 is equal to cal2");

}

5.使用 Instant 类比较

对于UTC时间的比较,可以使用 Instant 类进行时间戳的比较。

Instant instant1 = dateTime1.toInstant(ZoneOffset.UTC);
Instant instant2 = dateTime2.toInstant(ZoneOffset.UTC);


if (instant1.isBefore(instant2)) {

    System.out.println("instant1 is before instant2");

} else if (instant1.isAfter(instant2)) {

    System.out.println("instant1 is after instant2");

} else {

    System.out.println("instant1 is equal to instant2");

}
  1. 考虑时区的时间比较

当涉及到不同时区的时间比较时,可能需要转换为统一的时区然后进行比较。

ZonedDateTime zonedDateTime1 = ZonedDateTime.of(dateTime1, ZoneId.of("America/New_York"));
ZonedDateTime zonedDateTime2 = ZonedDateTime.of(dateTime2, ZoneId.of("America/Los_Angeles"));


if (zonedDateTime1.isBefore(zonedDateTime2)) {

    System.out.println("zonedDateTime1 is before zonedDateTime2");

} else if (zonedDateTime1.isAfter(zonedDateTime2)) {

    System.out.println("zonedDateTime1 is after zonedDateTime2");

} else {

    System.out.println("zonedDateTime1 is equal to zonedDateTime2");

}

引用:java比较时间大小 -- PingCode

相关推荐
qmx_0712 分钟前
HTB-Jerry(tomcat war文件、msfvenom)
java·web安全·网络安全·tomcat
为风而战21 分钟前
IIS+Ngnix+Tomcat 部署网站 用IIS实现反向代理
java·tomcat
技术无疆2 小时前
快速开发与维护:探索 AndroidAnnotations
android·java·android studio·android-studio·androidx·代码注入
架构文摘JGWZ5 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
拾光师6 小时前
spring获取当前request
java·后端·spring
aPurpleBerry6 小时前
neo4j安装启动教程+对应的jdk配置
java·neo4j
我是苏苏6 小时前
Web开发:ABP框架2——入门级别的增删改查Demo
java·开发语言
xujinwei_gingko6 小时前
Spring IOC容器Bean对象管理-Java Config方式
java·spring
2301_789985946 小时前
Java语言程序设计基础篇_编程练习题*18.29(某个目录下的文件数目)
java·开发语言·学习
IT学长编程6 小时前
计算机毕业设计 教师科研信息管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·毕业设计·springboot·毕业论文·计算机毕业设计选题·计算机毕业设计开题报告·教师科研管理系统