java get the first day and the last day of the current month

获取当前月第一天:
/ ** 
     * Get the current month on the first day 
     * @param month The 
     * @return 
     * / 
    public  static String getFirstDayOfMonth ( int month The) { 
        Calendar Calendar = Calendar.getInstance ();
         // set the month 
        calendar.set (Calendar.MONTH, month --1 );
         // get the minimum number of days a month, 
        int firstDay = calendar.getActualMinimum (Calendar.DAY_OF_MONTH);
         // set the calendar month of the minimum number of days 
        calendar.set (Calendar.DAY_OF_MONTH, firstDay);
         // format the date 
        SimpleDateFormat sdf = new new SimpleDateFormat("yyyy-MM-dd");
        String firstDay = sdf.format(calendar.getTime())+" 00:00:00";
        return firstDay;
    }
获取当前月最后一天:
public  static String getLastDayOfMonth ( int month The) { 
        Calendar Calendar = Calendar.getInstance ();
         // set the month 
        calendar.set (the Calendar.MONTH, month The -. 1 );
         // get the maximum number of days a month, 
        int lastDay = 0 ;
         // 2 -average number of days in the month Swiss 
        IF (month the == 2 ) { 
            lastDay = Calendar.getLeastMaximum (Calendar.DAY_OF_MONTH); 
        } the else { 
            lastDay = calendar.getActualMaximum (Calendar.DAY_OF_MONTH); 
        } 
        //Set the maximum number of days in a calendar month 
        calendar.set (Calendar.DAY_OF_MONTH, lastDay);
         // format the date 
        SimpleDateFormat SDF = new new SimpleDateFormat ( "yyyy-MM-dd" ); 
        String lastDay = sdf.format (calendar.getTime () ) + "23:59:59" ;
         return lastDay; 
    }

 

 
 

Guess you like

Origin www.cnblogs.com/stromgao/p/12048781.html