public static String getLastDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
// 设置年份
cal.set(Calendar.YEAR, year);
// 设置月份
cal.set(Calendar.MONTH, month - 1);
// 获取某月最大天数
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
// 设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
// 获取今天年月日
public static String getDateToday() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());
}
// 获取昨天年月日
public static String getYesterdayDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, -1);
Date date = c.getTime();
return format.format(date);
}
// 获取明天年月日
public static String getTomorrowDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, 1);
Date date = c.getTime();
return format.format(date);
}
// 获取当前年月
public static String getDateYM() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
return sdf.format(new Date());
}
// 获取上月年月
public static String getProMonthYM() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.MONTH, -1);
Date date = c.getTime();
return format.format(date);
}
// 通过年月获取当前月的最开始时间
public static String getDateMin(String str) {
return str.trim() + "-01 00:00:00";
}
// 通过年月获取当下一月的最开始时间
public static String getDateMax(String str) {
String s[] = str.split("-");
int y = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
if (m < 12) {
m = m + 1;
} else {
y = y + 1;
m = 1;
}
return y + "-" + getCoverageNum(m + "") + "-01 00:00:00";
}
public static String getCoverageNum(String alarmType) {
if (Integer.parseInt(alarmType) < 10) {
return "0" + alarmType;
}
return alarmType;
}
// 获取当前日期前一个星期的日期
public static String getProWeekDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, -7);
Date date = c.getTime();
return format.format(date);
}
// 获取当前包括当前日期前6天的日期
public static String getProWeekDateNew() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, -6);
Date date = c.getTime();
return format.format(date);
}
// 获取当前日期前一个月的日期
public static String getProMonthDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.MONTH, -1);
Date date = c.getTime();
return format.format(date);
}