DateUtil 工具类

package com.htoou.util;


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


import com.htoou.log.Logger;


/**
 * @author 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 MIDDLE_DATE_PATTERN = "yyyy-MM-dd HH:mm";


public final static String CHINESE_DATE_PATTERN = "yyyy年MM月dd日 HH时mm分ss秒";


public final static String DATE_PATTERN = "yyyyMMddHHmmss";


private final static Logger logger = Logger.getLogger(DateUtil.class);


/**

*/
public DateUtil() {
}


public static Date dealNull(Date date, Date defaultValue) {
if (date == null) {
return defaultValue;
}
return date;
}


public static boolean isEmpty(Date date) {
return date == null;
}


public static boolean isNull(Date date) {
return isEmpty(date);
}


public static int getInt(Date date, String strFormat) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.YEAR);


}


public static String getFormatDate(Timestamp timeStamp) {
return format(timeStamp, CHINESE_DATE_PATTERN);
}


public static String getFormatDate(java.sql.Date date) {
return format(date, CHINESE_DATE_PATTERN);
}


public static String getTodayDate(String format) {
return format(new Date(), format);
}


public static String getTodayDay() {
return format(new Date(), SHORT_DATE_PATTERN);
}


public static String getYesterDay() {
return format(addDateByDay(new Date(), -1), SHORT_DATE_PATTERN);
}


public static String getDayByDate(Date date) {
return format(date, SHORT_DATE_PATTERN);
}


public static Date getDay(Date date) {
String strDate = format(date, SHORT_DATE_PATTERN);
if (!StringUtil.isEmpty(strDate)) {
return getDate(strDate, SHORT_DATE_PATTERN);
}
return null;
}


public static String getFormatDate(Date date) {
return format(date, CHINESE_DATE_PATTERN);
}


public static String getDefaultDate(Date date) {
return format(date, DEFAULT_DATE_PATTERN);
}


public static String getFormatDate(java.sql.Date date, String strFormat) {
return format(date, strFormat);
}


public static String format(Date date, String strFormat) {
String strRet = null;
if (date == null) {
return strRet = StringUtil.BLANK;
}
SimpleDateFormat simple = new SimpleDateFormat(strFormat);
if (simple != null) {
strRet = simple.format(date);
}
return strRet;
}


public static String toString(java.sql.Date date, String strFormat) {
return getFormatDate(date, strFormat);
}


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;
}


public static Date getDateByTime(Date date, String time, String timeFormat) {
String strRet = null;
Date rtnDate = null;
SimpleDateFormat simple = new SimpleDateFormat(SHORT_DATE_PATTERN);
if (simple != null) {
strRet = simple.format(date);
}
simple = new SimpleDateFormat(SHORT_DATE_PATTERN + StringUtil.BLANK_SPACE + timeFormat);
if (simple != null) {
try {
rtnDate = simple.parse(strRet + StringUtil.BLANK_SPACE + time);
} catch (ParseException e) {
}
}
return rtnDate;
}


public static Date getBeginDate(Date date) {
if (!StringUtil.isEmpty(date)) {
String beginDate = DateUtil.getDayByDate(date) + " 00:00:00";
return DateUtil.getDate(beginDate, "yyyy-MM-dd HH:mm:ss");
}
return null;
}


public static Date getBeginDate(String date) {
if (!StringUtil.isEmpty(date)) {
String beginDate = date + " 00:00:00";
return DateUtil.getDate(beginDate, "yyyy-MM-dd HH:mm:ss");
}
return null;
}


public static Date getEndDate(Date date) {
if (!StringUtil.isEmpty(date)) {
String beginDate = DateUtil.getDayByDate(date) + " 23:59:59:999";
return DateUtil.getDate(beginDate, "yyyy-MM-dd HH:mm:ss:SSS");
}
return null;
}


public static Date getEndDate(String date) {
if (!StringUtil.isEmpty(date)) {
String beginDate = date + " 23:59:59:999";
return DateUtil.getDate(beginDate, "yyyy-MM-dd HH:mm:ss:SSS");
}
return null;
}


public static Date getStartCurrentDay() {
String date = getFormatDate(new Date(), "yyyy-MM-dd 00:00:00");
return getDate(date, "yyyy-MM-dd HH:mm:ss");
}


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");
}


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");
}


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");
}


public static Date getNextDay() {
String date = getFormatDate(addDateByDay(new Date(), 1), "yyyy-MM-dd 00:00:00");
return getDate(date, "yyyy-MM-dd HH:mm:ss");
}


public static Date getWeekFirstDay() {
Calendar c = Calendar.getInstance();
c.setTime(DateUtil.getDay(c.getTime()));
c.set(Calendar.DAY_OF_WEEK, 1);
return c.getTime();
}


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");
}


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");
}


public static Date getDate(Date cdate, String strFormat) {
String date = getFormatDate(cdate, strFormat);
return getDate(date, strFormat);
}


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;
}


public static String getFormatDate(Timestamp timeStamp, String strFormat) {
String strRet = null;
if (timeStamp == null)
return strRet = StringUtil.BLANK;
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;
}


public static String yyMMdd() {


SimpleDateFormat simple = new SimpleDateFormat("yyMMdd");
Date date = new Date();
return simple.format(date);


}


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);
}


/**
* 是否同一年

* @param date1
* @param date2
* @return
*/
public static boolean isSameYear(Date date1, Date date2) {
if (date1 == null || date2 == null) {
return false;
}
Calendar c = Calendar.getInstance();
int y1, y2;
c.setTime(date1);
y1 = c.get(Calendar.YEAR);
c.setTime(date2);
y2 = c.get(Calendar.YEAR);
return y1 == y2;
}


