import java.util.Calendar;
import java.util.Date;
public class CalendarUtils {
/**
* 得到本月的第一天
*
* @return
*/
public static Date getMonthFirstDay() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, calendar
.getActualMinimum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
public static Date getMonthFirstDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar
.getActualMinimum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
/**
* 时间加减
*
* @param currentDate
* @param delta
* @return
*/
public static Date addTime(Date currentDate, int delta) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
calendar.add(Calendar.MINUTE, delta);
return calendar.getTime();
}
/**
* 月份加减
*
* @param currentDate
* @param delta
* @return
*/
public static Date addMonth(Date currentDate, int delta) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
calendar.add(Calendar.MONTH, delta);
return calendar.getTime();
}
/**
* 月份加减
*
* @param currentDate
* @param delta
* @return
*/
public static Date addDay(Date currentDate, int delta) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
calendar.add(Calendar.DATE, delta);
return calendar.getTime();
}
/**
* 年份加减
*
* @param currentDate
* @param delta
* @return
*/
public static Date addYear(Date currentDate, int delta) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
calendar.add(Calendar.YEAR, delta);
return calendar.getTime();
}
/**
* 得到本月的最后一天
*
* @return
*/
public static Date getMonthLastDay() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, calendar
.getActualMaximum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
public static Date getMonthLastDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar
.getActualMaximum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
/**
*
* @param year
* int 年份
* @param month
* int 月份
*
* @return int 某年某月的最后一天
*/
public static int getLastDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
// 某年某月的最后一天
return cal.getActualMaximum(Calendar.DATE);
}
/**
* 根据年份与月份得到Date
*
* @param year
* @param month
* @return
*/
public static Date getDate(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, 1);
return cal.getTime();
}
/**
* 得到当年的第一天
*
* @param year
* @return
*/
public static Date getYearFirstDay(int year) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DATE, 1);
return cal.getTime();
}
/**
* 得到当年的最后一天
*
* @param year
* @return
*/
public static Date getYearLastDay(int year) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, 11);
cal.set(Calendar.DATE, 1);
return cal.getTime();
}
}