星空网 > 软件开发 > Java

使用java的Calendar对象获得当前日期的上几个度开始、结束时间

思路:

先获得当前季度的开始和结束日期,在当前日期的基础上往前推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的Calendar对象获得当前日期的上几个度开始、结束时间

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

易税通:https://www.ikjzd.com/w/2043
Red Points:https://www.ikjzd.com/w/2044
JollyPay:https://www.ikjzd.com/w/2045
波罗蜜:https://www.ikjzd.com/w/2046
百伦:https://www.ikjzd.com/w/2047
易麦宝功能,费用,使用介绍:https://www.ikjzd.com/w/2048
SaaS公司要不要实行销售“追回”机制?:https://www.kjdsnews.com/a/1836574.html
美国消费者法案已开始年度认证,卖家请尽快提交!:https://www.kjdsnews.com/a/1836575.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流