思路:
先获得当前季度的开始和结束日期,在当前日期的基础上往前推3个月即上个季度的开始和结束日期
/** * @param flag true:开始日期;false:结束日期 * @return */ public static String getLastQuarterTime(boolean flag){ SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String resultDate=""; Date Now = null; try { Calendar calendar = Calendar.getInstance(); int currentMonth = calendar.get(Calendar.MONTH) + 1; //true:开始日期;false:结束日期 if(flag){ if (currentMonth >= 1 && currentMonth <= 3) calendar.set(Calendar.MONTH,0); else if (currentMonth >= 4 && currentMonth <= 6) calendar.set(Calendar.MONTH,3); else if (currentMonth >= 7 && currentMonth <= 9) calendar.set(Calendar.MONTH,6); else if (currentMonth >= 10 && currentMonth <= 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 && currentMonth <= 3) { calendar.set(Calendar.MONTH,2); calendar.set(Calendar.DATE,31); } else if (currentMonth >= 4 && currentMonth <= 6) { calendar.set(Calendar.MONTH,5); calendar.set(Calendar.DATE,30); } else if (currentMonth >= 7 && currentMonth <= 9) { calendar.set(Calendar.MONTH,8); calendar.set(Calendar.DATE,30); } else if (currentMonth >= 10 && currentMonth <= 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 (Exception e) { ; } return resultDate; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。