posts - 41, comments - 15, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

获取上个月的第一天和最后一天

Posted on 2012-03-05 14:36 yuhaibo736 阅读(638) 评论(0)  编辑  收藏
    public static String lastMonFirstDay(){ 
        Calendar cal = Calendar.getInstance(); 
        int year = cal.get(Calendar.YEAR); 
        int month = cal.get(Calendar.MONTH) + 1; 
        cal.set(Calendar.DAY_OF_MONTH, 1); 
        cal.add(Calendar.DAY_OF_MONTH, -1); 
        int day = cal.get(Calendar.DAY_OF_MONTH); 
        String months = ""; 
        String days = ""; 
        if (month > 1) { 
            month--; 
        } else { 
            year--; 
            month = 12; 
        } 
        if (!(String.valueOf(month).length() > 1)) { 
            months = "0" + month; 
        } else { 
            months = String.valueOf(month); 
        } 
        if (!(String.valueOf(day).length() > 1)) { 
            days = "0" + day; 
        } else { 
            days = String.valueOf(day); 
        } 
        String firstDay = "" + year + "-" + months + "-01"; 
        String[] lastMonth = new String[2]; 
        lastMonth[0] = firstDay; 
        return firstDay; 
    } 
     
    public static String lastMonLastDay(){ 
        Calendar cal = Calendar.getInstance(); 
        int year = cal.get(Calendar.YEAR); 
        int month = cal.get(Calendar.MONTH) + 1; 
        cal.set(Calendar.DAY_OF_MONTH, 1); 
        cal.add(Calendar.DAY_OF_MONTH, -1); 
        int day = cal.get(Calendar.DAY_OF_MONTH); 
        String months = ""; 
        String days = ""; 
        if (month > 1) { 
            month--; 
        } else { 
            year--; 
            month = 12; 
        } 
        if (!(String.valueOf(month).length() > 1)) { 
            months = "0" + month; 
        } else { 
            months = String.valueOf(month); 
        } 
        if (!(String.valueOf(day).length() > 1)) { 
            days = "0" + day; 
        } else { 
            days = String.valueOf(day); 
        } 
        String lastDay = "" + year + "-" + months + "-" + days; 
        String[] lastMonth = new String[2]; 
        lastMonth[1] = lastDay; 
        return lastDay; 
    }

只有注册用户登录后才能发表评论。


网站导航: