---
title: "日期工具类"
author: "wangzg"
date: "2011-05-28T20:01:18Z"
tags: ["C", "C#", "C++", "Oracle", "SQL"]
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-05-28T20:01:18Z"
id: 1063268
url: "https://wangzg.iteye.com/blog/1063268"
---

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

import org.apache.commons.logging.Log;  
import org.apache.commons.logging.LogFactory;

 

/\**  
 * @author cui 2004-12-21 Table-TableManagementData  
 \*/  
public class DateUtil {

 public final static String ORACLE\_DATE\_PATTERN = "YYYY-MM-DD HH24MISS";

 public final static String DEFAULT\_DATE\_PATTERN = "yyyy-MM-dd HHmmss";

 public final static String FULL\_DATE\_PATTERN = "yyyy-MM-dd HH:mm:ss";

 public final static String LONG\_DATE\_PATTERN = "yyyy-MM-dd HH:mm:ss:SSS";

 public final static String SHORT\_DATE\_PATTERN = "yyyy-MM-dd";

 public final static String DATE\_PATTERN = "yyyyMMddHHmmss";

 private final static Log logger = LogFactory.getLog(DateUtil.class);

 /\**  
  *  
  \*/  
 public DateUtil() {  
 }

 /\**  
  * 返回日期中的年份  
  * @author cui  
  * @date Jun 26, 2009  
  * @param date 日期参数  
  * @param strFormat 格式串  
  * @return  
  \*/  
 public static int getInt(Date date, String strFormat) {  
  Calendar c = Calendar.getInstance();  
  c.setTime(date);  
  return c.get(Calendar.YEAR);

 }

 /\**  
  * 返回给定日期的格式化串  
  * @author cui  
  * @date Jun 26, 2009  
  * @param timeStamp 时间对像  
  * @return 返回格式为yyyy年MM月dd日 HH时mm分ss秒  
  \*/  
 public static String getFormatDate(Timestamp timeStamp) {  
  String strRet = null;  
  if (timeStamp == null)  
   return strRet = "&nbsp;";  
  SimpleDateFormat simple = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
  if (simple != null)  
   strRet = simple.format(timeStamp);  
  return strRet;  
 }

 /\**  
  * 返回给定日期的格式化串  
  * @author cui   
  * @date Jun 26, 2009  
  * @param date 日期对像  
  * @return 返回yyyy年MM月dd日 HH时mm分ss秒  
  \*/  
 public static String getFormatDate(java.sql.Date date) {

  String strRet = null;  
  if (date == null) {  
   return strRet = "&nbsp;";  
  }  
  SimpleDateFormat simple = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
  if (simple != null)  
   strRet = simple.format(date);  
  return strRet;  
 }

 /\**  
  * 返回当日日期的格式化串,格式为format  
  * @author cui  
  * @date Jun 26, 2009  
  * @param format 格式化串  
  * @return 返回被格式化的当日日期  
  \*/  
 public static String getTodayDate(String format) {  
  String strRet = null;  
  SimpleDateFormat simple = new SimpleDateFormat(format);  
  if (simple != null)  
   strRet = simple.format(new Date());  
  return strRet;  
 }

 /\**  
  * 返回当日日期的格式化串  
  * @author cui  
  * @date Jun 26, 2009  
  * @return 格式为yyyy-MM-dd  
  \*/  
 public static String getTodayDay() {  
  String strRet = null;  
  SimpleDateFormat simple = new SimpleDateFormat(SHORT\_DATE\_PATTERN);  
  if (simple != null)  
   strRet = simple.format(new Date());  
  return strRet;  
 }

 /\**  
  * 返回昨日日期的格式化串  
  * @author cui  
  * @date Jun 26, 2009  
  * @return 格式为yyyy-MM-dd  
  \*/  
 public static String getYesterDay() {  
  String strRet = null;  
  SimpleDateFormat simple = new SimpleDateFormat(SHORT\_DATE\_PATTERN);  
  if (simple != null)  
   strRet = simple.format(addDateByDay(new Date(), -1));  
  return strRet;  
 }

 /\**  
  * 返回给定日期的格式化串  
  * @author cui  
  * @date Jun 26, 2009  
  * @param date 日期参数  
  * @return 格式为yyyy-MM-dd  
  \*/  
 public static String getDayByDate(Date date) {  
  String strRet = null;  
  SimpleDateFormat simple = new SimpleDateFormat(SHORT\_DATE\_PATTERN);  
  if (simple != null)  
   strRet = simple.format(date);  
  return strRet;  
 }

 /\**  
  * 格式化日期,去掉日期中的时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @param date 日期参数  
  * @return 返回忽略日期中的时间部分的日期  
  \*/  
 public static Date getDay(java.util.Date date) {  
  String strDate = getFormatDate(date, SHORT\_DATE\_PATTERN);  
  if (!StringUtil.isEmpty(strDate)) {  
   return getDate(strDate, SHORT\_DATE\_PATTERN);  
  }  
  return null;  
 }

 /\**  
  * 获取给定日期的格式化串  
  * @author cui  
  * @date Jun 26, 2009  
  * @param date 日期参数  
  * @param strFormat 格式化串  
  * @return 返回被格式化的日期串，如果日期参数为空返回&nbsp;  
  \*/  
 public static String getFormatDate(java.sql.Date date, String strFormat) {  
  String strRet = null;  
  if (date == null) {  
   return strRet = "&nbsp;";  
  }  
  SimpleDateFormat simple = new SimpleDateFormat(strFormat);  
  if (simple != null)  
   strRet = simple.format(date);  
  return strRet;  
 }

 /\**  
  * 对给定日期按格式strFormat进行格式化串处理，并将处理结果按格式toFromat转为日期(如把日期格式化为串yyyy-MM-dd 23:59:59,再接着把串变为日期)  
  * @author cui  
  * @date Jun 26, 2009  
  * @param date 日期参数  
  * @param strFormat 日期格式化串  
  * @param toFromat 日期格式化串  
  * @return 格式化好的日期  
  \*/  
 public static Date getDate(Date date, String strFormat, String toFromat) {  
  String strRet = null;  
  Date rtnDate = null;  
  SimpleDateFormat simple = new SimpleDateFormat(strFormat);  
  if (simple != null) {  
   strRet = simple.format(date);  
  }  
  simple = new SimpleDateFormat(toFromat);  
  if (simple != null) {  
   try {  
    rtnDate = simple.parse(strRet);  
   } catch (ParseException e) {  
   }  
  }  
  return rtnDate;  
 }

 /\**  
  * 获取某个日期(格式必须为yyyy-MM-dd)的起始时间(时间精确到00:00:00)  
  * @author cui  
  * @date Jun 26, 2009  
  * @param date 日期串,格式为yyyy-MM-dd  
  * @return 起始日期  
  \*/  
 public static Date getBeginDate(String date) {  
  if (date!=null && !"".equals(date)) {  
   String beginDate = date + " 00:00:00";  
   return DateUtil.getDate(beginDate, FULL\_DATE\_PATTERN);  
  }  
  return null;  
 }

 /\**  
  * 获取某个日期的结束时间(时间精确到23:59:59:999)  
  * @author cui  
  * @date Jun 26, 2009  
  * @param date 日期串,格式为yyyy-MM-dd  
  * @return 结束时间  
  \*/  
 public static Date getEndDate(String date) {  
  if (date!=null && !"".equals(date)) {  
   String beginDate = date + " 23:59:59:999";  
   return DateUtil.getDate(beginDate, LONG\_DATE\_PATTERN);  
  }  
  return null;  
 }

 /\**  
  * 返回当天的起始时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @return  
  \*/  
 public static Date getStartCurrentDay() {  
  String date = getFormatDate(new Date(), "yyyy-MM-dd 00:00:00");  
  return getDate(date, "yyyy-MM-dd HH:mm:ss");  
 }

 /\**  
  * 返回当天的结束时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @return  
  \*/  
 public static Date getEndCurrentDay() {  
  String date = getFormatDate(new Date(), "yyyy-MM-dd 23:59:59:999");  
  return getDate(date, "yyyy-MM-dd HH:mm:ss:SSS");  
 }

