java Calendar Date Gets the date where the month or the first day and the last day

A. Get passed the first day of the month date falls

    public static Date getFirstDayDateOfMonth(final Date date) {

        final Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        final int last = cal.getActualMinimum(Calendar.DAY_OF_MONTH);

        cal.set(Calendar.DAY_OF_MONTH, last);

        return cal.getTime();

    }

II. Gets the incoming date falls on the last day of the month

    public static Date getLastDayOfMonth(final Date date) {

        final Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        cal.set(Calendar.DAY_OF_MONTH, last);

        return cal.getTime();

    }

III. Gets the first day where the incoming date year

    public static Date getFirstDayDateOfYear(final Date date) {

        final Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        final int last = cal.getActualMinimum(Calendar.DAY_OF_YEAR);

        cal.set(Calendar.DAY_OF_YEAR, last);

        return cal.getTime();

    }

IV. Obtaining incoming date falls on the last day

    public static Date getLastDayOfYear(final Date date) {

        final Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        final int last = cal.getActualMaximum(Calendar.DAY_OF_YEAR);

        cal.set(Calendar.DAY_OF_YEAR, last);

        return cal.getTime();

    }

 V. acquired on the first day of the specified date

/ ** 
      * Get the specified date of the first day 
      * @param year 
      * @param month The 
      * @return 
      * / 
     public  static String getFirstDayOfMonth1 ( int year, int month The) {      
         Calendar CAL = Calendar.getInstance ();   
          // set the year 
         cal.set (Calendar.YEAR, year);
          // set the month 
         cal.set (the Calendar.MONTH,-month The. 1 ); 
          // Get the minimum number of days of a month 
         int firstDay = cal.getMinimum (Calendar.DATE);
          // set the minimum number of days in the calendar month
         cal.set(Calendar.DAY_OF_MONTH,firstDay);  
         //格式化日期
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         return sdf.format(cal.getTime());  
     }

 

VI. Incoming year and month, access to the last day of the month

/ ** 
     * Get the last day of the specified date 
     * @param year 
     * @param month The 
     * @return 
     * / 
     public  static String getLastDayOfMonth1 ( int year, int month The) {      
         Calendar cal = Calendar.getInstance ();     
          // set the year   
         cal the .set (Calendar.YEAR, year);  
          // set the month   
         cal.set (of the Calendar.MONTH, month The-1 ); 
          // get the maximum number of days a month, 
         int lastDay = cal.getActualMaximum (Calendar.DATE);
          //Setting the calendar month the maximum number of days   
         cal.set (Calendar.DAY_OF_MONTH, lastDay);  
          // format the date 
         the SimpleDateFormat SDF = new new the SimpleDateFormat ( "the MM-dd-YYYY" );  
          return sdf.format (cal.getTime ()); 
     }

 

Guess you like

Origin www.cnblogs.com/lingtiaoti/p/11306572.html