帮我用Java封装一个方法 当我传入一个某一年的数字参数时 获取到该年的最大时间和最小时间 封装成map返回
时间: 2024-03-12 07:43:46 浏览: 56
好的,下面是代码:
```java
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
public class YearUtil {
public static Map<String, LocalDateTime> getYearMinMax(int year) {
Map<String, LocalDateTime> map = new HashMap<>();
LocalDate localDate = LocalDate.of(year, 1, 1);
LocalDateTime minDateTime = localDate.atStartOfDay();
LocalDateTime maxDateTime = localDate.withDayOfYear(localDate.lengthOfYear()).atTime(23, 59, 59, 999999999);
map.put("minDateTime", minDateTime);
map.put("maxDateTime", maxDateTime);
return map;
}
}
```
这个方法接收一个int类型的year作为参数,返回一个包含最大时间和最小时间的Map,其中键是字符串类型的"minDateTime"和"maxDateTime",值是LocalDateTime类型的时间。
阅读全文
相关推荐











