根据给的年月,获取当月第一天和最后一天
/** * 根据给的时间获取此月的最后一天 * 传入参数为String,可传入:1、"201807";2、"2018-07-01" * @param date * 账期 * @return String * 当月的最后一天 */ public static String getLastDayByMonth(String date) { if (date.contains("-")) { date = date.replaceAll("-", ""); } else if (date.length() < 6 || date.length() > 10) { System.out.println("日期错误!"); } int year = Integer.parseInt(date.substring(0, 4)); int month = Integer.parseInt(date.substring(4, 6)); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month - 1); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); return new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime()); } /** * 根据给的时间获取此月的第一天 * 传入参数为String,可传入:1、"201807";2、"2018-07-01" * @param date * 账期 * @return String * 当月的第一天 */ public static String getFirstDayByMonth(String date) { if (date.contains("-")) { date = date.replaceAll("-", ""); } else if (date.length() < 6 || date.length() > 10) { System.out.println("日期错误!"); } int year = Integer.parseInt(date.substring(0, 4)); int month = Integer.parseInt(date.substring(4, 6)); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month - 1); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DATE)); return new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime()); }