java判断两个日期相差时长_Java日期和时间

作者:Java伴我余生

链接:Java日期和时间 - Java伴我余生 - 博客园

来源:博客园

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

概念#
时区#
不同时区在同一时刻,它们的本地时间是不同的,全球一共有24个时区,我们把伦敦所在的时区称为标准时区,其他时区按照东西偏移的小时来区分,北京所在的时区是东八区(GMT+08:00)。
时区的表现方式如下:

  • GMT+08:00
  • UTC+08:00
  • CST(China Standard Time / Central Standard Time USA)
  • Asia/Shanghai

b4b8e045dbd2a511cfc4a3b8682bf804.png

地区#
Local 表示一个国家或地区的日期、时间、数字、货币等格式

  • zh_CN: 表示的是中国的Local,日期用年月日表示2020-07-05
  • en_US: 表示的是美国的Local,日期用日月年表示05/07/2020

计算机用Local在日期、时间、货币和字符串之间进行转换
中国用户 美国用户 购买价格: 12000.0 12,000.00 购买日期: 2020-07-05 05/07/2020 java.time的API#
java.time提供了新的日期和时间API

  • LocalDate/LocalTime/LocalDateTime
  • ZonedDateTime/ZoneId
  • Instant
  • Formatter
  • ...

新API的特点:

  • 严格区分日期、时间
  • 不变类(类似String)
  • Month范围1~12(Jan~Dec)
  • Week范围1~7(Mon~Sun)

b8d4e16dc0282b72ae7b4b38c5f4ef39.png

LocalDateTime#
LocalDate d = LocalDate.now(); // 当前日期 LocalTime t = LocalTime.now(); //当前时间 LocalDateTime dt = LocalDateTime.now(); // 当前日期时间 System.out.println(dt); // 严格按照ISO 8601格式打印 2020-07-05T16:38:37.356 //指定日期和时间 LocalDate d2 = LocalDate.of(2020, 7, 5); // 2020-07-05,注意 7=7月 LocalTime t2 = LocalTime.of(16, 38, 37); // 16:38:37 LocalDateTime dt2 = LocalDateTime.of(2020, 7, 5,16, 38, 37); // 2020-07-05T16:38:37 LocalDateTime dt3 = LocalDateTime.of(d2, t2); // 2020-07-05T16:38:37 //对日期进行格式化a DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println(dtf.format(LocalDateTime.now())); // 2020-07-05 16:45:08 //将字符串解析成日期 LocalDateTime parse = LocalDateTime.parse("2020-07-05 16:45:08", dtf); System.out.println(parse); // 2020-07-05T16:45:08
新的DateTimeFormatter是不可变的。默认情况下,LocalDate,LocalTime,LocalDateTime按照ISO标准格式化和解析:

  • yyyy-MM-dd
  • HH:mm:ss
  • HH:mm:ss:SSS
  • yyyy-MM-dd'T'HH:mm:ss
  • yyyy-MM-dd'T'HH:mm:ss:SSS

051eec054487c3948aa82aacdf8f98b9.png

重大更新:对日期和时间进行加减

  • plusDays() 在现在的日期加上具体天数
  • minusHous() 在现在的时间减去具体的小时
  • plusWeeks() 在现在的日期时间加上具体的周数

// +5 天 LocalDate today = LocalDate.now(); LocalDate after5Days = today.plusDays(5); System.out.println(after5Days); //2020-07-10 // -2小时 LocalTime now = LocalTime.now(); LocalTime before2Hours = now.minusHours(2); System.out.println(before2Hours); // 14:59:22.526 // +1月-2周 LocalDate date = today.plusMonths(1).minusWeeks(2); System.out.println(date); // 2020-07-22
对日期和时间进行调整:

  • withDayOfMonth()
  • withMonth()
  • withHour()
  • ...
  • with()

//本月第一天 LocalDate firstDay = LocalDate.now().withDayOfMonth(1); System.out.println(firstDay); // 2020-07-01 //把秒和纳秒调整为0 LocalTime at = LocalTime.now().withSecond(0).withNano(0); System.out.println(at); // 17:08 //本月最后一天 LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()); System.out.println(lastDay); // 2020-07-31 //本月第一个周日 LocalDate firstSunday = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY)); System.out.println(firstSunday); //2020-07-05
判断日期和时间的先后:

  • isBefore()
  • isAfter()
  • equals()

