| | |
| | | return cal.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年月的第一天时间 |
| | | * @param year 年份 |
| | | * @param month 月份 |
| | | * @return 时间 |
| | | */ |
| | | public static Date getYearMonthStart(Integer year,Integer month) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | // 设置年份 |
| | | cal.set(Calendar.YEAR, year); |
| | | // 设置月份 |
| | | cal.set(Calendar.MONTH, month - 1); |
| | | // 获取某月最小天数 |
| | | int firstDay = cal.getMinimum(Calendar.DATE); |
| | | // 设置日历中月份的最小天数 |
| | | cal.set(Calendar.DAY_OF_MONTH, firstDay); |
| | | int actualMinimum = cal.getActualMinimum(Calendar.DAY_OF_MONTH); |
| | | cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),actualMinimum,00,00,00); |
| | | return cal.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年月的最后一天时间 |
| | | * @param year 年份 |
| | | * @param month 月份 |
| | | * @return 时间 |
| | | */ |
| | | public static Date getYearMonthEnd(Integer year,Integer month) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | // 设置年份 |
| | | cal.set(Calendar.YEAR, year); |
| | | // 设置月份 |
| | | cal.set(Calendar.MONTH, month - 1); |
| | | // 获取某月最大天数 |
| | | int lastDay = cal.getMaximum(Calendar.DATE); |
| | | // 设置日历中月份的最大天数 |
| | | cal.set(Calendar.DAY_OF_MONTH, lastDay); |
| | | int actualMinimum = cal.getActualMinimum(Calendar.DAY_OF_MONTH); |
| | | cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),actualMinimum,00,00,00); |
| | | return cal.getTime(); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | |
| | | // Date date= new Date(); |
| | | // Date after = new Date(); |
| | | // System.out.println(calTimeDifference(date,after)); |
| | | System.out.println(getFirstDayOfMonth()); |
| | | System.out.println(getYearMonthStart(2021,2)); |
| | | |
| | | } |
| | | } |