//获取当月和上一月的月初月末publicstaticvoidmain(String[] args){SimpleDateFormat format =newSimpleDateFormat("yyyy-MM-dd");//获取前月的第一天Calendar cal_1=Calendar.getInstance();//获取当前日期
cal_1.add(Calendar.MONTH,-1);
cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天String format1 = format.format(cal_1.getTime());System.out.println("-----1------firstDay:"+format1);//获取前月的最后一天Calendar cale =Calendar.getInstance();
cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天String format2 = format.format(cale.getTime());System.out.println("-----2------lastDay:"+format2);//获取当前月第一天:Calendar c =Calendar.getInstance();
c.add(Calendar.MONTH,0);
c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天String first = format.format(c.getTime());System.out.println("===============first:"+first);//获取当前月最后一天Calendar ca =Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));String last = format.format(ca.getTime());System.out.println("===============last:"+last);}
//封装后月初月末publicstaticStringfirstDay(){SimpleDateFormat format =newSimpleDateFormat("yyyy-MM-dd");Calendar c =Calendar.getInstance();
c.add(Calendar.MONTH,0);
c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天return format.format(c.getTime());}publicstaticStringlastDay(){SimpleDateFormat format =newSimpleDateFormat("yyyy-MM-dd");Calendar ca =Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));return format.format(ca.getTime());}