 /\**  
  * 获取某个日期累加指定天数后的起始时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @param day 日期参数  
  * @param i 累加天数  
  * @return 起始时间  
  \*/  
 public static Date getStartDay(Date day, int i) {  
  String date = getFormatDate(addDateByDay(day, i), "yyyy-MM-dd 00:00:00");  
  return getDate(date, "yyyy-MM-dd HH:mm:ss");  
 }

 /\**  
  * 获取某个日期累加指定天数后的结束时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @param day 日期参数  
  * @param i 累加天数  
  * @return 结束时间  
  \*/  
 public static Date getEndDay(Date day, int i) {  
  String date = getFormatDate(addDateByDay(day, i), "yyyy-MM-dd 23:59:59:999");  
  return getDate(date, "yyyy-MM-dd HH:mm:ss:SSS");  
 }

 /\**  
  * 获取明日的起始时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @return 起始时间  
  \*/  
 public static Date getStartNextDay() {  
  String date = getFormatDate(addDateByDay(new Date(), 1), "yyyy-MM-dd 00:00:00");  
  return getDate(date, "yyyy-MM-dd HH:mm:ss");  
 }

 /\**  
  * 获取本周的第一天  
  * @author cui  
  * @date Jun 26, 2009  
  * @return 本周的第一天  
  \*/  
 public static Date getWeekFirstDay() {  
  Calendar c = Calendar.getInstance();  
  c.setTime(DateUtil.getDay(c.getTime()));  
  c.set(Calendar.DAY\_OF\_WEEK, 1);  
  return c.getTime();  
 }

 /\**  
  * 获取当前日期累加指定天数后的起始时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @param i 累加天数  
  * @return  
  \*/  
 public static Date getNextDay(int i) {  
  String date = getFormatDate(addDateByDay(new Date(), i), "yyyy-MM-dd 00:00:00");  
  return getDate(date, "yyyy-MM-dd HH:mm:ss");  
 }

 /\**  
  * 获取当前日期累加指定天数的格式化时间(例:可以直接获取明天的12:30:00的时间)  
  * @author cui  
  * @date Jun 26, 2009  
  * @param i 累加天数  
  * @param strFormat 格式化串(如yyyy-MM-dd 12:30:00)  
  * @return 时间  
  \*/  
 public static Date getNextDay(int i, String strFormat) {  
  String date = getFormatDate(addDateByDay(new Date(), i), strFormat);  
  return getDate(date, "yyyy-MM-dd HH:mm:ss");  
 }

 /\**  
  * 获取某个日期某个格式化的时间(例:可以获取某个日期的12:30:00的时间)  
  * @author cui  
  * @date Jun 26, 2009  
  * @param cdate 日期参数  
  * @param strFormat 格式化串(如yyyy-MM-dd 12:30:00)  
  * @return 时间  
  \*/  
 public static Date getDate(Date cdate, String strFormat) {  
  String date = getFormatDate(cdate, strFormat);  
  return getDate(date, "yyyy-MM-dd HH:mm:ss");  
 }

 /\**  
  * 返回某个日期的格式化串  
  * @author cui  
  * @date Jun 26, 2009  
  * @param date 日期参数  
  * @param strFormat 格式化串  
  * @return 格式化串(如果参数为空返回空串)  
  \*/  
 public static String getFormatDate(Date date, String strFormat) {  
  String strRet = null;  
  if (date == null) {  
   return strRet = "";  
  }  
  SimpleDateFormat simple = new SimpleDateFormat(strFormat);  
  if (simple != null)  
   strRet = simple.format(date);  
  return strRet;  
 }

 /\**  
  * 返回某个时间的格式化串  
  * @author cui  
  * @date Jun 26, 2009  
  * @param timeStamp 时间参数  
  * @param strFormat 格式化串  
  * @return 格式化串(如果参数为空返回&nbsp;)  
  \*/  
 public static String getFormatDate(Timestamp timeStamp, String strFormat) {  
  String strRet = null;  
  if (timeStamp == null)  
   return strRet = "&nbsp;";  
  try {  
   SimpleDateFormat simple = new SimpleDateFormat(strFormat);  
   if (simple != null) {  
    strRet = simple.format(timeStamp);  
   }  
  } catch (Exception e) {  
   logger.error("Formate Date Error" + e.getMessage(), e);  
  }  
  return strRet;  
 }

