this week,last week,this month,last month

本文介绍了如何根据用户选择计算不同时间段的注册日期范围,包括全部时间、本周、上周、本月和上个月。
var registerDateRange = (function(option){
            var dateRange = {};
            var sysDate = new Date();
            switch(option) {
            case '0' : // all
                break;
            case '1' :  // this week
                sysDate.setDate(sysDate.getDate() - (sysDate.getDay() - 1));
                dateRange.from = sysDate;
                dateRange.to = new Date();
                break;
            case '2' :  // last week
                sysDate.setDate(sysDate.getDate() - (sysDate.getDay() + 6));
                dateRange.from = sysDate;
                
                var toDate = new Date();
                toDate.setDate(sysDate.getDate() + 6);
                dateRange.to = toDate;
                break;
            case '3' : // this month
                sysDate.setDate(sysDate.getDate() - (sysDate.getDate() - 1));
                dateRange.from = sysDate;
                dateRange.to = new Date();
                break;
            case '4' : // last month
                sysDate.setDate(sysDate.getDate() - sysDate.getDate());
                dateRange.to = sysDate;
                
                var fromDate = new Date(sysDate);
                fromDate.setDate(fromDate.getDate() - (fromDate.getDate() - 1) );
                dateRange.from = fromDate;
                break;
            }
            return dateRange;
        })($("#registerDate").val());

 

转载于:https://www.cnblogs.com/shouwangzhe-/p/3986119.html

