zoukankan      html  css  js  c++  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;
    	}
    

      

  • 相关阅读:
    vc6编译64位程序
    WebBrowser 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件
    python中subprocess.Popen的使用
    对AutoResetEvent和ManualResetEvent的理解
    Vue-Socket.io跨域问题 has been blocked by CORS policy: No 'Access-Control-Allow-Origin' Mentalflow解决思路
    如何使用GoLand debug
    Python协程与JavaScript协程的对比
    [基础] TCP小结
    导出字段脚本
    永恒之蓝——windows server 2003 漏洞
  • 原文地址:https://www.cnblogs.com/qadada/p/4614692.html
Copyright © 2011-2022 走看看