最近在项目中进行考勤打卡的模块的开发,中间的业务涉及到对某年某月的数据统计,特在此做出一个小结,高手可以忽略,不喜勿喷。
根据2020-11-23这种日期格式获取到这个月的日历,如果是过去的月份则返回整个月份的日历,如果是正在进行的月份,则返回日期截止到这个月最新的日期。
public class DateCountUtils {
public static void main(String[] args) {
// 2、打印出当前月份的工作日日期(条件来源:代码)
List<String> monthDay = getMonthFullDayWorkingDay("2020-11-05");
List<String> monthDay2 = getMonthFullDayWorkingDay("2020-12-02");
System.out.println(monthDay);
System.out.println(monthDay2);
}
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
/**
* 条件来源程序判断:
* 获取当前月份的所有日期
* @param date "2020-11-20"
* @return 如果是当前月,返回到当月截止今天的日期集合 如果是之前月返回之前月的日期集合
*
* 来源:https://www.cnblogs.com/jinzhiming/p/6224860.html
*/
public static List<String> getMonthFullDayWorkingDay(String date) {
List<String> fullDayList = new ArrayList<String>();
int year = Integer.parseInt(date.substring(0, 4));
int month = Integer.parseInt(date.substring(5, 7));
int day = 1;// 所有月份从1号开始
Calendar cal = Calendar.getInstance();// 获得当前日期对象
// 获取当前月的月份,和日期
int current_month = cal.get(Calendar.MONTH) + 1; // 当前月
int current_date = cal.get(Calendar.DATE); // 当前天
cal.clear();// 清除信息
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);// 1月从0开始
cal.set(Calendar.DAY_OF_MONTH, day);
// 判断出入的时间中的月份是否在当前时间
// 如果是过去的月份,则,获取传入的月份 整个月的工作日
if(month < current_month){
int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 获取所在月份的最大天数
for (int j = 0; j <= (count - 1); ) {
// 判断,如果是最后一个月份,跳出循环
if (sdf.format(cal.getTime()).equals(getLastDay(year, month)))
break;
// 否则,天数++
cal.add(Calendar.DAY_OF_MONTH, j == 0 ? +0 : +1);
j++;
//=========================== 追加条件 Start ============================
// 追加日期
fullDayList.add(sdf.format(cal.getTime()));
//=========================== 追加条件 End ============================
}
}
// 如果相等,获取 1号 到 今天的一个时间
else if(month == current_month){
int count = current_date; // 设置当前月份的当前天数
for (int j = 0; j <= (count - 1); ) {
// 判断,如果是最后一个月份,跳出循环
if (sdf.format(cal.getTime()).equals(getCurrentDay(year, month,current_date)))
break;
// 否则,天数++
cal.add(Calendar.DAY_OF_MONTH, j == 0 ? +0 : +1);
j++;
//=========================== 追加条件 Start ============================
// 追加日期
fullDayList.add(sdf.format(cal.getTime()));
//=========================== 追加条件 End ============================
}
}
return fullDayList;
}
/**
* 获取所在月的最后一天
* @param year 2020
* @param month 10
* @return "2020-10-31 08:49:29"
*/
public static String getLastDay(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, 0);
return sdf.format(cal.getTime());
}
/**
* 获取本月 当前的日期
* @param year 2020
* @param month 10
* @return "2020-10-31 08:49:29"
*/
public static String getCurrentDay(int year, int month,int data) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, data);
return sdf.format(cal.getTime());
}
//查询是周几 1~7
public static int dayForWeek(String pTime) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
int dayForWeek = 0;
try {
c.setTime(format.parse(pTime));
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
dayForWeek = 7;
} else {
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
System.out.println(dayForWeek);
} catch (Exception e) {
e.printStackTrace();
}
return dayForWeek;
}
}
运行结果如下:
若要判断日期是否为周六日,可以参考之前的博客:https://blog.csdn.net/duan196_118/article/details/109847388。
在具体的业务场景中,或许我们需要的日期格式不一样,可以根据上面的方法进行改进。若要加上节假日的判断,我在展示代码中贴出了网址,需要的朋友可以做个参考,根据Excel的读取去获取节假日。另外还可以去调用免费节假日的API。但是这种情况存在一定的问题,比如当前没网,那你的程序是不是就受到限制了。有兴趣的朋友可以去百度一波。网上关于这个的博客还是比较多的。比如:https://www.kancloud.cn/xiaoggvip/holiday_free/1606802。
其实在成长的路上我们会发现,难倒程序员的不是功能的实现,而是完善的业务梳理。由于甲方的需求存在着变动,所以能设计出灵活完善的流程显得尤为重要,当然这也来自我们日积月累的成长,慢慢的自己就会注意在程序设计中避免硬编码,尽量让程序可扩展更加灵活。减少因需求变动导致的代码改动。
尊重原创,希望可以和广大朋友更好的成长,一起加油!!!