时间工具类:获取指定年、季、月的开始和结束时间

public class DateUtils {
    private static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    
    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
        return sf.format(cal.getTime())+" 00:00:00";
    }

    
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, value);
        return sf.format(cal.getTime())+" 23:59:59";

    }
    
      
    public static String getFirstDayOfYear(int year){  
        Calendar calendar = Calendar.getInstance();  
        calendar.clear();  
        calendar.set(Calendar.YEAR, year);  
        Date currYearFirst = calendar.getTime();  
        return sf.format(currYearFirst)+" 00:00:00";
    }  
      
     
    public static String getLastDayOfYear(int year){  
        Calendar calendar = Calendar.getInstance();  
        calendar.clear();  
        calendar.set(Calendar.YEAR, year);  
        calendar.roll(Calendar.DAY_OF_YEAR, -1);  
        Date currYearLast = calendar.getTime();  
          
        return sf.format(currYearLast)+" 23:59:59";
    }  

   
    public static String getFirstDayOfSeason(int year, int season) {
        if(season == 1){
            return String.valueOf(year)+"-01-01 00:00:00";
        }else if(season == 2){
            return String.valueOf(year)+"-04-01 00:00:00";
        }else if(season == 3){
            return String.valueOf(year)+"-07-01 00:00:00";
        }else{
            return String.valueOf(year)+"-10-01 00:00:00";
        }
    }  
    
   
    public static String getLastDayOfSeason(int year, int season) {
        if(season == 1){
            return String.valueOf(year)+"-03-31 23:59:59";
        }else if(season == 2){
            return String.valueOf(year)+"-06-30 23:59:59";
        }else if(season == 3){
            return String.valueOf(year)+"-09-30 23:59:59";
        }else{
            return String.valueOf(year)+"-12-31 23:59:59";
        }
    }
    
    public static void main(String[] args) {
        String s = getFirstDayOfSeason(2013,2);
        String e = getLastDayOfSeason(2013,2);
        System.out.println(s);
        System.out.println(e);
    }
}          

posted @ 2016-01-08 11:24  xxbcoder  阅读(413)  评论(0编辑  收藏  举报