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;  
    }  
}