 /\**  
  * 返回当天的年月日串  
  * @author cui  
  * @date Jun 26, 2009  
  * @return 返回格式化串(yyMMdd)  
  \*/  
 public static String yyMMdd() {  
  SimpleDateFormat simple = new SimpleDateFormat("yyMMdd");  
  Date date = new Date();  
  return simple.format(date);  
 }

 /\**  
  * 返回当天的年月串  
  * @author cui  
  * @date Jun 26, 2009  
  * @return 返回格式化串(yyMM)  
  \*/  
 public static String yyMM() {  
  SimpleDateFormat simple = new SimpleDateFormat("yyMM");  
  Date date = new Date();  
  return simple.format(date);  
 }

 /\**  
  * 是否同一个月  
  * @param date1  
  * @param date2  
  * @return  
  \*/  
 public static boolean isSameMonth(Date date1, Date date2) {  
  if (date1 == null || date2 == null) {  
   return false;  
  }  
  Calendar c = Calendar.getInstance();  
  int m1, m2;  
  c.setTime(date1);  
  m1 = c.get(Calendar.MONTH);  
  c.setTime(date2);  
  m2 = c.get(Calendar.MONTH);  
  return m1 == m2;  
 }

 /\**  
  * 是否同一个小时  
  * @param date1  
  * @param date2  
  * @return  
  \*/  
 public static boolean isSameHour(Date date1, Date date2) {  
  if (date1 == null || date2 == null) {  
   return false;  
  }  
  Calendar c = Calendar.getInstance();  
  int m1, m2;  
  c.setTime(date1);  
  m1 = c.get(Calendar.HOUR\_OF\_DAY);  
  c.setTime(date2);  
  m2 = c.get(Calendar.HOUR\_OF\_DAY);  
  return m1 == m2;  
 }

 /\**  
  * 是否同一天  
  * @param date1  
  * @param date2  
  * @return  
  \*/  
 public static boolean isSameDay(Date date1, Date date2) {  
  if (date1 == null || date2 == null) {  
   return false;  
  }  
  String str1 = DateUtil.getDayByDate(date1);  
  String str2 = DateUtil.getDayByDate(date2);  
  return str1.equals(str2);  
 }

 /\**  
  * 获取下月的每一天  
  * @author cui  
  * @date Jun 26, 2009  
  * @return 日期  
  \*/  
 public static Date getNextMonthDay() {  
  Calendar c = Calendar.getInstance();  
  c.add(Calendar.MONTH, 1);  
  c.set(Calendar.DAY\_OF\_MONTH, 1);  
  return c.getTime();  
 }

 /\**  
  * 获取上月的第一天起始时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @return 日期  
  \*/  
 public static Date getPreviousMonthFirstDay() {  
  Calendar c = Calendar.getInstance();  
  c.set(Calendar.HOUR\_OF\_DAY, 0);  
  c.set(Calendar.MINUTE, 0);  
  c.set(Calendar.SECOND, 0);  
  c.set(Calendar.MILLISECOND, 0);  
  c.add(Calendar.MONTH, -1);  
  c.set(Calendar.DAY\_OF\_MONTH, 1);  
  return c.getTime();  
 }

 /\**  
  * 获取上月第一天的结束时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @return  
  \*/  
 public static Date getPreviousMonthLastDay() {  
  Calendar cl = Calendar.getInstance();  
  cl.set(Calendar.DAY\_OF\_MONTH, 0);  
  cl.set(Calendar.HOUR\_OF\_DAY, 23);  
  cl.set(Calendar.MINUTE, 59);  
  cl.set(Calendar.SECOND, 59);  
  return cl.getTime();  
 }

 /\**  
  * 获取本月的第一天  
  * @author cui  
  * @date Jun 26, 2009  
  * @return  
  \*/  
 public static Date getDateByCurrentMonth() {  
  Calendar c = Calendar.getInstance();  
  c.setTime(DateUtil.getDay(c.getTime()));  
  c.set(Calendar.DAY\_OF\_MONTH, 1);  
  return c.getTime();  
 }

 /\**  
  * 获取指定日期当月的每一天日期  
  * @author cui  
  * @date Jun 26, 2009  
  * @param currentDate  
  * @return  
  \*/  
 public static Date getNextMonthDay(Date currentDate) {  
  Calendar c = Calendar.getInstance();  
  c.setTime(currentDate);  
  c.add(Calendar.MONTH, 1);  
  c.set(Calendar.DAY\_OF\_MONTH, 1);  
  return c.getTime();  
 }

