• java 周期时期计算


    package org.apple.date;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    /**
     * 周期时间
     * @author shaoyu
     *
     */
    public class CycleDate {
    	
    	public static void main(String[] args) {
    		
    		SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
    		
    		Calendar calendar = Calendar.getInstance();
    		//calendar.setTime(new Date());
    		System.out.println(calendar.get(Calendar.YEAR));
    		System.out.println(calendar.get(Calendar.MONTH)+1);				// 月加1
    		System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
    		System.out.println("当月第几周:"+calendar.get(Calendar.WEEK_OF_MONTH));
    		System.out.println("当年第几周:"+calendar.get(Calendar.WEEK_OF_YEAR));
    		System.out.println(calendar.get(Calendar.DAY_OF_WEEK)-1);		// 周减1
    		
    		int hour_24 = calendar.get(Calendar.HOUR_OF_DAY);
    		int hour_12 = calendar.get(Calendar.HOUR);
    		int minute = calendar.get(Calendar.MINUTE);
    		int second = calendar.get(Calendar.SECOND);
    		System.out.println("24小时制      "+hour_24 +":"+ minute +":" + second);
    		System.out.println("12小时制      "+hour_12 +":"+ minute +":" + second);
    		
    		System.out.println("当前时间:"+dateformat.format(calendar.getTime()) + " ||  毫秒数:"+ calendar.getTimeInMillis());
    		
    		calendar.add(Calendar.MONTH, 1);
    		
    		System.out.println("下一月当前时间:"+dateformat.format(calendar.getTime()) + " ||  毫秒数:"+ calendar.getTimeInMillis());
    		
    		calendar.add(Calendar.WEEK_OF_MONTH, 1);
    		
    		System.out.println("下一周当前时间:" + dateformat.format(calendar.getTime()) + " ||  毫秒数:"+ calendar.getTimeInMillis());
    		
    		calendar.add(Calendar.YEAR, 1);
    		
    		System.out.println("下一年当前时间:" + dateformat.format(calendar.getTime()) + " ||  毫秒数:"+ calendar.getTimeInMillis());
    		
    		//System.out.println("季度开始时间 : "+ dateformat.format(getCurrentQuarterStartTime()));
    		System.out.println("下一季度结果时间: " + dateformat.format(getCurrentQuarterEndTime(calendar.get(Calendar.MONTH)+4)));
    	}
    
    	public Date lastDayOfMonth(Date date) {
    		Calendar calendar = Calendar.getInstance();
    		calendar.setTime(date);
    		calendar.set(Calendar.DAY_OF_MONTH, 1);
    		calendar.roll(Calendar.DAY_OF_MONTH, -1);
    		return calendar.getTime();
    	}
    	
    	/** 
    	    * 当前季度的开始时间 
    	*/  
        public static Date getCurrentQuarterStartTime() {  
        	SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c = Calendar.getInstance();  
            int currentMonth = c.get(Calendar.MONTH) + 1;  
            Date now = null;  
            try {  
                if (currentMonth >= 1 && currentMonth <= 3)  
                     c.set(Calendar.MONTH, 0);  
                 else if (currentMonth >= 4 && currentMonth <= 6)  
                     c.set(Calendar.MONTH, 3);  
                 else if (currentMonth >= 7 && currentMonth <= 9)  
                     c.set(Calendar.MONTH, 6);  
                 else if (currentMonth >= 10 && currentMonth <= 12)  
                     c.set(Calendar.MONTH, 9);  
                 c.set(Calendar.DATE, 1);  
                 now = dateformat.parse(dateformat.format(c.getTime()) + " 00:00");  
             } catch (Exception e) {  
                 e.printStackTrace();  
             }  
             return now;  
         }
        
        /** 
         * 当前季度的结束时间 
         */  
        public static Date getCurrentQuarterEndTime(int currentMonth) {  
        	SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c = Calendar.getInstance();   
            Date now = null;  
            try {  
                if (currentMonth >= 1 && currentMonth <= 3) {  
                    c.set(Calendar.MONTH, 2);  
                    c.set(Calendar.DATE, 31);  
                } else if (currentMonth >= 4 && currentMonth <= 6) {  
                    c.set(Calendar.MONTH, 5);  
                    c.set(Calendar.DATE, 30);  
                } else if (currentMonth >= 7 && currentMonth <= 9) {  
                    c.set(Calendar.MONTH, 8);  
                    c.set(Calendar.DATE, 30);  
                } else if (currentMonth >= 10 && currentMonth <= 12) {  
                   c.set(Calendar.MONTH, 11);  
                   c.set(Calendar.DATE, 31);  
               }  
               now = dateformat.parse(dateformat.format(c.getTime()) + " 23:59");  
           } catch (Exception e) {  
               e.printStackTrace();  
           }  
           return now;  
        }  
    }
    

      

  • 相关阅读:
    PHP开发用工具
    AS3与js的几种简单通讯方式
    圣斗士,饶了我吧。
    有关访问域的深入探讨
    Flex实现将Wrod文档和Excel文档转xml,以及读取csv
    [转]帝国cms目录结构与模板结构以及标签变量介绍
    flash的小技巧,查看操作代码
    在javascript中在function处提示missing(before function parameters错误
    窗口中的对象和元素(BOM)
    IE9下通过windows.status修改浏览器的状态栏,修改不能成功
  • 原文地址:https://www.cnblogs.com/shaoyu19900421/p/4489236.html
Copyright © 2020-2023  润新知