Java计算两个时间间隔的几个月

使用Period对象,进行计算两个时间之间,间隔的月数

Date nowYearMonth = new SimpleDateFormat("yyyy-MM").parse(DateUtils.parseDateToStr("yyyy-MM", new Date()));
String suspenseAccountDate = rule.getSuspenseAccountDate();
String dateType = rule.getDateType();
String suspenseMonth = rule.getSuspenseMonth();

Instant nowInstant = nowYearMonth.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDate now = nowInstant.atZone(zoneId).toLocalDate();

Date insureMonth = new SimpleDateFormat("yyyy-MM").parse(vo.getInsureMonth());
Instant insureMonthInstant = insureMonth.toInstant();
LocalDate insureMonthDate = insureMonthInstant.atZone(zoneId).toLocalDate();

//对比现在时间和业务费用所属月
int distance = Period.between(now, insureMonthDate).getMonths();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要求两个LocalDateTime类型时间相隔的秒数,可以使用Duration类的between方法来计算。首先,需要将两个LocalDateTime对象作为参数传递给Duration.between方法。然后,可以使用getSeconds方法获取到时间间隔的秒数。以下是一个示例代码: ```java LocalDateTime dateTime1 = LocalDateTime.of(2021, 1, 1, 12, 0, 0); LocalDateTime dateTime2 = LocalDateTime.of(2021, 1, 1, 12, 0, 10); Duration duration = Duration.between(dateTime1, dateTime2); long seconds = duration.getSeconds(); System.out.println("两个LocalDateTime相隔的秒数:" + seconds); ``` 在这个示例中,我们将dateTime1设置为2021年11日12:00:00,将dateTime2设置为2021年11日12:00:10。通过Duration.between方法计算得到的时间间隔的秒数为10秒。因此,答案是两个LocalDateTime相隔的秒数为10秒。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [java 8 两个LocalDateTime类型时间相差几个](https://blog.csdn.net/weixin_35751194/article/details/128868194)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_tab","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^insert_down28v1"}}] [.reference_item style="max-width: 50%"] - *2* [java中LocalDateTime计算两个时间间隔,转换为时-分-秒](https://blog.csdn.net/weixin_47315082/article/details/131641490)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_tab","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^insert_down28v1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值