java.util.Date date=new java.util.Date(); //获得当前时间 int month=date.getMonth(); //获得当前月份 然后就是根据常识写些判断
//get current month and current year GregroianCalendar calendar=new GregorianCalendar(); int thisMonth=calendar.get(Calendar.MONTH); int year=calendar.get(Calendar.YEAR); //set the date is last month' 28 calendar.set(year, thisMonth-1, 28); //get the last month int lastMonth=calendar.get(Calendar.MONTH); int month;//if the month of this month, add one day do { //add one day calendar.add(Calendar.DAY_OF_MONTH, 1); month=calendar.get(Calendar.MONTH);}while(month=lastMonth()); //get the day int day =calendar.get(Calendar.DAY_OF_MONTH);
Calendar cal=Calendar.getInstance();//当前日期 cal.set(Calendar.DATE,1);//设为当前月的1号 cal.add(Calendar.DATE,-1);//减一天,变为上月最后一天SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); simpleDateFormat.format(calendar.getTime());//输出2005-04-30
int month=date.getMonth(); //获得当前月份
然后就是根据常识写些判断
GregroianCalendar calendar=new GregorianCalendar();
int thisMonth=calendar.get(Calendar.MONTH);
int year=calendar.get(Calendar.YEAR);
//set the date is last month' 28
calendar.set(year, thisMonth-1, 28);
//get the last month
int lastMonth=calendar.get(Calendar.MONTH);
int month;//if the month of this month, add one day
do
{
//add one day
calendar.add(Calendar.DAY_OF_MONTH, 1);
month=calendar.get(Calendar.MONTH);}while(month=lastMonth());
//get the day
int day =calendar.get(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DATE,1);//设为当前月的1号
cal.add(Calendar.DATE,-1);//减一天,变为上月最后一天SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
simpleDateFormat.format(calendar.getTime());//输出2005-04-30
int month=date.getMonth(); //获得当前月份
然后就是根据常识写些判断
....................
要判断是否是闰年,估计代码比我写的要麻烦.
cal.set(Calendar.DATE,1);//设为当前月的1号
cal.add(Calendar.DATE,-1);//减一天,变为上月最后一天SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
simpleDateFormat.format(calendar.getTime());//输出2005-04-30可以取
Calendar cal=new GregorianCalendar(2000,2,1);
cal.add(Calendar.MONTH,-1);
System.out.println("2000年3月1号的上个月份最后一天是"+cal.getActualMaximum(Calendar.DAY_OF_MONTH));