创建测试类TestCalendar 设置当前时间为2023年5月29日 2.提示30天后进行期末考试3.返回改变后的日期
时间: 2024-02-16 08:05:31 浏览: 18
好的,以下是Java代码实现:
```java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class TestCalendar {
public static void main(String[] args) {
// 设置当前时间为2023年5月29日
LocalDate currentDate = LocalDate.of(2023, 5, 29);
System.out.println("当前时间:" + formatDate(currentDate));
// 30天后进行期末考试
LocalDate examDate = currentDate.plusDays(30);
System.out.println("期末考试时间:" + formatDate(examDate));
}
// 格式化日期
private static String formatDate(LocalDate date) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return date.format(formatter);
}
}
```
执行上述代码,控制台输出如下:
```
当前时间:2023-05-29
期末考试时间:2023-06-28
```
其中,`LocalDate` 类是 Java 8 引入的日期时间 API,它提供了许多日期时间操作方法,比如 `plusDays` 方法用于增加天数。`formatDate` 方法用于将日期格式化为指定格式。