JAVA疑难杂症(8)——获取指定日期的年份、月份、日期号、月份起始日期、月份结束日期

static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
    
    public static void main(String[] args) throws ParseException {  
        String date = "2011-04-25 22:28:30";  
        System.out.println(date);  
        System.out.println("年份:" + getYear(date));  
        System.out.println("月份:" + getMonth(date));  
        System.out.println("日期:" + getDay(date));  
        System.out.println("月初日期是: " + getMinMonthDate(date));  
        System.out.println("月末日期是: " + getMaxMonthDate(date));  
    }  
  
    /** 
     * 获取日期年份 
     * @param date 
     * @return 
     * @throws ParseException 
     */  
    public static int getYear(String date) throws ParseException{  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(dateFormat.parse(date));  
        return calendar.get(Calendar.YEAR);  
    }  
      
    /** 
     * 获取日期月份 
     * @param date 
     * @return 
     * @throws ParseException 
     */  
    public static int getMonth(String date) throws ParseException{  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(dateFormat.parse(date));  
        return (calendar.get(Calendar.MONTH) + 1);  
    }  
      
    /** 
     * 获取日期号 
     * @param date 
     * @return 
     * @throws ParseException 
     */  
    public static int getDay(String date) throws ParseException{  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(dateFormat.parse(date));  
        return calendar.get(Calendar.DAY_OF_MONTH);  
    }  
    /** 
     * 获取月份起始日期 
     * @param date 
     * @return 
     * @throws ParseException 
     */  
    public static String getMinMonthDate(String date) throws ParseException{  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(dateFormat.parse(date));  
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));  
        return dateFormat.format(calendar.getTime());  
    }  
      
    /** 
     * 获取月份最后日期 
     * @param date 
     * @return 
     * @throws ParseException 
     */  
    public static String getMaxMonthDate(String date) throws ParseException{  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(dateFormat.parse(date));  
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));  
        return dateFormat.format(calendar.getTime());  
    }  

 

/**
     * 获取指定月份的最后日期
     * @return
     */
    private String getMaxMonthDate() {
        String date = "";
        if (mYearMonth.length()==6) {
            date = mYearMonth.substring(0, 4)
                    +"-"+mYearMonth.substring(4, 6)+"-01 01:01:01";
        }  
        if (mYearMonth.length()==4) {
            date = mYearMonth +"-12-31 01:01:01";
        } 
        Calendar calendar = null;
        try {
            calendar = Calendar.getInstance();
            calendar.setTime(dateFormat.parse(date));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));  
        } catch (ParseException e) {
            setError("ItemOfAccountSummaryBL","getMinMonthDate","获取指定月份的最后日期失败");
            e.printStackTrace();
        }  
        return dateFormat.format(calendar.getTime());  
    }
    
    
    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        AccYuePreQueryUI accYuePreQueryUI = new AccYuePreQueryUI();
        
    }

    /**
     * 获得指定月份的月初时间
     * @return
     */
    private String getMinMonthDate() {
//        String date = "2011-04-25 22:28:30";
        String date = "";
        if (mYearMonth.length()==6) {
            date = mYearMonth.substring(0, 4)
                    +"-"+mYearMonth.substring(4, 6)+"-01 01:01:01";
        }
        if(mYearMonth.length()==4){
            date = mYearMonth +"-01-01 01:01:01";
        }
//        System.out.println(date);
        Calendar calendar = null;
        try {
            calendar = Calendar.getInstance();  
            calendar.setTime(dateFormat.parse(date));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));  
        } catch (ParseException e) {
            setError("ItemOfAccountSummaryBL","getMinMonthDate","获取指定月份的期初时间失败");
            e.printStackTrace();
        }  
        return dateFormat.format(calendar.getTime());  
    }

 

posted @ 2015-06-30 18:01  xu_shuyi  阅读(751)  评论(0)    收藏  举报