 /\**  
  * 日期是否是当前月  
  * @author cui  
  * @date Jun 26, 2009  
  * @param date  
  * @return  
  \*/  
 public static boolean isCurrentMonth(Date date) {  
  if (date == null) {  
   return false;  
  }  
  Calendar c = Calendar.getInstance();  
  int m1 = c.get(Calendar.MONTH);  
  c.setTime(date);  
  int m2 = c.get(Calendar.MONTH);  
  return m1 == m2;  
 }

 /\**  
  * 获取当天是周的第几天减1  
  * @author cui  
  * @date Jun 26, 2009  
  * @return  
  \*/  
 public static int getWeekDay() {  
  int weekday;  
  Calendar tmp = Calendar.getInstance();  
  weekday = tmp.get(Calendar.DAY\_OF\_WEEK) - 1;  
  return weekday;  
 }

 /\**  
  * 获取当前日期是星期几的文字描述  
  * @author cui  
  * @date Jun 26, 2009  
  * @return  
  \*/  
 public static String getWeekText() {  
  String txt = "星期";  
  int weekday;  
  Calendar tmp = Calendar.getInstance();  
  weekday = tmp.get(Calendar.DAY\_OF\_WEEK) - 1;  
  switch (weekday) {  
  case 1:  
   txt += "一";  
   break;  
  case 2:  
   txt += "二";  
   break;  
  case 3:  
   txt += "三";  
   break;  
  case 4:  
   txt += "四";  
   break;  
  case 5:  
   txt += "五";  
   break;  
  case 6:  
   txt += "六";  
   break;  
  case 0:  
   txt += "日";  
   break;  
  }  
  return txt;  
 }

 /\**  
  * 获取格式化日期  
  * @author cui  
  * @date Jun 26, 2009  
  * @param time  
  * @param 格式化串  
  * @return  
  \*/  
 public static Date getDate(String time, String strFormat) {  
  Date date = null;  
  if (time == null || time.equals("")) {  
   return date = null;  
  }  
  time = StringUtil.dealNull(time);  
  try {  
   SimpleDateFormat simple = new SimpleDateFormat(strFormat);  
   date = simple.parse(time);  
  } catch (Exception e) {  
   logger.error("Get Date Error!" + e.getMessage(), e);  
  }  
  return date;  
 }

 /\**  
  * 更新日期的日分秒  
  * @author cui  
  * @date Jun 26, 2009  
  * @param date 日期参数  
  * @param hh 小时  
  * @param mm 分数  
  * @param ss 秒  
  * @return  
  \*/  
 public static Date getDate(Date date, int hh, int mm, int ss) {  
  Calendar cal = Calendar.getInstance();  
  cal.setTime(date);  
  cal.set(Calendar.HOUR\_OF\_DAY, hh);  
  cal.set(Calendar.MINUTE, mm);  
  cal.set(Calendar.SECOND, ss);  
  return cal.getTime();  
 }

 /\**  
  * 给日期累加指定天数并返回累加后的日期  
  * @author cui  
  * @date Jun 26, 2009  
  * @param date 日期参数  
  * @param day 累加天数  
  * @return 返回累加后的日期  
  \*/  
 public static Date absoluteDate(Date date, int day) {  
  if (date == null) {  
   return new Date();  
  }  
  Calendar cal = Calendar.getInstance();  
  cal.setTime(date);  
  cal.add(Calendar.DAY\_OF\_YEAR, day);  
  return cal.getTime();  
 }

 /\**  
  *  给当天日期累加指定天数并返回累加后的日期  
  * @author cui  
  * @date Jun 26, 2009  
  * @param day 累加天数  
  * @return 返回累加后的日期  
  \*/  
 public static Date absoluteDate(int day) {  
  Calendar cal = Calendar.getInstance();  
  cal.setTime(new Date());  
  cal.add(Calendar.DAY\_OF\_YEAR, day);  
  return cal.getTime();  
 }

