// 获取当前月的第一天 public static String getFirstDay(String format) { Calendar calendar = Calendar.getInstance(); int month = calendar.get(Calendar.MONTH); int year = calendar.get(Calendar.YEAR); calendar.set(year, month, 1); return DateUtil.getDateText(calendar.getTime(), format); }
// 获取日期的前n个月的月份(闭区间) public static List<String> getPreNMonths(String curMonth, Integer n) { String[] curArr = curMonth.split("-"); Integer curY = Integer.parseInt(curArr[0]); Integer curM = Integer.parseInt(curArr[1]); Calendar curCalendar = Calendar.getInstance(); curCalendar.set(curY, curM - 1, 1, 0, 0); Calendar tmpCalendar = Calendar.getInstance(); tmpCalendar.set(curY, curM - 1 - n, 1, 0, 0); List<String> months = new ArrayList<String>(); while (tmpCalendar.getTimeInMillis() <= curCalendar.getTimeInMillis()) { months.add(DateUtil.getDateText(tmpCalendar.getTime(), "yyyy-MM")); tmpCalendar.add(Calendar.MONTH, 1); } return months; }
public static List<String> getBetweenDateStr(String startTime, String endTime, String format) { List<String> dateList = new ArrayList<String>(); try { Long startM = DateUtil.COMMON.getTextDate(startTime).getTime(); Long endM = DateUtil.COMMON.getTextDate(endTime).getTime(); long result = (endM - startM) / (24 * 60 * 60 * 1000); String[] startTimeStr = startTime.split("-"); Calendar startCalendar = Calendar.getInstance(); startCalendar.set( Integer.parseInt(startTimeStr[0]), Integer.parseInt(startTimeStr[1]) - 1, Integer.parseInt(startTimeStr[2])); startCalendar.add(Calendar.DATE, -1); for (int i = 0; i <= result; i++) { startCalendar.add(Calendar.DATE, 1); dateList.add(DateUtil.getDateText(startCalendar.getTime(), format)); } } catch (ParseException e) { e.printStackTrace(); } return dateList; }
// 获取前x天的数据, x < 10; public static String getPreXDay(int x, String format) { Calendar calendar = Calendar.getInstance(); int month = calendar.get(Calendar.MONTH); int year = calendar.get(Calendar.YEAR); int date = calendar.get(Calendar.DATE); if (date - x < 0) { if (month - 1 < 0) { calendar.add(Calendar.YEAR, -1); calendar.set(Calendar.MONTH, 11); calendar.set(Calendar.DATE, 31 - (x - date)); } else { calendar.add(Calendar.MONTH, -1); calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE)); // 设置本月最大日期 int maxDate = calendar.get(Calendar.DATE); calendar.set(year, month - 1, maxDate - (x - date)); } } else { calendar.set(year, month, date - x); } return DateUtil.getDateText(calendar.getTime(), format); }
// 获取当前明天的数据 public static String getNextDay(String format) { Calendar calendar = Calendar.getInstance(); Calendar tmpCalendar = Calendar.getInstance(); tmpCalendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE)); // 设置本月最大日期 int maxDate = tmpCalendar.get(Calendar.DATE); int month = calendar.get(Calendar.MONTH); int year = calendar.get(Calendar.YEAR); int date = calendar.get(Calendar.DATE); if (date + 1 > maxDate) { if (month + 1 > 11) { calendar.add(Calendar.YEAR, 1); calendar.set(Calendar.MONTH, 0); calendar.set(Calendar.DATE, 1); } else { calendar.set(year, month, date + 1); } } else { calendar.set(year, month, date + 1); } return DateUtil.getDateText(calendar.getTime(), format); }
// 获取开始月份和结束月份中间的所有月份,包括开始月份和结束月份 public static List<String> getBetweenMonths(String startMonth, String endMonth) { String[] startArr = startMonth.split("-"); String[] endArr = endMonth.split("-"); Integer startY = Integer.parseInt(startArr[0]); Integer startM = Integer.parseInt(startArr[1]); Integer endY = Integer.parseInt(endArr[0]); Integer endM = Integer.parseInt(endArr[1]); Calendar startCalendar = Calendar.getInstance(); startCalendar.set(startY, startM - 1, 1, 0, 0); Calendar endCalendar = Calendar.getInstance(); endCalendar.set(endY, endM - 1, 1, 0, 0); List<String> months = new ArrayList<String>(); while (startCalendar.getTimeInMillis() <= endCalendar.getTimeInMillis()) { months.add(DateUtil.getDateText(startCalendar.getTime(), "yyyy-MM")); startCalendar.add(Calendar.MONTH, 1); } return months; }
// 获取当前月的最后一天 public static String getLastDay(String format) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE)); // 设置本月最大日期 return DateUtil.getDateText(calendar.getTime(), format); }
// 获取当前天 public static String getToday(String format) { Calendar calendar = Calendar.getInstance(); return DateUtil.getDateText(calendar.getTime(), format); }