时间初始化工具类

场景:在统计数据时,有可能需要初始化一段时间,下面工具类主要用于初始化时间

依赖

        <dependency>
			<groupId>org.projectlombokgroupId>
			<artifactId>lombokartifactId>
			<scope>providedscope>
		dependency>

时间枚举

/**
 * 时间枚举
 * 

* 创建时间: 2022-08-04 09:35 * * @author fuxshen * @version v1.0.0 * @since v1.0.0 */ @Getter @AllArgsConstructor public enum TimeTypeEnum { YEAR("year","年"), MONTH("month","月"), DAY("day","日"); private final String code; private final String name; }

具体方法

/**
 * 统计时间工具类
 * 

* 创建时间: 2022-08-01 17:43 * * @author fuxshen * @version v1.0.0 * @since v1.0.0 */ public class StatisticalTimeUtils { public static void main(String[] args) { List<String> weeks = getWeeks("2022-07-31", "2022-08-09"); System.out.println(weeks); } /** * 初始化时间周,格式2022年 第1周 * * @return java.util.List * @Description * @author fuxshen * @date 2022-08-02 10:24:38 **/ @SneakyThrows public static List getWeeks(String startTime, String endTime) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date d1 = df.parse(startTime); Date d2 = df.parse(endTime); //Date date = DateUtil.parse(startTime); Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(Calendar.MONDAY); //c.setMinimalDaysInFirstWeek(7); c.setTime(d1); //获取开始时间是当年的第几周 int startWeek = c.get(Calendar.WEEK_OF_YEAR); //获取月份,如果月份是12,并且周数是1,重新赋值53周 int startMonth = c.get(Calendar.MONTH) + 1; if (startMonth == 12 && startWeek == 1) { startWeek = 53; } //获取开始的年份 int startYear = c.get(Calendar.YEAR); //Date dateEnd = DateUtil.parse(endTime); Calendar cEnd = Calendar.getInstance(); cEnd.setFirstDayOfWeek(Calendar.MONDAY); //cEnd.setMinimalDaysInFirstWeek(7); cEnd.setTime(d2); //获取结束时间是第几周 int endWeek = cEnd.get(Calendar.WEEK_OF_YEAR); //获取月份,如果月份是12,并且周数是1,重新赋值53周 int endMonth = cEnd.get(Calendar.MONTH) + 1; if (endMonth == 12 && endWeek == 1) { endWeek = 53; } //获取结束时间的年份 int endYear = cEnd.get(Calendar.YEAR); List list = new ArrayList<>(); //默认每年53周 if (endYear == startYear) { for (int week = startWeek; week <= endWeek; week++) { String yearAndWeek = startYear +"年"+"第"+ week+"周"; list.add(yearAndWeek); } } if (endYear > startYear) { //计算相差年份 int year = endYear - startYear; //循环次数 int count; if (year == 1) { //循环次数 count = 53 - startWeek + endWeek - 1; } else { int i = endYear - startYear;//循环次数 count = 53 - startWeek + (i - 1) * 53 + endWeek; } for (int i = 0; i <= count; i++) { if (startWeek >= 53 && endWeek >= 1){ startYear = startYear + 1; startWeek = 1; } String yearAndWeek = startYear+"年"+"第"+startWeek+"周"; startWeek++; list.add(yearAndWeek); } } return list; } /** * 获取前12周分别有哪些周 * * @return java.util.List * @Description * @author fuxshen * @date 2022-08-02 10:24:38 **/ public static List<String> getYearWeeks() { Calendar c = Calendar.getInstance(); c.setTime(new Date()); c.add(Calendar.WEEK_OF_MONTH, -52);//前52周; Date eightWeeks = c.getTime(); /**获取往前推第52周的时间年和时间周*/ Calendar calendars = Calendar.getInstance(); calendars.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天 calendars.setMinimalDaysInFirstWeek(4);//可以不用设置 calendars.setTimeInMillis(eightWeeks.getTime());//获得往前推52周的时间戳 int eightWeekYear = calendars.get(Calendar.YEAR);//获得当前的年(往前推52周的时间年) int eightWeekOfYear = calendars.get(Calendar.WEEK_OF_YEAR);//获得当前日期属于今年的第几周(往前推52周的时间年) /**获取当前时间年和时间周*/ Calendar calendar = Calendar.getInstance(); calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天 calendar.setMinimalDaysInFirstWeek(4);//可以不用设置 calendar.setTimeInMillis(System.currentTimeMillis());//获得当前的时间戳 int weekYear = calendar.get(Calendar.YEAR);//获得当前的年 int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);//获得当前日期属于今年的第几周 List<String> synchronizedList = Collections.synchronizedList(new ArrayList<String>()); //初始化非当前年的周区间 for (int i = eightWeekOfYear; i <= 52; i++) { StringBuffer weekBuffer = new StringBuffer(); weekBuffer.append(eightWeekYear) .append("年") .append("第") .append(i) .append("周"); synchronizedList.add(weekBuffer.toString()); } //初始化,当前年的周区间 for (int i = 1; i <= weekOfYear; i++) { StringBuffer weekBuffer = new StringBuffer(); weekBuffer.append(weekYear) .append("年") .append("第") .append(i) .append("周"); synchronizedList.add(weekBuffer.toString()); } return synchronizedList; } /** * 获取两个时间段的时间段值 * * @param startTime 开始时间 * @param endTime 结束时间 * @param typeEnum 时间类型枚举,决定返回的是之间的年份还是月份 * @return */ public static List<String> getTimePeriodFromTwoTime(Long startTime, Long endTime, TimeTypeEnum typeEnum) { LocalDate start = Instant.ofEpochMilli(startTime).atZone(ZoneOffset.ofHours(8)).toLocalDate(); LocalDate end = Instant.ofEpochMilli(endTime).atZone(ZoneOffset.ofHours(8)).toLocalDate(); List<String> result = new ArrayList<>(); // 年 if (typeEnum.getCode().equals(TimeTypeEnum.YEAR.getCode())) { Year startyear = Year.from(start); Year endYear = Year.from(end); // 包含最后一个时间 for (long i = 0; i <= ChronoUnit.YEARS.between(startyear, endYear); i++) { result.add(startyear.plusYears(i).toString()); } } // 月 else if (TimeTypeEnum.MONTH.getCode().equals(typeEnum.getCode())) { YearMonth from = YearMonth.from(start); YearMonth to = YearMonth.from(end); for (long i = 0; i <= ChronoUnit.MONTHS.between(from, to); i++) { result.add(from.plusMonths(i).toString()); } } // 日 else { for (long i = 0; i <= ChronoUnit.DAYS.between(start, end); i++) { result.add(start.plus(i, ChronoUnit.DAYS).toString()); } } return result; } /** * 根据年份和周数,获取前后时间 * * @param year * @param week * @return java.lang.String * @Description * @author fuxshen * @date 2022-09-05 17:01:44 **/ public static String getTimeInterval(Integer year, Integer week) { /**此处为一周7天*/ SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd"); Calendar cal = Calendar.getInstance(); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.set(Calendar.YEAR, year); cal.set(Calendar.WEEK_OF_YEAR, week); cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()); String formatDate1 = sdf.format(cal.getTime()); cal.add(Calendar.DAY_OF_WEEK, 6); String formatDate7 = sdf.format(cal.getTime()); StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("(") .append(formatDate1) .append("至") .append(formatDate7) .append(")"); return stringBuffer.toString(); } }

你可能感兴趣的:(工具类,java)