比较时间的先后;计算时间差;时间加减;夏令时转换
·
java比较LocalDate类型的时间大小
LocalDate date = LocalDate.now()
LocalDate firstDay = LocalDate.parse("2020-11-11",DateTimeFormatter.ofPattern("yyyy-MM-dd")
date.isBefore(firstDay))
date.isAfter(firstDay);
date.isEqual(firstDay);
java获取当前日期0分0秒
LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0))
java计算时间差
方式1,使用Duration精确计算
这里要注意duration计算天数时按照时分秒相减是否=24小时来算的,如果正好是2022-08-09 11:11:1:11 到2022-08-10 00:00:00,不足24小时,那么获得的天数是0
Duration对象用秒和纳秒来衡量时间的长短,所以入参不能使用LocalDate类型,
否则抛UnsupportedTemporalTypeException: Unsupported unit: Seconds
LocalDateTime dateTime1 = LocalDateTime.now()
LocalDateTime dateTime2 = LocalDateTime.parse("2020-11-25 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
Duration duration = Duration.between(dateTime1,dateTime2)
//总天数
duration.toDays()
//总小时数
duration.toHours()
//总分钟数
duration.toMinutes()
//总毫秒数
duration.toMillis()
方式2,使用Period计算周期
最小单位是最小天数,不足一个月则只计算天数,满一个月则是1个月零几天,以此类推
比如date1和date2相差1个月零2天,用period.getDays()计算得到的是2天
计算结果可能有正负
,between
方法中后一个时间减去前一个时间
LocalDate dateTime3 = LocalDate.parse("2020-11-24", DateTimeFormatter.ofPattern("yyyy-MM-dd"))
Period period = Period.between(LocalDate.now(),dateTime3)
println(period.getDays())
println(period.getMonths())
println(period.getYears())
java8时间加减
//加一天
LocalDate tomorrow = LocalDate.now().plusDays(1);
//减5小时,减30分钟
LocalDateTime dateTime = LocalDateTime.now().minusHours(5).minusMinutes(30);
LocalDate/LocalDateTime自带方法直接算计时间差
LocalDateTime
用法也是一样的,这里就不多写了,只上个运行demo截图
LocalDate start = LocalDate.parse("2018-08-28");
LocalDate end = LocalDate.parse("2018-09-30");
long year = start.until(end, ChronoUnit.YEARS);
long month = start.until(end, ChronoUnit.MONTHS);
long days = start.until(end, ChronoUnit.DAYS);
System.out.println("间隔:" + year + "年");
System.out.println("间隔:" + month + "月");
System.out.println("间隔:" + days + "天");
DateUtils/DateFormatUtils工具类
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
// Date 转化为字符串
DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");
// 字符串 转 Date
DateUtils.parseDate("2020-05-07 22:00:00","yyyy-MM-dd HH:mm:ss");
Date now = new Date();
// Date 加 1 天
Date addDays = DateUtils.addDays(now, 1);
// Date 加 33 分钟
Date addMinutes = DateUtils.addMinutes(now, 33);
// Date 减去 233 秒
Date addSeconds = DateUtils.addSeconds(now, -233);
// 判断是否 Wie 同一天
boolean sameDay = DateUtils.isSameDay(addDays, addMinutes);
// 过滤时分秒,若 now 为 2020-05-07 22:13:00 调用 truncate 方法以后
// 返回时间为 2020-05-07 00:00:00
Date truncate = DateUtils.truncate(now, Calendar.DATE);
// 过滤月份,若 now 为 2020-05-07 22:13:00 调用 truncate 方法以后
// 返回时间为 2020-05-01 00:00:00
Date truncate = DateUtils.truncate(now, Calendar.MONTH);
夏令时转换
如果有用mysql8,在db中有现成的函数可以转换
CONVERT_TZ(now(), 'Asia/Shanghai','US/Pacific')
或者用代码转换
美国太平时时区和上海查16小时,如果遇到太平洋时区夏令时则相差15
// 原始日期时间
Instant instant = Instant.ofEpochMilli(new DateTime("2024-10-17 16:00:00").getTime());
// 源时区,例如中国上海时区,该时区不实行夏令时
ZoneId sourceZone = ZoneId.of("Asia/Shanghai");
// 目标时区,例如美国太平洋时区,该时区有夏令时
ZoneId targetZone = ZoneId.of("US/Pacific");
// 获取目标时区的规则,以检查夏令时
ZoneRules targetRules = targetZone.getRules();
// 如果源时区正在实行夏令时,则转换时需要考虑
if (targetRules.isDaylightSavings(instant)) {
// 转换为带有夏令时偏移的日期时间
ZonedDateTime sourceZonedDateTime = instant.atZone(sourceZone);
ZonedDateTime targetZonedDateTime = sourceZonedDateTime.withZoneSameInstant(targetZone);
System.out.println("转换后的日期时间(考虑夏令时):" + targetZonedDateTime);
System.out.println(targetZonedDateTime.toLocalDateTime());
return;
}
// 如果目标时区不在夏令时期间,直接转换
ZonedDateTime targetZonedDateTime = instant.atZone(sourceZone).withZoneSameInstant(targetZone);
System.out.println("转换后的日期时间(不考虑夏令时):" + targetZonedDateTime);
System.out.println(targetZonedDateTime.toLocalDateTime());
更多推荐
已为社区贡献13条内容
所有评论(0)