当前位置: 代码迷 >> Web前端 >> 失去某年某月的第一天和最后一天
  详细解决方案

失去某年某月的第一天和最后一天

热度:296   发布时间:2012-08-28 12:37:01.0
得到某年某月的第一天和最后一天

import java.text.SimpleDateFormat;

import java.util.Calendar;

public class FirstEndOfMonth {


 /**
  * 得到某年某月的第一天
  * 
  * @param year
  * @param month
  * @return
  */
 public String getFirstDayOfMonth(int year, int month) {

  Calendar cal = Calendar.getInstance();

  cal.set(Calendar.YEAR, year);

  cal.set(Calendar.MONTH, month-1);

  cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));


  return new SimpleDateFormat("yyyy/MM/dd").format(cal.getTime());
 }
 
 /**
  * 得到某年某月的最后一天
  * 
  * @param year
  * @param month
  * @return
  */ 
 public String getLastDayOfMonth(int year, int month) {

  Calendar cal = Calendar.getInstance();

  cal.set(Calendar.YEAR, year);

  cal.set(Calendar.MONTH, month-1);

   cal.set(Calendar.DAY_OF_MONTH, 1);
  int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
  cal.set(Calendar.DAY_OF_MONTH, value);

  return new SimpleDateFormat("yyyy/MM/dd").format(cal.getTime());

 }



 /**
  * 
  * @param args
  */

 public static void main(String[] args) {

  FirstEndOfMonth tFirstEndOfMonth = new FirstEndOfMonth();
  System.out.println(tFirstEndOfMonth.getFirstDayOfMonth(2012, 2));
  System.out.println(tFirstEndOfMonth.getLastDayOfMonth(2012, 2));

 }

}

  相关解决方案