java獲取時間所在月第一天和最後一天

/**
     * 獲取時間所在月第一天和最後一天
     * @param start_time
     * @param num 0-第一天  1-最後一天
     * @return
     */
    public static int getEndDayOrOneDay(Date start_time,int num){
    	String time = "";
    	Calendar cale = Calendar.getInstance();
    	SimpleDateFormat format = new SimpleDateFormat("dd");   
    	if(num==0){
    		// 獲取前月的第一天
        	cale = Calendar.getInstance();
        	cale.setTime(start_time);
    		cale.add(Calendar.MONTH, 0);
        	cale.set(Calendar.DAY_OF_MONTH, 1);
    		time = format.format(cale.getTime());
    	}else{
    		// 獲取前月的最後一天
    		cale = Calendar.getInstance();
        	cale.setTime(start_time);
        	cale.add(Calendar.MONTH, 1);
        	cale.set(Calendar.DAY_OF_MONTH, 0);
    		time = format.format(cale.getTime());
    	}
		return Integer.parseInt(time);
    }

測試一下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章