//本月
@Test
public void test01(){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH,1);//1:本月第一天
String day1= format.format(c.getTime());
System.out.println("本月第一天:"+day1);
//获取当前月最后一天
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String day2= format.format(ca.getTime());
System.out.println("本月最后一天:"+day2);
}
//获取上个月的第一天
@Test
public void test02(){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
System.out.println("上个月第一天:"+format.format(calendar.getTime()));
}
//获取上个月的最后一天
@Test
public void test03(){
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar=Calendar.getInstance();
int month=calendar.get(Calendar.MONTH);
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
System.out.println("上个月最后一天:"+sf.format(calendar.getTime()));
}