根据年份+月度+月获取开始时间和结束时间
程序员文章站
2024-03-22 23:01:22
...
接收前端年份、月度、月获取开始时间和结束时间
/**
* @param year 年为0则是全部,后面的季度和月份就不用选择了
* @param quarter 季度[0,1,2,3,4],0是全部,剩下的是四个季度,1-3,4-6,7,9,10-12
* @param month 月份[0-12]
* @return
*/
public static List<Date> getDateList(int year, int quarter, int month) {
List<Date> list = new ArrayList<>(2);
LocalDateTime d1 = null;
LocalDateTime d2 = null;
LocalDateTime localDate = LocalDateTime.now();
if (year == 0 || (localDate.getYear() != year && localDate.getYear() - 1 != year)) {
list.add(null);
list.add(null);
return list;
}
//全年
if (quarter == 0 && month == 0) {
d1 = LocalDateTime.of(year, Month.JANUARY, 1, 0, 0);
d2 = LocalDateTime.of(year, Month.DECEMBER, 31, 23, 59);
} else {
switch (quarter) {
case 1:
d1 = LocalDateTime.of(year, Month.JANUARY, 1, 0, 0);
d2 = LocalDateTime.of(year, Month.MARCH, 31, 23, 59);
break;
case 2:
d1 = LocalDateTime.of(year, Month.APRIL, 1, 0, 0);
d2 = LocalDateTime.of(year, Month.JUNE, 30, 23, 59);
break;
case 3:
d1 = LocalDateTime.of(year, Month.JULY, 1, 0, 0);
d2 = LocalDateTime.of(year, Month.SEPTEMBER, 30, 23, 59);
break;
case 4:
d1 = LocalDateTime.of(year, Month.OCTOBER, 1, 0, 0);
d2 = LocalDateTime.of(year, Month.DECEMBER, 31, 23, 59);
break;
default:
}
if (month != 0) {
d1 = LocalDateTime.of(year, month, 1, 0, 0);
d2 = d1.with(TemporalAdjusters.lastDayOfMonth());
}
}
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt1 = d1.atZone(zoneId);
ZonedDateTime zdt2 = d2.atZone(zoneId);
Date from = Date.from(zdt1.toInstant());
Date to = Date.from(zdt2.toInstant());
list.add(from);
list.add(to);
return list;
}
上一篇: 采坑记录
下一篇: 【Android】登录界面的实现