生成 当前时间与指定时间3个月之前 的时间 比较 java代码
时间: 2024-05-09 17:19:02 浏览: 5
以下是生成当前时间与指定时间3个月之前的时间比较的Java代码:
```java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// 获取当前时间
LocalDate now = LocalDate.now();
// 获取3个月之前的时间
LocalDate threeMonthsAgo = now.minusMonths(3);
// 格式化时间字符串
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String nowStr = now.format(formatter);
String threeMonthsAgoStr = threeMonthsAgo.format(formatter);
// 比较两个时间字符串并输出结果
if (nowStr.compareTo(threeMonthsAgoStr) > 0) {
System.out.println("当前时间晚于3个月之前的时间");
} else if (nowStr.compareTo(threeMonthsAgoStr) < 0) {
System.out.println("当前时间早于3个月之前的时间");
} else {
System.out.println("当前时间等于3个月之前的时间");
}
}
}
```