posts - 14,comments - 11,trackbacks - 0

  取一个月的最后一天,一个非常愚蠢的方法:
                 Calendar MonthEnd=Calendar.getInstance();
                 MonthEnd.clear(); 
                 MonthEnd.set(Calendar.YEAR, Integer.parseInt(year));
                 MonthEnd.set(Calendar.MONTH,Integer.parseInt(month));   
   //写本月的数字实际上返回的是下个月
                 MonthEnd.set(Calendar.DATE,1);                 //设置成下月的第一天
                 MonthEnd.add(Calendar.DATE,-1);              //再减掉一天
                 int end=MonthEnd.get(Calendar.DAY_OF_MONTH);    //就是这个月的最后一天了
 
  Calendar原来就有这么一种方法:
                 Calendar MonthEnd=Calendar.getInstance();
                 MonthEnd.clear(); 
                 MonthEnd.set(Calendar.YEAR, Integer.parseInt(year));
                 MonthEnd.set(Calendar.MONTH,Integer.parseInt(month)-1);
                 int end=MonthEnd.getActualMaximum(MonthEnd.DAY_OF_MONTH);   //直接得就OK
                 //也可以取一个月的最小一天
                 int first=MonthEnd.getActualMinimum(MonthEnd.DAY_OF_MONTH);

 

示例:

 

import java.util.Calendar;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class DateTimeUtil {

 /**
  * 由java.util.Date到java.sql.Date的类型转换
  *
  * @param date
  * @return Date
  */
 public static Date getSqlDate(java.util.Date date) {
  return new Date(date.getTime());
 }

 public static Date nowDate() {
  Calendar calendar = Calendar.getInstance();
  return getSqlDate(calendar.getTime());
 }

 /**
  * 获得某一日期的后一天
  *
  * @param date
  * @return Date
  */
 public static Date getNextDate(Date date) {
  Calendar calendar = Calendar.getInstance();
  calendar.setTime(date);
  int day = calendar.get(Calendar.DATE);
  calendar.set(Calendar.DATE, day + 1);
  return getSqlDate(calendar.getTime());
 }

 /**
  * 获得某一日期的前一天
  *
  * @param date
  * @return Date
  */
 public static Date getPreviousDate(Date date) {
  Calendar calendar = Calendar.getInstance();
  calendar.setTime(date);
  int day = calendar.get(Calendar.DATE);
  calendar.set(Calendar.DATE, day - 1);
  return getSqlDate(calendar.getTime());
 }

 /**
  * 获得某年某月第一天的日期
  *
  * @param year
  * @param month
  * @return Date
  */
 public static Date getFirstDayOfMonth(int year, int month) {
  Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.YEAR, year);
  calendar.set(Calendar.MONTH, month - 1);
  calendar.set(Calendar.DATE, 1);
  return getSqlDate(calendar.getTime());
 }

 /**
  * 获得某年某月最后一天的日期
  *
  * @param year
  * @param month
  * @return Date
  */
 public static Date getLastDayOfMonth(int year, int month) {
  Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.YEAR, year);
  calendar.set(Calendar.MONTH, month);
  calendar.set(Calendar.DATE, 1);
  return getPreviousDate(getSqlDate(calendar.getTime()));
 }

 /**
  * 由年月日构建java.sql.Date类型
  *
  * @param year
  * @param month
  * @param date
  * @return Date
  */
 public static Date buildDate(int year, int month, int date) {
  Calendar calendar = Calendar.getInstance();
  calendar.set(year, month - 1, date);
  return getSqlDate(calendar.getTime());
 }

 /**
  * 取得某月的天数
  *
  * @param year
  * @param month
  * @return int
  */
 public static int getDayCountOfMonth(int year, int month) {
  Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.YEAR, year);
  calendar.set(Calendar.MONTH, month);
  calendar.set(Calendar.DATE, 0);
  return calendar.get(Calendar.DATE);
 }

 /**
  * 获得某年某季度的最后一天的日期
  *
  * @param year
  * @param quarter
  * @return Date
  */
 public static Date getLastDayOfQuarter(int year, int quarter) {
  int month = 0;
  if (quarter > 4) {
   return null;
  } else {
   month = quarter * 3;
  }
  return getLastDayOfMonth(year, month);

 }

 /**
  * 获得某年某季度的第一天的日期
  *
  * @param year
  * @param quarter
  * @return Date
  */
 public static Date getFirstDayOfQuarter(int year, int quarter) {
  int month = 0;
  if (quarter > 4) {
   return null;
  } else {
   month = (quarter - 1) * 3 + 1;
  }
  return getFirstDayOfMonth(year, month);
 }

 /**
  * 获得某年的第一天的日期
  *
  * @param year
  * @return Date
  */
 public static Date getFirstDayOfYear(int year) {
  return getFirstDayOfMonth(year, 1);
 }

 /**
  * 获得某年的最后一天的日期
  *
  * @param year
  * @return Date
  */
 public static Date getLastDayOfYear(int year) {
  return getLastDayOfMonth(year, 12);
 }

 /**
  * String到java.sql.Date的类型转换
  *
  * @param param
  * @return Date
  */
 public static java.sql.Date StringToDate(String param) {

  if (param.equals(null)) {

   return null;
  
  } else {
   java.util.Date date = null;
   try {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    date = sdf.parse(param);
    return new Date(date.getTime());
   } catch (ParseException ex) {
    // ex.printStackTrace();
    return null;
   }
  }
 }
}

 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/shazhenzhong/archive/2008/09/23/2968501.aspx

posted on 2009-06-12 10:45 阿丁 阅读(1795) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: