java拓展日期工具类

/**
 * 拓展日期工具类
 * @author hejianwei
 *
 */  
public class MOEDateUtils {  
      
    /**
     * 获取某周得第一天
     * @param year 年
     * @param week 周
     * @return
     */  
    public static Date getFirstDayOfWeek(int year, int week){  
        Calendar c = new GregorianCalendar();  
          
        c.set(Calendar.YEAR, year);  
        c.set(Calendar.WEEK_OF_YEAR,  week);  
        c.setFirstDayOfWeek(Calendar.MONDAY);  
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());  
        c.set(Calendar.HOUR, 0);  
        c.set(Calendar.MINUTE, 0);  
        c.set(Calendar.SECOND, 0);  
          
        return c.getTime();   
    }  
      
    /**
     * 获取某周得最后一天
     * @param year 年
     * @param week 周
     * @return
     */  
    public static Date getLastDayOfWeek(int year, int week){  
        Calendar c = new GregorianCalendar();  
          
        c.set(Calendar.YEAR, year);  
        c.set(Calendar.WEEK_OF_YEAR,  week);  
        c.setFirstDayOfWeek(Calendar.MONDAY);  
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);  
        c.set(Calendar.HOUR, 23);  
        c.set(Calendar.MINUTE, 59);  
        c.set(Calendar.SECOND, 59);  
          
        return c.getTime();   
    }  
      
    /**
     * 获取某月得第一天
     * @param year 年
     * @param month 月
     * @return
     */  
    public static Date getFirstDayOfMonth(int year, int month){  
        Calendar c = new GregorianCalendar();  
          
        c.set(year, month, 1);  
        c.set(Calendar.HOUR, 0);  
        c.set(Calendar.MINUTE, 0);  
        c.set(Calendar.SECOND, 0);  
          
        return c.getTime();   
    }  
      
    /**
     * 获取某月得最后一天
     * @param year 年
     * @param month 月
     * @return
     */  
    public static Date getLastDayOfMonth(int year, int month){  
        Calendar c = new GregorianCalendar();  
        c.clear();
        c.set(Calendar.MONTH, month);  
        int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        c.set(year, month, lastDay);  
//        c.roll(Calendar.DATE, -1);  
//        c.add(Calendar.DATE, -1);
        c.set(Calendar.HOUR, 0);  
        c.set(Calendar.MINUTE, 0);  
        c.set(Calendar.SECOND, 0);  
          
        return c.getTime();   
    }  
      
    /**
     * 获取某季度得第一天
     * @param year 年
     * @param quarter 季度
     * @return
     */  
    public static Date getFirstDayOfQuarter(int year, int quarter){  
        Calendar c = new GregorianCalendar();  
          
        int month = 0;  
        switch (quarter) {  
        case 1:  
            month = 1-1;  
            break;  
        case 2:  
            month = 4-1;          
            break;  
        case 3:  
            month = 7-1;  
            break;  
        case 4:  
            month = 10-1;  
            break;  
        default:  
            month = c.get(Calendar.MONTH);  
            break;  
        }  
          
        return getFirstDayOfMonth(year, month);   
    }  
      
    /**
     * 获取某季度得最一天
     * @param year 年
     * @param quarter 季度
     * @return
     */  
    public static Date getLastDayOfQuarter(int year, int quarter){  
        Calendar c = new GregorianCalendar();  
          
        int month = 0;  
        switch (quarter) {  
        case 1:  
            month = 3-1;  
            break;  
        case 2:  
            month = 6-1;          
            break;  
        case 3:  
            month = 9-1;  
            break;  
        case 4:  
            month = 12-1;  
            break;  
        default:  
            month = c.get(Calendar.MONTH);  
            break;  
        }  
          
        return getLastDayOfMonth(year, month);   
    }  
      
    /**
     * 获取某年得第一天
     * @param year 年
     * @return
     */  
    public static Date getFirstDayOfYear(int year){  
        return getFirstDayOfMonth(year, 0);  
    }  
      
    /**
     * 获取某年得最后一天
     * @param year 年
     * @return
     */  
    public static Date getLastDayOfYear(int year){  
        return getLastDayOfMonth(year, 11);  
    }  
 
    public static void main(String[] args) {  
//        System.out.println("周一: " + DateUtils.formatDate(getFirstDayOfWeek(2016, 25)));  
//  
//        System.out.println("周末: " + DateUtils.formatDate(getLastDayOfWeek(2016, 25)));   
          
        System.out.println("月初:" + DateUtils.formatDate(getFirstDayOfMonth(2016, 6)));  
          
        System.out.println("月末:" + DateUtils.formatDate(getLastDayOfMonth(2016, 8)));  
          
//        System.out.println("季初:" + DateUtils.formatDate(getFirstDayOfQuarter(2016, 1)));  
//          
//        System.out.println("季末:" + DateUtils.formatDate(getLastDayOfQuarter(2016, 1)));  
//          
//        System.out.println("年初:" + DateUtils.formatDate(getFirstDayOfYear(2016)));  
//          
//        System.out.println("年末:" + DateUtils.formatDate(getLastDayOfYear(2016)));
    } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值