Calendar的使用以及活动每月星期的信息
import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; /** * @author ssqswyf * @date 2021/9/17 */ public class TimeUtil { private static int leapYear[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //闰年每月天数 private static int commonYear[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //平年每月天数 //判断闰年 public static boolean isLeapYear(int year){ return (year%4 == 0 && year%100 != 0) || (year%400 == 0); } public static void main(String[] args) { for (int[] ints : TimeUtil.getWeekInfoInt(2021, 1)) { System.out.println("" + ints[0] + " " + ints[1]); } System.out.println("-------------------------"); for (String[] strings : TimeUtil.getWeekInfoString(2013, 8)) { System.out.println(strings[0] + " " + strings[1]); } } public static List<String[]> getWeekInfoString(int year,int month) { List<String[]> weekInfoString = new ArrayList<>(); List<int[]> weekInfoInt = getWeekInfoInt(year, month); for (int[] ints : weekInfoInt) { weekInfoString.add(new String[]{thisDayMidnight(year, month, ints[0]),secondDayMidnight(year, month, ints[1])}); } return weekInfoString; } public static List<int[]> getWeekInfoInt(int year,int month) { // 当月天数 int day = 0; // 当月一号是周几 int weekday =0; // 1900.1.1到当前输入日期之间的天数 int allDays = 0; // 当月周数 int weeks = 0; //计算当月天数 day = isLeapYear(year)?leapYear[month]:commonYear[month]; //计算距离1900.1.1天数 for(int i=1900; i<year; i++){ allDays += isLeapYear(i)?366:365; } for(int i=1; i<month; i++){ allDays += isLeapYear(year)?leapYear[i]:commonYear[i]; } //计算当月一号是星期几 weekday = 1+allDays%7; // 计算当月周数 int lastWeek = (day - (8 - weekday))%7 != 0?1:0; weeks = (day - (8 - weekday))/7+2+lastWeek; List<int[]> dayList = new ArrayList<>(); dayList.add(new int[]{1,8-weekday}); int start = 0; int end = 0; for (int i = 0;i<weeks-2;i++){ start = i*7+9-weekday; end = start + 6; if (end > day){ end = day; } dayList.add(new int[]{start,end}); } return dayList; } /** * @return 指定日期凌晨字符串 */ public static String thisDayMidnight(int year,int month,int day){ Calendar calendar = thisDayCalendar(year, month, day); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = sdf.format(calendar.getTime()); return time; } /** * @return 指定日期第二天凌晨字符串 */ public static String secondDayMidnight(int year,int month,int day){ Calendar calendar = thisDayCalendar(year, month, day); calendar.add(Calendar.HOUR,24); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = sdf.format(calendar.getTime()); return time; } public static Calendar thisDayCalendar(int year,int month,int day){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month-1); calendar.set(Calendar.DAY_OF_MONTH, day); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar; } /** * @return 当日凌晨字符串 */ public static String todayMidnight(){ Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = sdf.format(calendar.getTime()); return time; } /** * @param year * @param month * @return 月初时间字符串 */ public static String monthBegin(Integer year,Integer month){ Calendar calendar = getMonthBeginCalendar(year, month); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = sdf.format(calendar.getTime()); return time; } /** * @param year * @param month * @return 月末时间字符串 */ public static String monthEnd(Integer year,Integer month){ Calendar calendar = getMonthBeginCalendar(year, month); calendar.add(Calendar.MONTH, 1); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = sdf.format(calendar.getTime()); return time; } /** * @param year * @param month * @return 月初时间日历格式 */ public static Calendar getMonthBeginCalendar(Integer year, Integer month) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month-1); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar; } }