码迷,mamicode.com
首页 > 其他好文 > 详细

关于指定日期的获取

时间:2014-08-19 14:16:34      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:blog   java   使用   strong   ar   问题   div   代码   

 

java使用Calendar类获得指定日期

   关于指定日期的获取,是根据指定日期和当前日期相差的天数,然后使用set方法设置Calendar.DAY_OF_MONTH的值。
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - dayPlus);
    (dayPlus表示指定日期和当前日期相差的天数)
    不进行set,默认是获取系统时间,初步测试没有问题,可正确获得指定日期。
    *************************************************************************************************
    代码:
    // 获得当前日期
    public static String getDate() {
        Calendar cal = Calendar.getInstance();
        return getDate(cal);
    }

    // 获得日期
    private static String getDate(Calendar cal) {
        String v_strDate = "";
        int v_intYear = cal.get(Calendar.YEAR);
        int v_intMonth = cal.get(Calendar.MONTH) + 1;
        int v_intDAY = cal.get(Calendar.DAY_OF_MONTH);
        int v_intHOUR = cal.get(Calendar.HOUR_OF_DAY);
        int v_intMINUTE = cal.get(Calendar.MINUTE);
        int v_intSECOND = cal.get(Calendar.SECOND);
        
        if (v_intDAY < 10) {
            v_strDate = v_strDate + "0" + v_intDAY + "-";
        }else {
            v_strDate = v_strDate + v_intDAY + "-";
        }
        if (v_intMonth < 10) {
            v_strDate = v_strDate + "0" + v_intMonth + "-";
        }else {
            v_strDate = v_strDate + v_intMonth + "-";
        }
        v_strDate = v_strDate + v_intYear + " ";
        
        if (v_intHOUR < 10) {
            v_strDate = v_strDate + "0" + v_intHOUR + ":";
        }else {
            v_strDate = v_strDate + v_intHOUR + ":";
        }
        if (v_intMINUTE < 10) {
            v_strDate = v_strDate + "0" + v_intMINUTE + ":";
        } else {
            v_strDate = v_strDate + v_intMINUTE + ":";
        }
        if (v_intSECOND < 10) {
            v_strDate = v_strDate + "0" + v_intSECOND;
        } else {
            v_strDate = v_strDate + v_intSECOND;
        }
        cal = null;
        return v_strDate;
    }

    //获得当前日期与本周日相差的天数
    private static int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {
            return 0;
        } else {
            return dayOfWeek - 1;
        }
    }

    //获得本周一的日期
    public static String getThisMondayDate() {
        Calendar cal = Calendar.getInstance();
        int mondayPlus = getMondayPlus();
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-mondayPlus);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return getDate(cal);
    }
    
    //获得本月1号的日期
    public static String getCurrentMonthBeginDate(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return getDate(cal);
    }
    
    //获得上周一的日期
    public static String getLastMondayDate(){
        Calendar cal = Calendar.getInstance();
        int dayPlus = getMondayPlus()+7;
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-dayPlus);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return getDate(cal);
    }
    
    //获得上周日的日期
    public static String getLastSundayDate(){
        Calendar cal = Calendar.getInstance();
        int dayPlus = getMondayPlus()+1;
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-dayPlus);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return getDate(cal);
    }
    
    //获得上月1号的日期
    public static String getLastMonthBeginDate(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1);
        cal.set(Calendar.DAY_OF_MONTH,1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return getDate(cal);
    }
    
    //获得上个月最后一天的日期
    public static String getLastMonthEndDate(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 0);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return getDate(cal);
    }

    public static void main(String[] args) {
        System.out.println(getDate());
        System.out.println(getThisMondayDate());
        System.out.println(getCurrentMonthBeginDate());
        System.out.println(getLastMondayDate());
        System.out.println(getLastSundayDate());
        System.out.println(getLastMonthBeginDate());
        System.out.println(getLastMonthEndDate());
    }

关于指定日期的获取,布布扣,bubuko.com

关于指定日期的获取

标签:blog   java   使用   strong   ar   问题   div   代码   

原文地址:http://www.cnblogs.com/isoftware/p/3921827.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!