展开全部
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Test {
/**
* 获取某月的最后一天
* @Title:getLastDayOfMonth
* @Description:
* @param:@param year
* @param:@param month
* @param:@return
* @return:String
* @throws
*/
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);
//设置日历中月份的e69da5e887aa3231313335323631343130323136353331333365643533最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
/**
* 获取这个月的最后一天
* @Title:getLastDayOfCurrentMonth
* @Description:
* @param:@param year
* @param:@param month
* @param:@return
* @return:String
* @throws
*/
public static String getLastDayOfCurrentMonth() {
Calendar cal = Calendar.getInstance();
//获取某月最大天数
int lastDay = cal.getActualMinimum(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;
}
/**
* 获取某月的第一天
* @Title:getLastDayOfMonth
* @Description:
* @param:@param year
* @param:@param month
* @param:@return
* @return:String
* @throws
*/
public static String getFirstDayOfMonth(int year,int month) {
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR,year);
//设置月份
cal.set(Calendar.MONTH, month-1);
//获取某月最大天数
int lastDay = cal.getActualMinimum(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;
}
/**
* 获取这个月的第一天
* @Title:getFirstDayOfCurrentMonth
* @Description:
* @param:@param year
* @param:@param month
* @param:@return
* @return:String
* @throws
*/
public static String getFirstDayOfCurrentMonth() {
Calendar cal = Calendar.getInstance();
//获取某月最大天数
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;
}
/**
* @Title:main
* @Description:
* @param:@param args
* @return: void
* @throws
*/
public static void main(String[] args)
{
String lastDay = getLastDayOfMonth(2014,2);
System.out.println("获取2014年2月的最后一天:" + lastDay);
String currentLDay= getLastDayOfCurrentMonth();
System.out.println("获取本月的最后一天:" + currentLDay);
String firstDay = getFirstDayOfMonth(2014,2);
System.out.println("获取2014年2月的第一天:" + firstDay);
String currentFDay= getFirstDayOfCurrentMonth();
System.out.println("获取本月的第一天:" + currentFDay);
}
}
传上来的代码真难看,不知道怎么发文件,我给你截一张图