| | |
| | | } |
| | | |
| | | /** |
| | | * @Description: 获取两个日期相差几个月 |
| | | * @Param: [start, end] |
| | | * @return: int |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/9/23 |
| | | */ |
| | | public static int getMonth(Date start, Date end) { |
| | | if (start.after(end)) { |
| | | Date t = start; |
| | | start = end; |
| | | end = t; |
| | | } |
| | | Calendar startCalendar = Calendar.getInstance(); |
| | | startCalendar.setTime(start); |
| | | Calendar endCalendar = Calendar.getInstance(); |
| | | endCalendar.setTime(end); |
| | | Calendar temp = Calendar.getInstance(); |
| | | temp.setTime(end); |
| | | temp.add(Calendar.DATE, 1); |
| | | |
| | | int year = endCalendar.get(Calendar.YEAR) |
| | | - startCalendar.get(Calendar.YEAR); |
| | | int month = endCalendar.get(Calendar.MONTH) |
| | | - startCalendar.get(Calendar.MONTH); |
| | | |
| | | if ((startCalendar.get(Calendar.DATE) == 1) |
| | | && (temp.get(Calendar.DATE) == 1)) { |
| | | return year * 12 + month + 1; |
| | | } else if ((startCalendar.get(Calendar.DATE) != 1) |
| | | && (temp.get(Calendar.DATE) == 1)) { |
| | | return year * 12 + month; |
| | | } else if ((startCalendar.get(Calendar.DATE) == 1) |
| | | && (temp.get(Calendar.DATE) != 1)) { |
| | | return year * 12 + month; |
| | | } else { |
| | | return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @Description: 获取两个日期之间所有的月份 |
| | | * @Param: [start, end] |
| | | * @return: java.util.List<java.lang.String> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/9/23 |
| | | */ |
| | | public static List<String> getAllMonth(Date start, Date end) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(start); |
| | | // 获取开始年份和开始月份 |
| | | int startYear = calendar.get(Calendar.YEAR); |
| | | int startMonth = calendar.get(Calendar.MONTH); |
| | | // 获取结束年份和结束月份 |
| | | calendar.setTime(end); |
| | | int endYear = calendar.get(Calendar.YEAR); |
| | | int endMonth = calendar.get(Calendar.MONTH); |
| | | // |
| | | List<String> list = new ArrayList<String>(); |
| | | |
| | | for (int i = startYear; i <= endYear; i++) { |
| | | String date = ""; |
| | | if (startYear == endYear) { |
| | | for (int j = startMonth; j <= endMonth; j++) { |
| | | if (j < 9) { |
| | | date = i + "-0" + (j + 1); |
| | | } else { |
| | | date = i + "-" + (j + 1); |
| | | } |
| | | list.add(date); |
| | | } |
| | | |
| | | } else { |
| | | if (i == startYear) { |
| | | for (int j = startMonth; j < 12; j++) { |
| | | if (j < 9) { |
| | | date = i + "-0" + (j + 1); |
| | | } else { |
| | | date = i + "-" + (j + 1); |
| | | } |
| | | list.add(date); |
| | | } |
| | | } else if (i == endYear) { |
| | | for (int j = 0; j <= endMonth; j++) { |
| | | if (j < 9) { |
| | | date = i + "-0" + (j + 1); |
| | | } else { |
| | | date = i + "-" + (j + 1); |
| | | } |
| | | list.add(date); |
| | | } |
| | | } else { |
| | | for (int j = 0; j < 12; j++) { |
| | | if (j < 9) { |
| | | date = i + "-0" + (j + 1); |
| | | } else { |
| | | date = i + "-" + (j + 1); |
| | | } |
| | | list.add(date); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 获取两个日期之间所有的年份 |
| | | * @Param: [start, end] |
| | | * @return: java.util.List<java.lang.String> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/9/23 |
| | | */ |
| | | public static List<String> getAllYear(Date start, Date end) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(start); |
| | | int startYear = calendar.get(Calendar.YEAR); |
| | | calendar.setTime(end); |
| | | int endYear = calendar.get(Calendar.YEAR); |
| | | |
| | | List<String> list = new ArrayList<>(); |
| | | list.add(String.valueOf(startYear)); |
| | | |
| | | int i = endYear - startYear; |
| | | |
| | | for (int j = 1; j < i + 1; j++) { |
| | | list.add(String.valueOf(startYear + j)); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | public static List<String> getAllDays(Date start, Date end) { |
| | | List<String> list = new ArrayList<String>(); |
| | | SimpleDateFormat outformat = new SimpleDateFormat("yyyy-MM-dd"); |
| | | |
| | | Calendar sCalendar = Calendar.getInstance(); |
| | | sCalendar.setTime(start); |
| | | int year = sCalendar.get(Calendar.YEAR); |
| | | int month = sCalendar.get(Calendar.MONTH); |
| | | int day = sCalendar.get(Calendar.DATE); |
| | | sCalendar.set(year, month, day, 0, 0, 0); |
| | | |
| | | Calendar eCalendar = Calendar.getInstance(); |
| | | eCalendar.setTime(end); |
| | | year = eCalendar.get(Calendar.YEAR); |
| | | month = eCalendar.get(Calendar.MONTH); |
| | | day = eCalendar.get(Calendar.DATE); |
| | | eCalendar.set(year, month, day, 0, 0, 0); |
| | | |
| | | while (sCalendar.before(eCalendar)) { |
| | | list.add(outformat.format(sCalendar.getTime())); |
| | | sCalendar.add(Calendar.DAY_OF_YEAR, 1); |
| | | } |
| | | list.add(outformat.format(eCalendar.getTime())); |
| | | |
| | | return list; |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 时间戳转 date |
| | | * |
| | | * @param longDate 时间戳 |