ccb8baf8 sonin

运营周报 根据指定周获取时间

1 个父辈 b8c39a0a
......@@ -15,7 +15,9 @@ import com.skua.modules.custom.vo.DepartEquipRemarkVO;
import com.skua.modules.custom.vo.DepartWeekReportVO;
import com.skua.modules.custom.vo.FCustomReportDatasetVO;
import java.text.DecimalFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
......@@ -226,23 +228,47 @@ public class FCustomReportDatasetServiceImpl extends ServiceImpl<FCustomReportDa
*/
public static Map<String,String> getWeekDate(String yearMonth,String week) {
Map<String,String> map = new HashMap<>();
int weekOfMonth = Integer.valueOf(week); // 输入的第几周
try {
// 解析年月并构造本月的第一天日期
LocalDate firstOfMonth = LocalDate.parse(yearMonth + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
// 获取本月的第一个星期一
WeekFields weekFields = WeekFields.of(Locale.getDefault());
LocalDate firstMondayOfMonth = firstOfMonth.with(TemporalAdjusters.firstInMonth(weekFields.getFirstDayOfWeek()));
// 计算目标周的开始和结束日期
LocalDate startOfWeek = firstMondayOfMonth.plusWeeks(weekOfMonth - 1).plusDays(1);
LocalDate endOfWeek = startOfWeek.plusDays(6);
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
map.put("start", startOfWeek.format(dateFormatter));
map.put("end", endOfWeek.format(dateFormatter));
} catch (Exception e) {
map.put("start", "");
map.put("end", "");
// int weekOfMonth = Integer.valueOf(week); // 输入的第几周
// try {
// // 解析年月并构造本月的第一天日期
// LocalDate firstOfMonth = LocalDate.parse(yearMonth + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
// // 获取本月的第一个星期一
// WeekFields weekFields = WeekFields.of(Locale.getDefault());
// LocalDate firstMondayOfMonth = firstOfMonth.with(TemporalAdjusters.firstInMonth(weekFields.getFirstDayOfWeek()));
// // 计算目标周的开始和结束日期
// LocalDate startOfWeek = firstMondayOfMonth.plusWeeks(weekOfMonth - 1).plusDays(1);
// LocalDate endOfWeek = startOfWeek.plusDays(6);
// DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// map.put("start", startOfWeek.format(dateFormatter));
// map.put("end", endOfWeek.format(dateFormatter));
// } catch (Exception e) {
// map.put("start", "");
// map.put("end", "");
// }
int year = Integer.parseInt(yearMonth.split("-")[0]);
int month = Integer.parseInt(yearMonth.split("-")[1]);
int weekNumber = Integer.parseInt(week);
YearMonth yearMonth0 = YearMonth.of(year, month);
LocalDate firstDayOfMonth = yearMonth0.atDay(1);
// 计算该月第一周的开始日期
LocalDate firstWeekStart = firstDayOfMonth.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
// 计算指定周次的开始日期
LocalDate startDate = firstWeekStart.plusWeeks(weekNumber - 1);
// 如果开始日期超出了该月范围,说明该月没有指定的周次
// if (startDate.getMonthValue() != month) {
// throw new IllegalArgumentException("该月没有第 " + weekNumber + " 周");
// }
// 计算结束日期
LocalDate endDate = startDate.plusDays(6);
if (endDate.getMonthValue() != month) {
endDate = yearMonth0.atEndOfMonth();
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String startDateStr = startDate.format(formatter);
String endDateStr = endDate.format(formatter);
map.put("start", startDateStr);
map.put("end", endDateStr);
return map;
}
//节能环保报表数据
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!