Calendar 获取前一天的年月日
时间: 2024-02-29 14:22:58 浏览: 133
获取年月日
获取前一天的年月日可以通过以下步骤实现:
1. 获取当前日期
```java
Calendar cal = Calendar.getInstance();
```
2. 通过Calendar对象减去一天
```java
cal.add(Calendar.DATE, -1);
```
3. 获取减去一天后的年月日
```java
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
```
完整代码如下:
```java
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
System.out.println("前一天的年月日为:" + year + "-" + month + "-" + day);
```
阅读全文