使用java的Calendar对象获得当前日期
思路:
先获得当前季度的开始和结束日期,在当前日期的基础上往前推3个月即上个季度的开始和结束日期
/** *@paramflagtrue:开始日期;false:结束日期 *@return */ publicstaticStringgetLastQuarterTime(booleanflag){ SimpleDateFormatshortSdf=newSimpleDateFormat("yyyy-MM-dd"); SimpleDateFormatlongSdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); StringresultDate=""; Datenow=null; try{ Calendarcalendar=Calendar.getInstance(); intcurrentMonth=calendar.get(Calendar.MONTH)+1; //true:开始日期;false:结束日期 if(flag){ if(currentMonth>=1&¤tMonth<=3) calendar.set(Calendar.MONTH,0); elseif(currentMonth>=4&¤tMonth<=6) calendar.set(Calendar.MONTH,3); elseif(currentMonth>=7&¤tMonth<=9) calendar.set(Calendar.MONTH,6); elseif(currentMonth>=10&¤tMonth<=12) calendar.set(Calendar.MONTH,9); calendar.set(Calendar.DATE,1); now=longSdf.parse(shortSdf.format(calendar.getTime())+"00:00:00"); }else{ if(currentMonth>=1&¤tMonth<=3){ calendar.set(Calendar.MONTH,2); calendar.set(Calendar.DATE,31); }elseif(currentMonth>=4&¤tMonth<=6){ calendar.set(Calendar.MONTH,5); calendar.set(Calendar.DATE,30); }elseif(currentMonth>=7&¤tMonth<=9){ calendar.set(Calendar.MONTH,8); calendar.set(Calendar.DATE,30); }elseif(currentMonth>=10&¤tMonth<=12){ calendar.set(Calendar.MONTH,11); calendar.set(Calendar.DATE,31); } now=longSdf.parse(shortSdf.format(calendar.getTime())+"23:59:59"); } calendar.setTime(now);//设置日期 calendar.add(Calendar.MONTH,-3); resultDate=longSdf.format(calendar.getTime()); }catch(Exceptione){ ; } returnresultDate; }