使用java的Calendar对象获得当前日期的上几个度开始、结束时间
2015-07-01 22:18
615 查看
思路:
先获得当前季度的开始和结束日期,在当前日期的基础上往前推3个月即上个季度的开始和结束日期
先获得当前季度的开始和结束日期,在当前日期的基础上往前推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; }
相关文章推荐
- 排序算法之冒泡排序Java版
- Java面向对象之类
- 2015070102 - EffactiveJava笔记 - 第47条 了解和使用类库(1)
- java打包
- JAVA集合之——Comparable和Comparator
- MyBatis数据持久化(十)与Spring4整合
- MyBatis数据持久化(十)与Spring4整合
- SpringMVC+Spring+Mybatis整合
- MyBatis数据持久化(十)与Spring4整合
- myEclipse6.5注册码生成
- 排序算法之堆排序Java版
- Maven搭建SpringMVC+Hibernate项目详解
- 《Java课程实习》日志(周二)
- 在ubuntu下用apt-get安装和管理java
- 排序算法之直接选择排序Java版
- Java 并发包之线程池和原子计数
- Flex&iBatis&Hibernate&Spring—师徒奶茶系统V1总结
- Java知多少(109)数据库更新
- iBATIS&Spring合奏(五)--整合lucene搜索表字段内容
- iBATIS&Spring合奏(四)--设计模式in iBATIS