java 中获取日期后一天和前一天代码
时间: 2024-05-04 22:15:39 浏览: 93
java获取当前时间和前一天日期(实现代码)
5星 · 资源好评率100%
获取日期后一天代码:
```
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class NextDayExample {
public static void main(String[] args) {
// create a date
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// add one day to the date
calendar.add(Calendar.DAY_OF_YEAR, 1);
date = calendar.getTime();
// format the date
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String nextDay = format.format(date);
System.out.println("Next day: " + nextDay);
}
}
```
获取日期前一天代码:
```
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class PreviousDayExample {
public static void main(String[] args) {
// create a date
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// subtract one day from the date
calendar.add(Calendar.DAY_OF_YEAR, -1);
date = calendar.getTime();
// format the date
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String previousDay = format.format(date);
System.out.println("Previous day: " + previousDay);
}
}
```
阅读全文