LocalDateTime获取去年月份
时间: 2023-10-26 10:27:42 浏览: 96
要获取去年的月份,可以使用LocalDateTime类的withYear()和withMonth()方法。首先,获取当前的LocalDateTime对象,然后使用withYear()方法将年份设置为去年所对应的年份,最后使用withMonth()方法将月份设置为需要获取的月份。具体代码如下所示:
```
LocalDateTime now = LocalDateTime.now();
LocalDateTime lastYear = now.withYear(now.getYear() - 1);
int month = lastYear.getMonthValue();
```
在这段代码中,首先获取当前的LocalDateTime对象now,然后使用withYear()方法将年份设置为去年,得到去年的LocalDateTime对象lastYear。最后,使用getMonthValue()方法获取去年的月份。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
#### 引用[.reference_title]
- *1* *3* [Date,LocalDateTime类型,获取今年开始时间,获取去年的今天。](https://blog.csdn.net/weixin_48720080/article/details/127323504)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *4* [localdatetime_Localdate_Localtime](https://blog.csdn.net/weixin_45379019/article/details/129590178)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文