/**
* add one get first day next month

* @param currentDate
* @return
*/
public static Date getNextMonthDay() {
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 1);
c.set(Calendar.DAY_OF_MONTH, 1);
return c.getTime();
}


/**
* add one get first day next month

* @param currentDate
* @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();
}


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();
}


/**
* add one get first day next month

* @param currentDate
* @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();
}


/**
* add one get first day next month

* @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();
}


/**
* is same month

* @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;
}


/**
* is same month

* @param date
* @return
*/
public static boolean isSameYear(Date date) {
if (date == null) {
return false;
}
Calendar c = Calendar.getInstance();
int m1 = c.get(Calendar.YEAR);
c.setTime(date);
int m2 = c.get(Calendar.YEAR);
return m1 == m2;
}


public static int getWeekDay() {
int weekday;
Calendar tmp = Calendar.getInstance();
weekday = tmp.get(Calendar.DAY_OF_WEEK) - 1;
return weekday;
}


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;
}


public static String strTime() {
String strRet = null;
Date date = new Date();
SimpleDateFormat simple = new SimpleDateFormat("HH:mm:ss");
if (simple != null) {
strRet = simple.format(date);
}
return strRet;
}


public static String strDate() {
String strDate = null;
Date date = new Date();
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
if (simple != null) {
strDate = simple.format(date);
}
return strDate;


}


public static Date parseDate(String time, String strFormat, String dateFlag) {
Date date = null;
if (time == null || time.equals("") || dateFlag.equals("")) {
return date = null;
} else {
if (!StringUtil.isEmpty(dateFlag) && dateFlag.equals("start"))
time = StringUtil.dealNull(time) + " 00:00:00";
if (!StringUtil.isEmpty(dateFlag) && dateFlag.equals("end"))
time = StringUtil.dealNull(time) + " 23:59:59";
}
try {
SimpleDateFormat simple = new SimpleDateFormat(strFormat);
date = simple.parse(time);
} catch (Exception e) {
logger.error("Get Date Error!" + e.getMessage(), e);
}
return date;
}


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;
}


/**
* get date HH:mm:ss

* @param date
* @param strHMSFormat
* @return
*/
public static Date getDateByHMS(Date date, String strHMSFormat) {
try {
String time = getFormatDate(date, SHORT_DATE_PATTERN + " " + strHMSFormat);
SimpleDateFormat simple = new SimpleDateFormat(FULL_DATE_PATTERN);
date = simple.parse(time);
} catch (Exception e) {
logger.error("Get Date Error!" + e.getMessage(), e);
}
return date;
}


/**
* set date by HH:mm:ss

* @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();
}


/**
* absolute date

* @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();
}


/**
* srcoll date to other day

* @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();
}


public static Date getDateByHMS(String strHMSFormat) {
return getDateByHMS(new Date(), strHMSFormat);
}


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);
Date date = simple.parse(time);
tsmp = new Timestamp(date.getTime());
} catch (Exception e) {
logger.error("Get Timestamp Error!" + e.getMessage(), e);
}
return tsmp;
}


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();
}


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();
}


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();
}


/**
* 去时分秒

* @param date
* @param add_day
* @return
*/
public static Date getRollDay(Date date, int add_day) {
return getDay(addDateByDay(date, add_day));
}

/**
* 获得Unix时间
* @return
*/
public static long getUnixTime(){
return System.currentTimeMillis()/1000;
}


/**
* 带时分秒

* @param date
* @param add_day
* @return
*/
public static Date getRollDate(Date date, int add_day) {
return addDateByDay(date, add_day);
}


/**
* 获得两个日期之间的秒差

* @param b
* @param e
* @return
*/
public static long between(Date b, Date e) {
if (b == null || e == null) {
return 0;
}
return (e.getTime() - b.getTime()) / 1000;
}


/**
* 获得两个日期之间的分钟

* @param b
* @param e
* @return
*/
public static long betweenForMinute(Date b, Date e) {
return between(b, e) / 60;
}


/**
* 获得两个日期之间的小时

* @param b
* @param e
* @return
*/
public static long betweenForHour(Date b, Date e) {
return betweenForMinute(b, e) / 60;
}


/**
* 获得两个日期之间的日差

* @param b
* @param e
* @return
*/
public static long betweenForDay(Date b, Date e) {
return betweenForHour(b, e) / 24;
}


/**
* 获得两个日期之间的月差

* @param b
* @param e
* @return
*/
public static int betweenForMonth(Date b, Date e) {
Calendar cal = Calendar.getInstance();
cal.setTime(b);
int month1 = cal.get(Calendar.MONTH);
int year1 = cal.get(Calendar.YEAR);


cal.setTime(e);
int month2 = cal.get(Calendar.MONTH);
int yea2 = cal.get(Calendar.YEAR);


return (month2 - month1) + (yea2 - year1) * 12;
}


/**
* 获得两个日期之间的年差

* @param b
* @param e
* @return
*/
public static int betweenForYear(Date b, Date e) {
Calendar cal = Calendar.getInstance();
cal.setTime(b);
int year1 = cal.get(Calendar.YEAR);


cal.setTime(e);
int yea2 = cal.get(Calendar.YEAR);


return yea2 - year1;
}


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();
}


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();
}


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();
}


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();
}


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");
}


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");
}


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");
}


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");
}


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");
}


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");
}


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");
}


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");
}


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);


}


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);
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值