430b7ceccfd76e1bb4c727861129f387.png

LocalDate d01 = LocalDate.of(2020,7,5); LocalDate d02 = LocalDate.of(2020,7,4); System.out.println(d01.isBefore(d02)); // false System.out.println(d01.isAfter(d02)); // true
计算日期的Period: 某一天到指定的某一天具体相差多久

  • getYears()
  • getMonths()
  • getDays()

LocalDate d03 = LocalDate.of(2020,7,5); LocalDate d04 = LocalDate.of(2018,3,28); //通过until()方法获取Period对象,判断两个日期相差?年?月?天 Period period = d03.until(d04); System.out.println(period); // P-2Y-3M-8D 表示2020年7月5日到2018年3月28日中相差 2年3个月8天 //两个日期一共相差多少天? long day01 = LocalDate.of(2020, 7, 5).toEpochDay(); long day02 = LocalDate.of(2018,3,28).toEpochDay(); System.out.println(day01-day02); // 830
LocalDateTime无法与long进行转换

  • 因为LocalDateTime 没有时区,无法确定某一时刻
  • ZonedDateTime 有时区,可以与long进行转换

ZonedDateTime#
ZonedDateTime = LocalDateTime + ZoneId

  • ZonedDateTime: 带时区的日期和时间
  • ZoneId: 新的时区对象(取代旧的java.util.TimeZone)
  • Instant: 时刻对象(epoch seconds)

ZonedDateTime zbj = ZonedDateTime.now(); // 当前时区的日期和时间 System.out.println(zbj); // 2020-07-05T17:32:40.415+08:00[Asia/Shanghai] ZonedDateTime zny = ZonedDateTime.now(ZoneId.of("America/New_York")); // 纽约时区的当前日期和时间 System.out.println(zny); // 2020-07-05T05:34:29.522-04:00[America/New_York]
ZonedDateTime可以从LocalDateTime转换: atZone()
//关联到当前默认时区 ZonedDateTime bj = ldt.atZone(ZoneId.systemDefault()); System.out.println(bj); // 2020-07-05T17:36:12+08:00[Asia/Shanghai] //关联到纽约时区 ZonedDateTime ny = ldt.atZone(ZoneId.of("America/New_York")); System.out.println(ny); // 2020-07-05T17:36:12-04:00[America/New_York]
转换时区:withZoneSamleInstant()
LocalDateTime ldt = LocalDateTime.of(2020, 7, 5, 17, 36, 12); //关联到当前默认时区 ZonedDateTime bj = ldt.atZone(ZoneId.systemDefault()); System.out.println(bj); // 2020-07-05T17:36:12+08:00[Asia/Shanghai] // 转换到纽约时区 ZonedDateTime zdt = bj.withZoneSameInstant(ZoneId.of("America/New_York")); System.out.println(zdt); // 2020-07-05T17:36:12-04:00[America/New_York]
Instant对象表示时刻:

  • ZonedDateTime.toInstant()
  • Instant.getEpochSecond()

Instant ins = Instant.now(); Instant ins2 = ZonedDateTime.now().toInstant(); ZonedDateTime zdt = ins.atZone(ZoneId.of("Z")); //注意是秒 long epoch = ins.getEpochSecond();
如果项目中需要使用generatorConfig.xml配置文件生成java8中的日期类型LocalDate、LocalTime、LocalDateTime等等,需要在配置文件中的javaTypeResolver结点中加入属性useJSR310Types,当useJSR310Types为true时,就会jdbc对应的日期类型会转成java8中的LocateDateTime类型,如果useJSR310Types为false,则还是转成java.util.Date类型。
<javaTypeResolver> <property name="useJSR310Types" value="true"/> </javaTypeResolver>
一旦mybatis中使用了java8中的日期类型,记得要在项目中引入mybatis-typehandlers-jsr310,这个主要是处理java8中的日期类型。
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-typehandlers-jsr310</artifactId> <version>1.0.2</version> </dependency>
注意:用maven mybatis插件如果不在plugin里面添加依赖包的引用的话,会找不到相关得jar包,在plugin外部得jar包,他不会去找到并执行,所以要把plugin运行依赖得jar配置都放在里面,pom.xml配置 mybatis插件如下:
<plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> </dependencies> </plugin> </plugins>
注意,在连接mysql8.0的时候要指明timeZone,即serverTimezone=UTC这个不能忘

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值