import java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; import java.util.ArrayList; import java.util.List; public class WeekSummaryUtils { // 主方法:根据当前日期判断是否跨月,返回对应的 week_id 列表 public static String getValidWeekIDs(LocalDate currentDate) { // 当前周的起始(周一)和结束(周日) LocalDate startOfWeek = currentDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); LocalDate endOfWeek = startOfWeek.plusDays(6); // 判断该周是否属于当前月 boolean isWeekInCurrentMonth = startOfWeek.getMonth() == currentDate.getMonth() && startOfWeek.getYear() == currentDate.getYear(); List<String> validWeekIDs = new ArrayList<>(); if (!isWeekInCurrentMonth) { // 周起始不在当前月:获取上个月所有完整周 validWeekIDs.addAll(getLastMonthFullWeeks(currentDate)); } else { // 周起始在当前月:添加当前周(不管是否完整) + 所有完整前周 validWeekIDs.add(generateWeekID(startOfWeek)); validWeekIDs.addAll(getPreviousFullWeeks(currentDate)); } return String.join(",", validWeekIDs); } // 根据起始日期生成 week_id(正确逻辑) private static String generateWeekID(LocalDate startOfWeek) { int year = startOfWeek.getYear(); Month month = startOfWeek.getMonth(); int weekNumber = (startOfWeek.getDayOfMonth() - 1) / 7 + 1; return String.format("%d%02d_%d", year, month.getValue(), weekNumber); } // 获取当前月中当前周之前的完整周 private static List<String> getPreviousFullWeeks(LocalDate currentDate) { List<String> weeks = new ArrayList<>(); LocalDate firstDayOfMonth = currentDate.with(TemporalAdjusters.firstDayOfMonth()); LocalDate currentMonday = currentDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); // 从“当前周的上一周”开始往前找 LocalDate current = currentMonday.minusWeeks(1); while (!current.isBefore(firstDayOfMonth)) { LocalDate monday = current.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); // 只要周一起始日在当前月内,就保留 if (monday.getMonth() == currentDate.getMonth() && monday.getYear() == currentDate.getYear()) { weeks.add(0, generateWeekID(monday)); } current = current.minusWeeks(1); } return weeks; } private static List<String> getLastMonthFullWeeks(LocalDate currentDate) { LocalDate lastMonth = currentDate.minusMonths(1); // 比如 2025-07 -> 2025-06 LocalDate firstDayOfLastMonth = lastMonth.with(TemporalAdjusters.firstDayOfMonth()); LocalDate lastDayOfLastMonth = lastMonth.with(TemporalAdjusters.lastDayOfMonth()); List<String> weeks = new ArrayList<>(); LocalDate current = lastDayOfLastMonth; while (!current.isBefore(firstDayOfLastMonth)) { LocalDate sunday = current.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); LocalDate monday = sunday.minusDays(6); if (monday.getMonth() == lastMonth.getMonth() && monday.getYear() == lastMonth.getYear()) { weeks.add(0, generateWeekID(monday)); } current = current.minusWeeks(1); } return weeks; } // 测试主方法 public static void main(String[] args) { LocalDate date1 = LocalDate.of(2025, 8, 1); System.out.println("date1 对应的 week_id:" + getValidWeekIDs(date1)); LocalDate date2 = LocalDate.of(2025, 8, 7); System.out.println("date2 对应的 week_id:" + getValidWeekIDs(date2)); } } 为什么date2输出只有202508_1,怎么不包含日期本周的
08-08
/* * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.dura.common.utils; import org.apache.commons.lang3.StringUtils; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.YearMonth; import java.time.format.DateTimeFormatter; import java.time.temporal.IsoFields; import java.time.temporal.TemporalAdjusters; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class DateTimeUtil { public static final Pattern SPECIFIC_YEAR_MONTH_DAY_PATTERN = Pattern.compile("\\d{4}年\\d{2}月\\d{2}日"); public static final Pattern GENERAL_YEAR_MONTH_DAY_PATTERN = Pattern.compile("(今年|去年|前年|明年|后年)(\\d{2}月\\d{2}日)"); public static final Pattern GENERAL_MONTH_DAY_PATTERN = Pattern.compile("(本月|上月|上上月|下月)(\\d{2}日)"); public static final Pattern GENERAL_DAY_PATTERN = Pattern.compile("(今天|昨天|前天|明天|后天|上月今天|上上月今天)"); public static final Pattern WEEK_DAY_PATTERN = Pattern.compile("本周第(\\d)天"); public static final Pattern GENERAL_MONTH_LAST_DAY_PATTERN = Pattern.compile("(本月|上月)最后一天"); public static final Pattern GENERAL_YEAR_MONTH_LAST_DAY_PATTERN = Pattern.compile("(今年)(\\d{2})月最后一天"); public static final Pattern GENERAL_WEEK_SPECIFIC_DAY_PATTERN = Pattern.compile("(本周|上周|上上周|下周|下下周)星期(\\d)"); public static final Pattern SPECIFIC_YEAR_MONTH_PATTERN = Pattern.compile("\\d{4}年\\d{2}月"); public static final Pattern GENERAL_YEAR_MONTH_PATTERN = Pattern.compile("(今年|去年|前年|明年|后年)(\\d{2}月)"); public static final Pattern GENERAL_MONTH_PATTERN = Pattern.compile("(本月|上月|上上月|下月|去年本月)"); public static final Pattern SPECIFIC_YEAR_PATTERN = Pattern.compile("(\\d{4})年"); public static final Pattern GENERAL_YEAR_PATTERN = Pattern.compile("(今年|去年|前年|明年|后年)"); public static final Pattern SPECIFIC_YEAR_QUARTER_PATTERN = Pattern.compile("\\d{4}年第\\d季度"); public static final Pattern GENERAL_YEAR_QUARTER_PATTERN = Pattern.compile("(今年|去年|前年|明年|后年)(第\\d季度)"); public static final Pattern GENERAL_QUARTER_PATTERN = Pattern.compile("(本季度|上季度|下季度|去年本季度)"); public static final Pattern GENERAL_WEEK_PATTERN = Pattern.compile("(本周|上周|上上周|下周|下下周)"); public static final Pattern SPECIFIC_YEAR_WEEK_PATTERN = Pattern.compile("(\\d{4})年第(\\d{2})周"); public static final Pattern SPECIFIC_YEAR_MONTH_WEEK_PATTERN = Pattern.compile("(\\d{4})年(\\d{2})月第(\\d)周"); public static final Pattern GENERAL_YEAR_WEEK_PATTERN = Pattern.compile("(今年|去年|前年|明年|后年)第(\\d{2})周"); public static final Pattern GENERAL_MONTH_WEEK_PATTERN = Pattern.compile("(本月|上月)第(\\d)周"); public static final Pattern GENERAL_YEAR_MONTH_WEEK_PATTERN = Pattern.compile("(今年|去年|前年|明年|后年)(\\d{2})月第(\\d)周"); public static final Pattern SPECIFIC_YEAR_MONTH_LAST_WEEK_PATTERN = Pattern.compile("(\\d{4})年(\\d{2})月最后一周"); public static final Pattern GENERAL_MONTH_LAST_WEEK_PATTERN = Pattern.compile("(本月|上月|上上月)最后一周"); public static final Pattern SPECIFIC_YEAR_MONTH_COMPLETE_WEEK_PATTERN = Pattern .compile("(\\d{4})年(\\d{2})月第(\\d)个完整周"); public static final Pattern GENERAL_YEAR_COMPLETE_WEEK_PATTERN = Pattern.compile("(今年|去年|前年|明年|后年)第(\\d{2})个完整周"); public static final Pattern SPECIFIC_YEAR_COMPLETE_WEEK_PATTERN = Pattern.compile("(\\d{4})年第(\\d{2})个完整周"); public static final Pattern GENERAL_YEAR_MONTH_COMPLETE_WEEK_PATTERN = Pattern .compile("(今年|去年|前年|明年|后年)(\\d{2})月第(\\d)个完整周"); public static final Pattern GENERAL_MONTH_COMPLETE_WEEK_PATTERN = Pattern.compile("(本月|上月)第(\\d)个完整周"); public static final Pattern GENERAL_MONTH_LAST_COMPLETE_WEEK_PATTERN = Pattern.compile("(本月|上月|上上月)最后一个完整周"); public static final Pattern RECENT_N_YEAR_PATTERN = Pattern.compile("近(\\d+)年"); public static final Pattern RECENT_N_MONTH_PATTERN = Pattern.compile("近(\\d+)个月"); public static final Pattern RECENT_N_WEEK_PATTERN = Pattern.compile("近(\\d+)周"); public static final Pattern RECENT_N_DAY_PATTERN = Pattern.compile("近(\\d+)天"); public static final Pattern RECENT_N_COMPLETE_YEAR_PATTERN = Pattern.compile("近(\\d+)个完整年"); public static final Pattern RECENT_N_COMPLETE_QUARTER_PATTERN = Pattern.compile("近(\\d+)个完整季度"); public static final Pattern RECENT_N_COMPLETE_MONTH_PATTERN = Pattern.compile("近(\\d+)个完整月"); public static final Pattern RECENT_N_COMPLETE_WEEK_PATTERN = Pattern.compile("近(\\d+)个完整周"); public static final Pattern RECENT_N_DAY_WITHOUT_TODAY_PATTERN = Pattern.compile("不包含今天的近(\\d+)天"); public static final Pattern RECENT_N_QUARTER_WITH_CURRENT_PATTERN = Pattern.compile("包含当前季度的近(\\d+)个季度"); public static final Pattern SPECIFIC_YEAR_HALF_YEAR_PATTERN = Pattern.compile("(\\d{4})年(上|下)半年"); public static final Pattern GENERAL_YEAR_HALF_YEAR_PATTERN = Pattern.compile("(今年|去年|前年|明年|后年)(上|下)半年"); public static final Pattern HALF_YEAR_PATTERN = Pattern.compile("(上|下)半年"); public static String buildDateTimeComment(List<String> expressions) { LocalDate now = LocalDate.now(); // Get year, month, day int year = now.getYear(); int month = now.getMonthValue(); int day = now.getDayOfMonth(); // Get current year's quarter int quarter = now.get(IsoFields.QUARTER_OF_YEAR); String todayComment = String.format("今天是%d年%02d月%02d日,是%d年的第%d季度", year, month, day, year, quarter); List<String> dateTimeCommentList = buildDateExpressions(expressions, now); StringBuilder finalExpression = new StringBuilder(); finalExpression.append(todayComment).append("\n"); finalExpression.append("需要计算的时间是:\n"); dateTimeCommentList.forEach(comment -> finalExpression.append(comment).append("\n")); return finalExpression.toString(); } public static List<String> buildDateExpressions(List<String> expressions, LocalDate now) { List<String> dateTimeCommentList = new ArrayList<>(); for (String expression : expressions) { Matcher specificYearMonthDayMatcher = SPECIFIC_YEAR_MONTH_DAY_PATTERN.matcher(expression); if (specificYearMonthDayMatcher.matches()) { dateTimeCommentList.add(expression + "=" + expression); continue; } Matcher generalYearMonthDayMatcher = GENERAL_YEAR_MONTH_DAY_PATTERN.matcher(expression); if (generalYearMonthDayMatcher.matches()) { String yearEx = generalYearMonthDayMatcher.group(1); String comment = getYearEx(now, yearEx, false) + generalYearMonthDayMatcher.group(2); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalMonthDayMatcher = GENERAL_MONTH_DAY_PATTERN.matcher(expression); if (generalMonthDayMatcher.matches()) { String monthEx = generalMonthDayMatcher.group(1); String comment = getMonthEx(now, monthEx) + generalMonthDayMatcher.group(2); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher yearMonthLastDayMatcher = GENERAL_YEAR_MONTH_LAST_DAY_PATTERN.matcher(expression); if (yearMonthLastDayMatcher.matches()) { String yearEx = yearMonthLastDayMatcher.group(1); String monthEx = yearMonthLastDayMatcher.group(2); String comment = getGeneralYearMonthLastDayEx(now, yearEx, Integer.valueOf(monthEx)); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher monthLastDayMatcher = GENERAL_MONTH_LAST_DAY_PATTERN.matcher(expression); if (monthLastDayMatcher.matches()) { String monthEx = monthLastDayMatcher.group(1); String comment = getMonthLastDayEx(now, monthEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher weekDayMatcher = WEEK_DAY_PATTERN.matcher(expression); if (weekDayMatcher.matches()) { int weekDay = Integer.parseInt(weekDayMatcher.group(1)); String comment = getWeekDayEx(now, weekDay); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalWeekDayMatcher = GENERAL_WEEK_SPECIFIC_DAY_PATTERN.matcher(expression); if (generalWeekDayMatcher.matches()) { String weekEx = generalWeekDayMatcher.group(1); int day = Integer.parseInt(generalWeekDayMatcher.group(2)); String comment = getGeneralWeekDayEx(now, weekEx, day); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher specificYearQuarterMatcher = SPECIFIC_YEAR_QUARTER_PATTERN.matcher(expression); if (specificYearQuarterMatcher.matches()) { dateTimeCommentList.add(expression + "=" + expression); continue; } Matcher generalYearQuarterMatcher = GENERAL_YEAR_QUARTER_PATTERN.matcher(expression); if (generalYearQuarterMatcher.matches()) { String yearEx = generalYearQuarterMatcher.group(1); String quarterEx = generalYearQuarterMatcher.group(2); String comment = getYearEx(now, yearEx, false) + quarterEx; dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalQuarterMatcher = GENERAL_QUARTER_PATTERN.matcher(expression); if (generalQuarterMatcher.matches()) { String quarterEx = generalQuarterMatcher.group(1); String comment = getQuarterEx(now, quarterEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalWeekMatcher = GENERAL_WEEK_PATTERN.matcher(expression); if (generalWeekMatcher.matches()) { String weekEx = generalWeekMatcher.group(1); String comment = getWeekEx(now, weekEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher specificYearWeekMatcher = SPECIFIC_YEAR_WEEK_PATTERN.matcher(expression); if (specificYearWeekMatcher.matches()) { int yearEx = Integer.parseInt(specificYearWeekMatcher.group(1)); int weekEx = Integer.parseInt(specificYearWeekMatcher.group(2)); String comment = getSpecificYearWeekEx(now, yearEx, weekEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalYearWeekMatcher = GENERAL_YEAR_WEEK_PATTERN.matcher(expression); if (generalYearWeekMatcher.matches()) { String yearEx = generalYearWeekMatcher.group(1); int weekEx = Integer.parseInt(generalYearWeekMatcher.group(2)); String comment = getGeneralYearWeekEx(now, yearEx, weekEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalMonthWeekMatcher = GENERAL_MONTH_WEEK_PATTERN.matcher(expression); if (generalMonthWeekMatcher.matches()) { String monthEx = generalMonthWeekMatcher.group(1); int weekEx = Integer.parseInt(generalMonthWeekMatcher.group(2)); String comment = getGeneralMonthWeekEx(now, monthEx, weekEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher specificYearMonthLastWeekMatcher = SPECIFIC_YEAR_MONTH_LAST_WEEK_PATTERN.matcher(expression); if (specificYearMonthLastWeekMatcher.matches()) { int yearEx = Integer.parseInt(specificYearMonthLastWeekMatcher.group(1)); int monthEx = Integer.parseInt(specificYearMonthLastWeekMatcher.group(2)); String comment = getSpecificYearMonthLastWeek(now, yearEx, monthEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalMonthLastWeekMatcher = GENERAL_MONTH_LAST_WEEK_PATTERN.matcher(expression); if (generalMonthLastWeekMatcher.matches()) { String monthEx = generalMonthLastWeekMatcher.group(1); String comment = getGeneralMonthLastWeek(now, monthEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalMonthLastCompleteWeekMatcher = GENERAL_MONTH_LAST_COMPLETE_WEEK_PATTERN.matcher(expression); if (generalMonthLastCompleteWeekMatcher.matches()) { String monthEx = generalMonthLastCompleteWeekMatcher.group(1); String comment = getGeneralMonthLastCompleteWeekEx(now, monthEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher recentNYearMatcher = RECENT_N_YEAR_PATTERN.matcher(expression); if (recentNYearMatcher.matches()) { int n = Integer.parseInt(recentNYearMatcher.group(1)); String comment = getRecentNYear(now, n); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher recentNMonthMatcher = RECENT_N_MONTH_PATTERN.matcher(expression); if (recentNMonthMatcher.matches()) { int n = Integer.parseInt(recentNMonthMatcher.group(1)); String comment = getRecentNMonth(now, n); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher recentNWeekMatcher = RECENT_N_WEEK_PATTERN.matcher(expression); if (recentNWeekMatcher.matches()) { int n = Integer.parseInt(recentNWeekMatcher.group(1)); String comment = getRecentNWeek(now, n); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher recentNDayWithoutTodayMatcher = RECENT_N_DAY_WITHOUT_TODAY_PATTERN.matcher(expression); if (recentNDayWithoutTodayMatcher.matches()) { int n = Integer.parseInt(recentNDayWithoutTodayMatcher.group(1)); String comment = getRecentNDayWithoutToday(now, n); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher recentNDayMatcher = RECENT_N_DAY_PATTERN.matcher(expression); if (recentNDayMatcher.matches()) { int n = Integer.parseInt(recentNDayMatcher.group(1)); String comment = getRecentNDay(now, n); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher recentNCompleteYearMatcher = RECENT_N_COMPLETE_YEAR_PATTERN.matcher(expression); if (recentNCompleteYearMatcher.matches()) { int n = Integer.parseInt(recentNCompleteYearMatcher.group(1)); String comment = getRecentNCompleteYear(now, n); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher recentNCompleteQuarterMatcher = RECENT_N_COMPLETE_QUARTER_PATTERN.matcher(expression); if (recentNCompleteQuarterMatcher.matches()) { int n = Integer.parseInt(recentNCompleteQuarterMatcher.group(1)); String comment = getRecentNCompleteQuarter(now, n); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher recentNCompleteMonthMatcher = RECENT_N_COMPLETE_MONTH_PATTERN.matcher(expression); if (recentNCompleteMonthMatcher.matches()) { int n = Integer.parseInt(recentNCompleteMonthMatcher.group(1)); String comment = getRecentNCompleteMonth(now, n); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher recentNCompleteWeekMatcher = RECENT_N_COMPLETE_WEEK_PATTERN.matcher(expression); if (recentNCompleteWeekMatcher.matches()) { int n = Integer.parseInt(recentNCompleteWeekMatcher.group(1)); String comment = getRecentNCompleteWeek(now, n); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher recentNQuarterWithCurrentMatcher = RECENT_N_QUARTER_WITH_CURRENT_PATTERN.matcher(expression); if (recentNQuarterWithCurrentMatcher.matches()) { int n = Integer.parseInt(recentNQuarterWithCurrentMatcher.group(1)); String comment = getRecentNQuarterWithCurrent(now, n); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher specificYearMonthMatcher = SPECIFIC_YEAR_MONTH_PATTERN.matcher(expression); if (specificYearMonthMatcher.matches()) { dateTimeCommentList.add(expression + "=" + expression); continue; } Matcher generalYearMonthMatcher = GENERAL_YEAR_MONTH_PATTERN.matcher(expression); if (generalYearMonthMatcher.matches()) { String yearEx = generalYearMonthMatcher.group(1); String comment = getYearEx(now, yearEx, false) + generalYearMonthMatcher.group(2); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalDayMatcher = GENERAL_DAY_PATTERN.matcher(expression); if (generalDayMatcher.matches()) { String dayEx = generalDayMatcher.group(1); String comment = getDayEx(now, dayEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalMonthMatcher = GENERAL_MONTH_PATTERN.matcher(expression); if (generalMonthMatcher.matches()) { String monthEx = generalMonthMatcher.group(1); String comment = getMonthEx(now, monthEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher specificYearMatcher = SPECIFIC_YEAR_PATTERN.matcher(expression); if (specificYearMatcher.matches()) { int yearEx = Integer.parseInt(specificYearMatcher.group(1)); String comment = String.valueOf(yearEx) + "年"; dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalYearMatcher = GENERAL_YEAR_PATTERN.matcher(expression); if (generalYearMatcher.matches()) { String yearEx = generalYearMatcher.group(1); String comment = getYearEx(now, yearEx, true); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher specificYearMonthWeekMatcher = SPECIFIC_YEAR_MONTH_WEEK_PATTERN.matcher(expression); if (specificYearMonthWeekMatcher.matches()) { int yearEx = Integer.parseInt(specificYearMonthWeekMatcher.group(1)); int monthEx = Integer.parseInt(specificYearMonthWeekMatcher.group(2)); int weekEx = Integer.parseInt(specificYearMonthWeekMatcher.group(3)); String comment = getSpecificYearMonthWeekEx(now, yearEx, monthEx, weekEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalYearMonthWeekMatcher = GENERAL_YEAR_MONTH_WEEK_PATTERN.matcher(expression); if (generalYearMonthWeekMatcher.matches()) { String yearEx = generalYearMonthWeekMatcher.group(1); int monthEx = Integer.parseInt(generalYearMonthWeekMatcher.group(2)); int weekEx = Integer.parseInt(generalYearMonthWeekMatcher.group(3)); String comment = getGeneralYearMonthWeekEx(now, yearEx, monthEx, weekEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher specificYearMonthCompleteWeekMatcher = SPECIFIC_YEAR_MONTH_COMPLETE_WEEK_PATTERN .matcher(expression); if (specificYearMonthCompleteWeekMatcher.matches()) { int yearEx = Integer.parseInt(specificYearMonthCompleteWeekMatcher.group(1)); int monthEx = Integer.parseInt(specificYearMonthCompleteWeekMatcher.group(2)); int weekEx = Integer.parseInt(specificYearMonthCompleteWeekMatcher.group(3)); String comment = getSpecificYearMonthCompleteWeekEx(now, yearEx, monthEx, weekEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalYearMonthCompleteWeekMatcher = GENERAL_YEAR_MONTH_COMPLETE_WEEK_PATTERN.matcher(expression); if (generalYearMonthCompleteWeekMatcher.matches()) { String yearEx = generalYearMonthCompleteWeekMatcher.group(1); int monthEx = Integer.parseInt(generalYearMonthCompleteWeekMatcher.group(2)); int weekEx = Integer.parseInt(generalYearMonthCompleteWeekMatcher.group(3)); String comment = getGeneralYearMonthCompleteWeekEx(now, yearEx, monthEx, weekEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalMonthCompleteWeekMatcher = GENERAL_MONTH_COMPLETE_WEEK_PATTERN.matcher(expression); if (generalMonthCompleteWeekMatcher.matches()) { String monthEx = generalMonthCompleteWeekMatcher.group(1); int weekEx = Integer.parseInt(generalMonthCompleteWeekMatcher.group(2)); String comment = getGeneralMonthCompleteWeekEx(now, monthEx, weekEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher specificYearCompleteWeekMatcher = SPECIFIC_YEAR_COMPLETE_WEEK_PATTERN.matcher(expression); if (specificYearCompleteWeekMatcher.matches()) { int yearEx = Integer.parseInt(specificYearCompleteWeekMatcher.group(1)); int weekEx = Integer.parseInt(specificYearCompleteWeekMatcher.group(2)); String comment = getSpecificYearCompleteWeekEx(now, yearEx, weekEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalYearCompleteWeekMatcher = GENERAL_YEAR_COMPLETE_WEEK_PATTERN.matcher(expression); if (generalYearCompleteWeekMatcher.matches()) { String yearEx = generalYearCompleteWeekMatcher.group(1); int weekEx = Integer.parseInt(generalYearCompleteWeekMatcher.group(2)); String comment = getGeneralYearCompleteWeekEx(now, yearEx, weekEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher specificYearHalfYearMatcher = SPECIFIC_YEAR_HALF_YEAR_PATTERN.matcher(expression); if (specificYearHalfYearMatcher.matches()) { int yearEx = Integer.parseInt(specificYearHalfYearMatcher.group(1)); String halfYearEx = specificYearHalfYearMatcher.group(2); String comment = getSpecificYearHalfYearEx(now, yearEx, halfYearEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher generalYearHalfYearMatcher = GENERAL_YEAR_HALF_YEAR_PATTERN.matcher(expression); if (generalYearHalfYearMatcher.matches()) { String yearEx = generalYearHalfYearMatcher.group(1); String halfYearEx = generalYearHalfYearMatcher.group(2); String comment = getGeneralYearHalfYearEx(now, yearEx, halfYearEx); dateTimeCommentList.add(expression + "=" + comment); continue; } Matcher halfYearMatcher = HALF_YEAR_PATTERN.matcher(expression); if (halfYearMatcher.matches()) { String halfYearEx = halfYearMatcher.group(1); String comment = getSpecificYearHalfYearEx(now, now.getYear(), halfYearEx); dateTimeCommentList.add(expression + "=" + comment); continue; } } return dateTimeCommentList; } public static String getYearEx(LocalDate now, String yearEx, boolean applyDomainLogic) { String comment = ""; int year = 0; if (yearEx.equals("今年")) { year = now.getYear(); } else if (yearEx.equals("去年")) { year = now.getYear() - 1; } else if (yearEx.equals("前年")) { year = now.getYear() - 2; } else if (yearEx.equals("明年")) { year = now.getYear() + 1; } else if (yearEx.equals("后年")) { year = now.getYear() + 2; } comment = String.valueOf(year) + "年"; return comment; } public static String getMonthEx(LocalDate now, String monthEx) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月"); String comment = ""; if (monthEx.equals("本月")) { comment = formatter.format(YearMonth.from(now)); } else if (monthEx.equals("上月")) { comment = formatter.format(YearMonth.from(now).minusMonths(1)); } else if (monthEx.equals("上上月")) { comment = formatter.format(YearMonth.from(now).minusMonths(2)); } else if (monthEx.equals("下月")) { comment = formatter.format(YearMonth.from(now).plusMonths(1)); } else if (monthEx.equals("去年本月")) { comment = formatter.format(YearMonth.from(now).minusYears(1)); } return comment; } public static String getDayEx(LocalDate now, String dayEx) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); String comment = ""; try { if (dayEx.equals("今天")) { comment = formatter.format(now); } else if (dayEx.equals("昨天")) { comment = formatter.format(now.minusDays(1)); } else if (dayEx.equals("前天")) { comment = formatter.format(now.minusDays(2)); } else if (dayEx.equals("明天")) { comment = formatter.format(now.plusDays(1)); } else if (dayEx.equals("后天")) { comment = formatter.format(now.plusDays(2)); } else if (dayEx.equals("上月今天")) { comment = formatter.format(YearMonth.from(now).minusMonths(1).atDay(now.getDayOfMonth())); } else if (dayEx.equals("上上月今天")) { comment = formatter.format(YearMonth.from(now).minusMonths(2).atDay(now.getDayOfMonth())); } } catch (Exception e) { e.printStackTrace(); } return comment; } public static final String getWeekDayEx(LocalDate now, int x) { // Calculate date of first day of week (Monday) LocalDate monday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); // Get date of xth day of week by adding (x - 1) days LocalDate desiredDay = monday.plusDays(x - 1); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(desiredDay); } public static final String getGeneralWeekDayEx(LocalDate now, String weekEx, int day) { LocalDate thisMonday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); LocalDate desiredDay = thisMonday.plusDays(day - 1); if (weekEx.equals("本周")) { } else if (weekEx.equals("上周")) { desiredDay = desiredDay.minusWeeks(1); } else if (weekEx.equals("上上周")) { desiredDay = desiredDay.minusWeeks(2); } else if (weekEx.equals("下周")) { desiredDay = desiredDay.plusWeeks(1); } else if (weekEx.equals("下下周")) { desiredDay = desiredDay.plusWeeks(2); } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(desiredDay); } public static final String getWeekEx(LocalDate now, String weekEx) { LocalDate desireMonday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); LocalDate desireSunday = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); if (weekEx.equals("本周")) { } else if (weekEx.equals("上周")) { desireMonday = desireMonday.minusWeeks(1); desireSunday = desireSunday.minusWeeks(1); } else if (weekEx.equals("上上周")) { desireMonday = desireMonday.minusWeeks(2); desireSunday = desireSunday.minusWeeks(2); } else if (weekEx.equals("下周")) { desireMonday = desireMonday.plusWeeks(1); desireSunday = desireSunday.plusWeeks(1); } else if (weekEx.equals("下下周")) { desireMonday = desireMonday.plusWeeks(2); desireSunday = desireSunday.plusWeeks(2); } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(desireMonday) + "至" + formatter.format(desireSunday); } public static String getSpecificYearWeekEx(LocalDate now, int year, int week) { LocalDate firstDayOfYear = LocalDate.of(year, 1, 1); LocalDate targetWeekFirstDay = firstDayOfYear.plusWeeks(week - 1); LocalDate targetWeekLastDay = targetWeekFirstDay.plusDays(6); LocalDate lastDayOfYear = firstDayOfYear.with(TemporalAdjusters.lastDayOfYear()); if (lastDayOfYear.isBefore(targetWeekLastDay)) { targetWeekLastDay = lastDayOfYear; } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(targetWeekFirstDay) + "至" + formatter.format(targetWeekLastDay); } public static String getGeneralYearWeekEx(LocalDate now, String yearEx, int week) { int year = now.getYear(); if (yearEx.equals("今年")) { } else if (yearEx.equals("去年")) { year = now.getYear() - 1; } else if (yearEx.equals("前年")) { year = now.getYear() - 2; } else if (yearEx.equals("明年")) { year = now.getYear() + 1; } else if (yearEx.equals("后年")) { year = now.getYear() + 2; } return getSpecificYearWeekEx(now, year, week); } public static String getSpecificYearMonthWeekEx(LocalDate now, int year, int month, int week) { LocalDate firstDayOfMonth = LocalDate.of(year, month, 1); LocalDate targetWeekFirstDay = firstDayOfMonth.plusWeeks(week - 1); LocalDate targetWeekLastDay = targetWeekFirstDay.plusDays(6); LocalDate lastDayOfMonth = firstDayOfMonth.with(TemporalAdjusters.lastDayOfMonth()); if (lastDayOfMonth.isBefore(targetWeekLastDay)) { targetWeekLastDay = lastDayOfMonth; } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(targetWeekFirstDay) + "至" + formatter.format(targetWeekLastDay); } public static String getGeneralYearMonthWeekEx(LocalDate now, String yearEx, int month, int week) { int year = now.getYear(); if (yearEx.equals("今年")) { } else if (yearEx.equals("去年")) { year = now.getYear() - 1; } else if (yearEx.equals("前年")) { year = now.getYear() - 2; } else if (yearEx.equals("明年")) { year = now.getYear() + 1; } else if (yearEx.equals("后年")) { year = now.getYear() + 2; } return getSpecificYearMonthWeekEx(now, year, month, week); } public static String getGeneralMonthWeekEx(LocalDate now, String monthEx, int week) { int year = now.getYear(); int month = now.getMonthValue(); if (monthEx.equals("本月")) { } else if (monthEx.equals("上月")) { month = now.getMonthValue() - 1; if (month <= 0) { year--; month = 12 + month; } } LocalDate firstDayOfMonth = LocalDate.of(year, month, 1); LocalDate targetWeekFirstDay = firstDayOfMonth.plusWeeks(week - 1); LocalDate targetWeekLastDay = targetWeekFirstDay.plusDays(6); LocalDate lastDayOfMonth = firstDayOfMonth.with(TemporalAdjusters.lastDayOfMonth()); if (lastDayOfMonth.isBefore(targetWeekLastDay)) { targetWeekLastDay = lastDayOfMonth; } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(targetWeekFirstDay) + "至" + formatter.format(targetWeekLastDay); } public static String getSpecificYearMonthCompleteWeekEx(LocalDate now, int year, int month, int week) { LocalDate firstDayOfMonth = LocalDate.of(year, month, 1); LocalDate firstMonday = firstDayOfMonth.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); LocalDate targetStartDate = firstMonday.plusWeeks(week - 1); LocalDate targetEndDate = targetStartDate.plusDays(6); LocalDate lastDayOfMonth = firstDayOfMonth.with(TemporalAdjusters.lastDayOfMonth()); if (lastDayOfMonth.isBefore(targetEndDate)) { return StringUtils.EMPTY; } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(targetStartDate) + "至" + formatter.format(targetEndDate); } public static String getGeneralYearMonthCompleteWeekEx(LocalDate now, String yearEx, int month, int week) { int year = now.getYear(); if (yearEx.equals("今年")) { } else if (yearEx.equals("去年")) { year = now.getYear() - 1; } else if (yearEx.equals("前年")) { year = now.getYear() - 2; } else if (yearEx.equals("明年")) { year = now.getYear() + 1; } else if (yearEx.equals("后年")) { year = now.getYear() + 2; } return getSpecificYearMonthCompleteWeekEx(now, year, month, week); } public static String getGeneralMonthCompleteWeekEx(LocalDate now, String monthEx, int week) { int year = now.getYear(); int month = now.getMonthValue(); if (monthEx.equals("本月")) { } else if (monthEx.equals("上月")) { month = now.getMonthValue() - 1; if (month <= 0) { year--; month = 12 + month; } } else if (monthEx.equals("上上月")) { month = now.getMonthValue() - 2; if (month <= 0) { year--; month = 12 + month; } } else if (monthEx.equals("下月")) { month = now.getMonthValue() + 1; if (month > 12) { year++; month = month - 12; } } return getSpecificYearMonthCompleteWeekEx(now, year, month, week); } public static String getSpecificYearCompleteWeekEx(LocalDate now, int year, int week) { LocalDate firstDayOfYear = LocalDate.of(year, 1, 1); LocalDate firstMonday = firstDayOfYear.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); LocalDate targetStartDate = firstMonday.plusWeeks(week - 1); LocalDate targetEndDate = targetStartDate.plusDays(6); LocalDate lastDayOfYear = firstDayOfYear.with(TemporalAdjusters.lastDayOfYear()); if (lastDayOfYear.isBefore(targetEndDate)) { return StringUtils.EMPTY; } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(targetStartDate) + "至" + formatter.format(targetEndDate); } public static String getGeneralYearCompleteWeekEx(LocalDate now, String yearEx, int week) { int year = now.getYear(); if (yearEx.equals("今年")) { } else if (yearEx.equals("去年")) { year = now.getYear() - 1; } else if (yearEx.equals("前年")) { year = now.getYear() - 2; } else if (yearEx.equals("明年")) { year = now.getYear() + 1; } else if (yearEx.equals("后年")) { year = now.getYear() + 2; } return getSpecificYearCompleteWeekEx(now, year, week); } public static String getSpecificYearMonthLastWeek(LocalDate now, int year, int month) { LocalDate firstDayOfMonth = LocalDate.of(year, month, 1); LocalDate lastDayOfMonth = firstDayOfMonth.with(TemporalAdjusters.lastDayOfMonth()); LocalDate previousMonday = lastDayOfMonth.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(previousMonday) + "至" + formatter.format(lastDayOfMonth); } public static String getGeneralMonthLastWeek(LocalDate now, String monthEx) { int year = now.getYear(); int month = now.getMonthValue(); if (monthEx.equals("本月")) { } else if (monthEx.equals("上月")) { month = now.getMonthValue() - 1; if (month <= 0) { year--; month = 12 + month; } } else if (monthEx.equals("上上月")) { month = now.getMonthValue() - 2; if (month <= 0) { year--; month = 12 + month; } } else if (monthEx.equals("下月")) { month = now.getMonthValue() + 1; if (month > 12) { year++; month = month - 12; } } return getSpecificYearMonthLastWeek(now, year, month); } public static String getSpecificYearMonthLastCompleteWeekEx(LocalDate now, int year, int month) { LocalDate firstDayOfMonth = LocalDate.of(year, month, 1); LocalDate lastSunday = firstDayOfMonth.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY)); LocalDate lastMonday = lastSunday.minusDays(6); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(lastMonday) + "至" + formatter.format(lastSunday); } public static String getGeneralMonthLastCompleteWeekEx(LocalDate now, String monthEx) { int year = now.getYear(); int month = now.getMonthValue(); if (monthEx.equals("本月")) { } else if (monthEx.equals("上月")) { month = now.getMonthValue() - 1; if (month <= 0) { year--; month = 12 + month; } } else if (monthEx.equals("上上月")) { month = now.getMonthValue() - 2; if (month <= 0) { year--; month = 12 + month; } } else if (monthEx.equals("下月")) { month = now.getMonthValue() + 1; if (month > 12) { year++; month = month - 12; } } return getSpecificYearMonthLastCompleteWeekEx(now, year, month); } public static String getQuarterEx(LocalDate now, String quarterEx) { int currentQuarter = now.get(IsoFields.QUARTER_OF_YEAR); // Calculate previous and next quarters int lastQuarter = currentQuarter == 1 ? 4 : currentQuarter - 1; int nextQuarter = currentQuarter == 4 ? 1 : currentQuarter + 1; int currentYear = now.getYear(); int yearOfLastQuarter = (currentQuarter == 1) ? currentYear - 1 : currentYear; int yearOfNextQuarter = (currentQuarter == 4) ? currentYear + 1 : currentYear; int yearOfSameQuarterLastYear = currentYear - 1; String comment = ""; if (quarterEx.equals("本季度")) { comment = currentYear + "年第" + currentQuarter + "季度"; } else if (quarterEx.equals("上季度")) { comment = yearOfLastQuarter + "年第" + lastQuarter + "季度"; } else if (quarterEx.equals("下季度")) { comment = yearOfNextQuarter + "年第" + nextQuarter + "季度"; } else if (quarterEx.equals("去年本季度")) { comment = yearOfSameQuarterLastYear + "年第" + currentQuarter + "季度"; } return comment; } public static String getRecentNYear(LocalDate now, int n) { LocalDate startDate = now.minusYears(n); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(startDate) + "至" + formatter.format(now); } public static String getRecentNMonth(LocalDate now, int n) { LocalDate startDate = now.minusMonths(n); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(startDate) + "至" + formatter.format(now); } public static String getRecentNWeek(LocalDate now, int n) { LocalDate startDate = now.minusWeeks(n); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(startDate) + "至" + formatter.format(now); } public static String getRecentNDay(LocalDate now, int n) { LocalDate startDate = now.minusDays(n); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(startDate) + "至" + formatter.format(now); } public static String getRecentNCompleteYear(LocalDate now, int n) { LocalDate endDate; if (now.getMonthValue() == 12 && now.getDayOfMonth() == 31) { endDate = now; } else { endDate = now.with(TemporalAdjusters.lastDayOfYear()).minusYears(1); } LocalDate startDate = endDate.minusYears(n).plusDays(1); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(startDate) + "至" + formatter.format(endDate); } public static String getRecentNCompleteMonth(LocalDate now, int n) { LocalDate endDate; if (now.equals(now.with(TemporalAdjusters.lastDayOfMonth()))) { endDate = now; } else { endDate = now.with(TemporalAdjusters.firstDayOfMonth()) .minusMonths(1) .with(TemporalAdjusters.lastDayOfMonth()); } LocalDate startDate = endDate.minusMonths(n).plusDays(1); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(startDate) + "至" + formatter.format(endDate); } public static String getRecentNCompleteQuarter(LocalDate now, int n) { LocalDate endDate; int currentMonth = now.getMonthValue(); if (currentMonth % 4 == 0 && now.getDayOfMonth() == 31) { endDate = now; } else { if (currentMonth < 4) { endDate = LocalDate.of(now.getYear() - 1, 12, 31); } else if (currentMonth < 7) { endDate = LocalDate.of(now.getYear(), 3, 31); } else if (currentMonth < 10) { endDate = LocalDate.of(now.getYear(), 6, 30); } else { endDate = LocalDate.of(now.getYear(), 9, 30); } } LocalDate startDate = endDate.minusMonths(n * 3).plusDays(1); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(startDate) + "至" + formatter.format(endDate); } public static String getRecentNCompleteWeek(LocalDate now, int n) { LocalDate endDate; if (now.getDayOfWeek().getValue() == 7) { endDate = now; } else { endDate = now.minusWeeks(1).with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); } LocalDate startDate = endDate.minusWeeks(n).plusDays(1); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(startDate) + "至" + formatter.format(endDate); } public static String getRecentNDayWithoutToday(LocalDate now, int n) { LocalDate startDate = now.minusDays(n); LocalDate endDate = now.minusDays(1); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(startDate) + "至" + formatter.format(endDate); } public static String getRecentNQuarterWithCurrent(LocalDate now, int n) { LocalDate endDate; int currentMonth = now.getMonthValue(); if (currentMonth < 4) { endDate = LocalDate.of(now.getYear(), 3, 31); } else if (currentMonth < 7) { endDate = LocalDate.of(now.getYear(), 6, 30); } else if (currentMonth < 10) { endDate = LocalDate.of(now.getYear(), 9, 30); } else { endDate = LocalDate.of(now.getYear(), 12, 31); } LocalDate startDate = endDate.minusMonths(n * 3).plusDays(1); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(startDate) + "至" + formatter.format(endDate); } public static String getMonthLastDayEx(LocalDate now, String monthEx) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); String comment = ""; if (monthEx.equals("本月")) { comment = formatter.format(YearMonth.from(now).atEndOfMonth()); } else if (monthEx.equals("上月")) { comment = formatter.format(YearMonth.from(now).minusMonths(1).atEndOfMonth()); } return comment; } public static String getGeneralYearMonthLastDayEx(LocalDate now, String yearEx, int month) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); String comment = ""; int year = 0; if (yearEx.equals("今年")) { year = now.getYear(); } else if (yearEx.equals("去年")) { year = now.getYear() - 1; } else if (yearEx.equals("前年")) { year = now.getYear() - 2; } else if (yearEx.equals("明年")) { year = now.getYear() + 1; } else if (yearEx.equals("后年")) { year = now.getYear() + 2; } comment = formatter.format(YearMonth.of(year, month).atEndOfMonth()); return comment; } public static String getSpecificYearHalfYearEx(LocalDate now, int year, String halfYearEx) { LocalDate startDate; LocalDate endDate; if (halfYearEx.equals("上")) { startDate = LocalDate.of(year, 1, 1); endDate = LocalDate.of(year, 6, 30); } else { startDate = LocalDate.of(year, 7, 1); endDate = LocalDate.of(year, 12, 31); } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); return formatter.format(startDate) + "至" + formatter.format(endDate); } public static String getGeneralYearHalfYearEx(LocalDate now, String yearEx, String halfYearEx) { int year = 0; if (yearEx.equals("今年")) { year = now.getYear(); } else if (yearEx.equals("去年")) { year = now.getYear() - 1; } else if (yearEx.equals("前年")) { year = now.getYear() - 2; } else if (yearEx.equals("明年")) { year = now.getYear() + 1; } else if (yearEx.equals("后年")) { year = now.getYear() + 2; } return getSpecificYearHalfYearEx(now, year, halfYearEx); } } 这个类在做什么
09-03
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.GregorianCalendar; class MyCalendar extends JFrame implements ActionListener { private JPanel p1, p2, p3; //三个面板 p1最上面 p2中间 p3下面 private JLabel yearStr, monthStr; //标签 private JTextField inputYear, inputMonth; private JButton confirm; //确认 private JButton lastMonth; private JButton nextMonth; private JLabel dayText;//文本框 private JLabel TimeText;//文本框 //p2面板里控件的声明 private String[] week = {"日", "一", "二", "三", "四", "五", "六"}; private JLabel[] weekLable = new JLabel[week.length];//数组的声明 //p3面板的42个按钮声明 private JButton[] dayBtn = new JButton[42]; private Calendar nowDate = new GregorianCalendar(); //Calendar是抽象类 new不出 用直接子类 private int nowYear = nowDate.get(Calendar.YEAR); private int nowMonth = nowDate.get(Calendar.MONTH); public MyCalendar() throws HeadlessException { p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); yearStr = new JLabel("Year:"); inputYear = new JTextField(4); monthStr = new JLabel(" Month:"); inputMonth = new JTextField(3); confirm = new JButton("确认"); lastMonth = new JButton("上月"); nextMonth = new JButton("下月"); dayText = new JLabel(); TimeText = new JLabel(); new Thread() { //线程内部类用来实时显示时间 public void run() { while (true) { LocalDateTime dateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); //大写的HH是24小时制的 String nowTime = dateTime.format(formatter); TimeText.setText(nowTime); try { Thread.sleep(1000); } catch (Inter
06-02
标题SpringBoot智能在线预约挂号系统研究AI更换标题第1章引言介绍智能在线预约挂号系统的研究背景、意义、国内外研究现状及论文创新点。1.1研究背景与意义阐述智能在线预约挂号系统对提升医疗服务效率的重要性。1.2国内外研究现状分析国内外智能在线预约挂号系统的研究与应用情况。1.3研究方法及创新点概述本文采用的技术路线、研究方法及主要创新点。第2章相关理论总结智能在线预约挂号系统相关理论,包括系统架构、开发技术等。2.1系统架构设计理论介绍系统架构设计的基本原则和常用方法。2.2SpringBoot开发框架理论阐述SpringBoot框架的特点、优势及其在系统开发中的应用。2.3数据库设计与管理理论介绍数据库设计原则、数据模型及数据库管理系统。2.4网络安全与数据保护理论讨论网络安全威胁、数据保护技术及其在系统中的应用。第3章SpringBoot智能在线预约挂号系统设计详细介绍系统的设计方案,包括功能模块划分、数据库设计等。3.1系统功能模块设计划分系统功能模块,如用户管理、挂号管理、医生排班等。3.2数据库设计与实现设计数据库表结构,确定字段类型、主键及外键关系。3.3用户界面设计设计用户友好的界面,提升用户体验。3.4系统安全设计阐述系统安全策略,包括用户认证、数据加密等。第4章系统实现与测试介绍系统的实现过程,包括编码、测试及优化等。4.1系统编码实现采用SpringBoot框架进行系统编码实现。4.2系统测试方法介绍系统测试的方法、步骤及测试用例设计。4.3系统性能测试与分析对系统进行性能测试,分析测试结果并提出优化建议。4.4系统优化与改进根据测试结果对系统进行优化和改进,提升系统性能。第5章研究结果呈现系统实现后的效果,包括功能实现、性能提升等。5.1系统功能实现效果展示系统各功能模块的实现效果,如挂号成功界面等。5.2系统性能提升效果对比优化前后的系统性能
在金融行业中,对信用风险的判断是核心环节之一,其结果对机构的信贷政策和风险控制策略有直接影响。本文将围绕如何借助机器学习方法,尤其是Sklearn工具包,建立用于判断信用状况的预测系统。文中将涵盖逻辑回归、支持向量机等常见方法,并通过实际操作流程进行说明。 一、机器学习基本概念 机器学习属于人工智能的子领域,其基本理念是通过数据自动学习规律,而非依赖人工设定规则。在信贷分析中,该技术可用于挖掘历史数据中的潜在规律,进而对未来的信用表现进行预测。 二、Sklearn工具包概述 Sklearn(Scikit-learn)是Python语言中广泛使用的机器学习模块,提供多种数据处理和建模功能。它简化了数据清洗、特征提取、模型构建、验证与优化等流程,是数据科学项目中的常用工具。 三、逻辑回归模型 逻辑回归是一种常用于分类任务的线性模型,特别适用于二类问题。在信用评估中,该模型可用于判断借款人是否可能违约。其通过逻辑函数将输出映射为0到1之间的概率值,从而表示违约的可能性。 四、支持向量机模型 支持向量机是一种用于监督学习的算法,适用于数据维度高、样本量小的情况。在信用分析中,该方法能够通过寻找最佳分割面,区分违约与非违约客户。通过选用不同核函数,可应对复杂的非线性关系,提升预测精度。 五、数据预处理步骤 在建模前,需对原始数据进行清理与转换,包括处理缺失值、识别异常点、标准化数值、筛选有效特征等。对于信用评分,常见的输入变量包括收入水平、负债比例、信用历史记录、职业稳定性等。预处理有助于减少噪声干扰,增强模型的适应性。 六、模型构建与验证 借助Sklearn,可以将数据集划分为训练集和测试集,并通过交叉验证调整参数以提升模型性能。常用评估指标包括准确率、召回率、F1值以及AUC-ROC曲线。在处理不平衡数据时,更应关注模型的召回率与特异性。 七、集成学习方法 为提升模型预测能力,可采用集成策略,如结合多个模型的预测结果。这有助于降低单一模型的偏差与方差,增强整体预测的稳定性与准确性。 综上,基于机器学习的信用评估系统可通过Sklearn中的多种算法,结合合理的数据处理与模型优化,实现对借款人信用状况的精准判断。在实际应用中,需持续调整模型以适应市场变化,保障预测结果的长期有效性。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值