 /\**  
  * 把串转为日期  
  * @author 作者  
  * @date Jun 26, 2009  
  * @param time 日期串  
  * @param strFormat 格式化串  
  * @return 时间  
  \*/  
 public static Timestamp getTimestampDate(String time, String strFormat) {  
  Timestamp tsmp = null;  
  if (time == null) {  
   return tsmp = null;  
  }

  time = StringUtil.dealNull(time);  
  try {  
   SimpleDateFormat simple = new SimpleDateFormat(strFormat);  
   java.util.Date date = simple.parse(time);  
   tsmp = new Timestamp(date.getTime());  
  } catch (Exception e) {  
   logger.error("Get Timestamp Error!" + e.getMessage(), e);  
  }  
  return tsmp;  
 }

 /\**  
  * 给日期累加指定天数并返回累加后的日期  
  * @author cui  
  * @date Jun 26, 2009  
  * @param time 日期参数  
  * @param add\_day 累加天数  
  * @return 累加后的日期  
  \*/  
 public static Date addDateByDay(Date time, int add\_day) {

  if (time == null) {  
   return new Date();  
  }  
  Calendar cal = Calendar.getInstance();  
  cal.setTime(time);  
  cal.add(Calendar.DAY\_OF\_YEAR, add\_day);  
  return cal.getTime();  
 }

 /\**  
  * 给日期累加指定周数并返回累加后的日期  
  * @author cui  
  * @date Jun 26, 2009  
  * @param time 日期参数  
  * @param add\_week 累加周数  
  * @return 累加后的日期  
  \*/  
 public static Date addDateByWeek(Date time, int add\_week) {

  if (time == null) {  
   return new Date();  
  }  
  Calendar cal = Calendar.getInstance();  
  cal.setTime(time);  
  cal.add(Calendar.WEEK\_OF\_MONTH, add\_week);  
  return cal.getTime();  
 }

 /\**  
  * 给日期累加指定天数并返回累加后的日期  
  * @author cui  
  * @date Jun 26, 2009  
  * @param time 日期参数  
  * @param add\_day 累加天数  
  * @return  
  \*/  
 public static Date addDateByDay(Timestamp time, int add\_day) {

  if (time == null) {  
   return new Date();  
  }  
  Calendar cal = Calendar.getInstance();  
  cal.setTime(time);  
  cal.add(Calendar.DAY\_OF\_YEAR, add\_day);  
  return cal.getTime();  
 }

 /\**  
  * 给日期累加指定天数并返回累加并去掉时分秒后的日期  
  * @author cui  
  * @date Jun 26, 2009  
  * @param date 日期参数  
  * @param add\_day 累回天数  
  * @return  
  \*/  
 public static Date getRollDay(Date date, int add\_day) {  
  return getDay(addDateByDay(date, add\_day));  
 }

 /\**  
  * 返回两个日期相差的秒数  
  * @author cui  
  * @date Jun 26, 2009  
  * @param begin  
  * @param end  
  * @return 相差的秒数  
  \*/  
 public static long between(Date begin, Date end) {  
  if (begin == null || end == null) {  
   return 0;  
  }  
  return (end.getTime() - begin.getTime()) / 1000;  
 }

 /\**  
  * 给指定日期累加上指定的月数  
  * @author cui  
  * @date Jun 26, 2009  
  * @param time 日期参数  
  * @param add\_month 累加月数  
  * @return 累加后的日期  
  \*/  
 public static Date addDateByMonth(Date time, int add\_month) {  
  if (time == null) {  
   return new Date();  
  }  
  Calendar cal = Calendar.getInstance();  
  cal.setTime(time);  
  cal.add(Calendar.MONTH, add\_month);  
  return cal.getTime();  
 }

 /\**  
  * 给指定日期累加上指定的小时数  
  * @author cui  
  * @date Jun 26, 2009  
  * @param time 日期参数  
  * @param add\_Hour 累加小时数  
  * @return 累加后的日期  
  \*/  
 public static Date addHourByDay(Date time, int add\_Hour) {

  if (time == null) {  
   return new Date();  
  }  
  Calendar cal = Calendar.getInstance();  
  cal.setTime(time);  
  cal.add(Calendar.HOUR\_OF\_DAY, add\_Hour);  
  return cal.getTime();  
 }

