做本地生活圈网站好吗,数据分析网站怎么做,网站开发用什么软件开发,网站建设与管理培训总结效果
最近7天#xff1a;2024年6月21日-2024年6月27日过去一周、最近一周#xff1a;2024年6月16日-2024年6月22日过去三个月#xff1a;2024年3月-2024年6月近半年、过去半年#xff1a;2023年12月-2024年6月去年#xff1a;2023年1月-2023年12月过去3年#xff1a;202…效果
最近7天2024年6月21日-2024年6月27日过去一周、最近一周2024年6月16日-2024年6月22日过去三个月2024年3月-2024年6月近半年、过去半年2023年12月-2024年6月去年2023年1月-2023年12月过去3年2021年1月-2024年12月累计2020年1月1日-2024年6月27日
代码
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.WeekFields;
import java.util.Locale;public class DataFormatUtil {public static String getDates(String timeType) {LocalDate now LocalDate.now();if (日.equals(timeType)) {// 格式化为 YYYY-MM-DDreturn now.format(DateTimeFormatter.ofPattern(yyyy-MM-dd));}if (周.equals(timeType)) {// 格式化为 YYYY-WX// 周数W是按照ISO 8601标准每周从周一开始每年的第一周至少包含4天return now.format(DateTimeFormatter.ofPattern(yyyy-Www));}if (月.equals(timeType)) {// 格式化为 YYYY-MMreturn now.format(DateTimeFormatter.ofPattern(yyyy-MM));}if (季.equals(timeType)) {// 格式化为 YYYY-QX// 季度Q是按照每个季度三个月计算1-3月为第一季度4-6月为第二季度依此类推String quarter String.valueOf((now.getMonthValue() - 1) / 3 1);return now.format(DateTimeFormatter.ofPattern(yyyy)) -Q quarter;}if (年.equals(timeType)) {// 格式化为 YYYYreturn now.format(DateTimeFormatter.ofPattern(yyyy));}return 未知时间颗粒度;}/*** 处理特殊时间** return 结果*/public static String getAllTimeRanges() {// 获取当前时间并格式化为字符串LocalDate now LocalDate.now();DateTimeFormatter formatter DateTimeFormatter.ofPattern(yyyyMMdd);String currentDateString now.format(formatter);LocalDate currentDate LocalDate.parse(currentDateString, DateTimeFormatter.BASIC_ISO_DATE);LocalDate startDate;String[] timeRanges new String[8];// 最近7天startDate currentDate.minusDays(6);timeRanges[0] String.format(最近7天%d年%d月%d日-%d年%d月%d日,startDate.getYear(), startDate.getMonthValue(), startDate.getDayOfMonth(),currentDate.getYear(), currentDate.getMonthValue(), currentDate.getDayOfMonth());// 过去一周、最近一周startDate currentDate.with(WeekFields.of(Locale.getDefault()).dayOfWeek(), 1).minusWeeks(1);LocalDate endDate startDate.plusDays(6);timeRanges[1] String.format(过去一周、最近一周%d年%d月%d日-%d年%d月%d日,startDate.getYear(), startDate.getMonthValue(), startDate.getDayOfMonth(),endDate.getYear(), endDate.getMonthValue(), endDate.getDayOfMonth());// 过去三个月startDate currentDate.minusMonths(3);timeRanges[2] String.format(过去三个月%d年%d月-%d年%d月,startDate.getYear(), startDate.getMonthValue(),currentDate.getYear(), currentDate.getMonthValue());// 近半年、过去半年startDate currentDate.minusMonths(6);timeRanges[3] String.format(近半年、过去半年%d年%d月-%d年%d月,startDate.getYear(), startDate.getMonthValue(),currentDate.getYear(), currentDate.getMonthValue());// 去年startDate currentDate.minusYears(1).withMonth(1).withDayOfMonth(1);endDate startDate.plusYears(1).minusDays(1);timeRanges[4] String.format(去年%d年1月-%d年12月,startDate.getYear(), startDate.getYear());// 过去3年startDate currentDate.minusYears(3);timeRanges[5] String.format(过去3年%d年1月-%d年12月,startDate.getYear(), currentDate.getYear());// 累计这里假设项目开始日期为2020年1月1日startDate LocalDate.of(2020, 1, 1);timeRanges[6] String.format(累计%d年1月1日-%d年%d月%d日,startDate.getYear(), currentDate.getYear(), currentDate.getMonthValue(), currentDate.getDayOfMonth());// 为了简化这里我们忽略近XX月、近XX年的情况因为它们与过去XX月、过去XX年的处理相同StringBuilder result new StringBuilder();for (int i 0; i timeRanges.length - 1; i) {result.append(i 1).append(. ).append(timeRanges[i]).append(\n);}return result.toString();}
}