 /\**  
  * 给指定日期累加上指定的分钟数  
  * @author cui  
  * @date Jun 26, 2009  
  * @param time 日期参数  
  * @param add\_minute 分钟数  
  * @return 累加后的日期  
  \*/  
 public static Date addHourByMinute(Date time, int add\_minute) {  
  if (time == null) {  
   return new Date();  
  }  
  Calendar cal = Calendar.getInstance();  
  cal.setTime(time);  
  cal.add(Calendar.MINUTE, add\_minute);  
  return cal.getTime();  
 }

 /\**  
  * 给指定日期累加上指定的秒数  
  * @author cui  
  * @date Jun 26, 2009  
  * @param time 日期参数  
  * @param add\_second 秒数  
  * @return 累加后的日期  
  \*/  
 public static Date addHourBySecond(Date time, int add\_second) {  
  if (time == null) {  
   return new Date();  
  }  
  Calendar cal = Calendar.getInstance();  
  cal.setTime(time);  
  cal.add(Calendar.SECOND, add\_second);  
  return cal.getTime();  
 }

 /\**  
  * 获取本周一起始时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @return 周一起始时间  
  \*/  
 public static Date getThisWeekFirstDay() {  
  Calendar cl = Calendar.getInstance();  
  cl.roll(Calendar.DAY\_OF\_YEAR, -cl.get(Calendar.DAY\_OF\_WEEK) + 2);  
  String date = getFormatDate(cl.getTime(), "yyyy-MM-dd 00:00:00");  
  return getDate(date, "yyyy-MM-dd HH:mm:ss");  
 }  
   
 /\**  
  * 获取本周日结束时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @return 周日结束时间  
  \*/  
 public static Date getThisWeekLastDay() {  
  Calendar cl = Calendar.getInstance();  
  cl.roll(Calendar.DAY\_OF\_YEAR, -cl.get(Calendar.DAY\_OF\_WEEK) + 8);  
  String date = getFormatDate(cl.getTime(), "yyyy-MM-dd 23:59:59:999");  
  return getDate(date, "yyyy-MM-dd HH:mm:ss:SSS");  
 }

 /\**  
  * 获取上周一起始时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @return 上周一起始时间  
  \*/  
 public static Date getPreviousWeekFirstDay() {  
  Calendar cl = Calendar.getInstance();  
  cl.roll(Calendar.DAY\_OF\_YEAR, -cl.get(Calendar.DAY\_OF\_WEEK) - 5);  
  String date = getFormatDate(cl.getTime(), "yyyy-MM-dd 00:00:00");  
  return getDate(date, "yyyy-MM-dd HH:mm:ss");  
 }

 /\**  
  * 获取上周日结束时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @return 上周日结束时间  
  \*/  
 public static Date getPreviousWeekLastDay() {  
  Calendar cl = Calendar.getInstance();  
  cl.roll(Calendar.DAY\_OF\_YEAR, -cl.get(Calendar.DAY\_OF\_WEEK) + 1);  
  String date = getFormatDate(cl.getTime(), "yyyy-MM-dd 23:59:59:999");  
  return getDate(date, "yyyy-MM-dd HH:mm:ss:SSS");  
 }

 /\**  
  * 获取本月第一天的起始时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @return 本月第一天的起始时间  
  \*/  
 public static Date getThisMonthFirstDay() {  
  Calendar cl = Calendar.getInstance();  
  cl.set(Calendar.DAY\_OF\_MONTH, 1);  
  String date = getFormatDate(cl.getTime(), "yyyy-MM-dd 00:00:00");  
  return getDate(date, "yyyy-MM-dd HH:mm:ss");  
 }

 /\**  
  * 获取本月最后一天的起始时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @return 本月最后一天的起始时间  
  \*/  
 public static Date getThisMonthLastDay() {  
  Calendar cl = Calendar.getInstance();  
  cl.add(Calendar.MONTH, 1);  
  cl.set(Calendar.DATE, 0);  
  String date = getFormatDate(cl.getTime(), "yyyy-MM-dd 23:59:59:999");  
  return getDate(date, "yyyy-MM-dd HH:mm:ss:SSS");  
 }

 /\**  
  * 获取指定日期当月第一天的起始时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @param now 日期参数  
  * @return 当月第一天的起始时间  
  \*/  
 public static Date getMonthFirstDay(Date now) {  
  Calendar cl = Calendar.getInstance();  
  cl.setTime(now);  
  cl.set(Calendar.DAY\_OF\_MONTH, 1);  
  String date = getFormatDate(cl.getTime(), "yyyy-MM-dd 00:00:00");  
  return getDate(date, "yyyy-MM-dd HH:mm:ss");  
 }

 /\**  
  * 获取指定日期当月最后一天的结束时间  
  * @author cui  
  * @date Jun 26, 2009  
  * @param now 日期参数  
  * @return 当月最后一天的结束时间  
  \*/  
 public static Date getMonthLastDay(Date now) {  
  Calendar cl = Calendar.getInstance();  
  cl.setTime(now);  
  cl.add(Calendar.MONTH, 1);  
  cl.set(Calendar.DATE, 0);  
  String date = getFormatDate(cl.getTime(), "yyyy-MM-dd 23:59:59:999");  
  return getDate(date, "yyyy-MM-dd HH:mm:ss:SSS");  
 }

 /\**  
  * 获取给定年月的最后一天  
  * @author cui  
  * @date Jun 26, 2009  
  * @param year 年份  
  * @param month 月份  
  * @return 给定年月的最后一天  
  \*/  
 public static int getMonthOfLastDay(int year, int month) {  
  Calendar calendar = Calendar.getInstance();  
  calendar.set(Calendar.YEAR, year);  
  calendar.set(Calendar.MONTH, month - 1);  
  return calendar.getActualMaximum(Calendar.DAY\_OF\_MONTH);

 }

 /\**  
  * 获取给定年月的第一天  
  * @author cui  
  * @date Jun 26, 2009  
  * @param year 年份  
  * @param month 月份  
  * @return 给定年月的第一天  
  \*/  
 public static int getMonthOfFirstDay(int year, int month) {  
  Calendar calendar = Calendar.getInstance();  
  calendar.set(Calendar.YEAR, year);  
  calendar.set(Calendar.MONTH, month - 1);  
  return calendar.getActualMinimum(Calendar.DAY\_OF\_MONTH);  
 }  
   
 /\**  
  * 判断一个日期是否在两个日期之间  
  * @author 滕辉  
  * @param startDate 开始日期  
  * @param endDate 结束日期  
  * @param date 指定日期  
  * @return  
  \*/  
 public static boolean between(Date startDate, Date endDate, Date date){  
  if(startDate == null && endDate == null){  
   return true;  
  }else if(startDate != null && startDate.getTime() &lt;= date.getTime() && endDate == null){  
   return true;  
  }else if(endDate != null && endDate.getTime() &gt;= date.getTime() && startDate == null){  
   return true;  
  }else if(startDate != null && endDate != null && startDate.getTime() &lt;= date.getTime() && endDate.getTime() &gt;= date.getTime()){  
   return true;  
  }else{  
   return false;  
  }  
 }  
 /\**  
  * 获得指定日期的前一天的数据  
  * @author caoxian  
  * @param dateStr  
  * @return  
  \*/  
 public static Date getBeforeToday(String dateStr) {  
  Calendar calendar = Calendar.getInstance();  
     int day = calendar.get(Calendar.DAY\_OF\_YEAR);  
     calendar.set(Calendar.DAY\_OF\_YEAR, day - 1);  
     dateStr = getFormatDate(calendar.getTime(), "yyyy-MM-dd 23:59:59:999");  
     return getDate(dateStr, "yyyy-MM-dd HH:mm:ss");  
 }  
 /\**  
  * 获得指定日期的前30天的数据  
  * @author caoxian  
  * @param dateStr  
  * @return  
  \*/  
 public static Date getBeforeThrityTody(String dateStr){  
  Calendar calendar = Calendar.getInstance();  
     int day = calendar.get(Calendar.DAY\_OF\_YEAR);  
     calendar.set(Calendar.DAY\_OF\_YEAR, day - 30);  
     dateStr = getFormatDate(calendar.getTime(), "yyyy-MM-dd 00:00:00");  
     return getDate(dateStr, "yyyy-MM-dd HH:mm:ss");  
 }  
 

}