开发中经常用到的日期工具类整合
package com.etouch.utils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateUtil {
private static Logger log = LoggerFactory.getLogger(DateUtil.class);
public static String getInterfaceCurrentTimestamp() {
String time_stamp = "";
try {
time_stamp = DateUtil.getStringFromDate(new Date(), "yyyyMMddHHmmss");
} catch (Exception e) {
e.printStackTrace();
}
return time_stamp;
}
public static Timestamp getCurrentTimestamp() {
Timestamp t = null;
try {
t = date2TimeStamp(getCurrentDate());
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
public static Date getCurrentDate() {
Date date = new Date();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = ((Date) sdf.parseObject(sdf.format(new Date())));
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static String getCurrentStringDate() {
return getStringFromDate(getCurrentDate());
}
public static String getStringFromDate(Date date) {
return format(date, "yyyy-MM-dd HH:mm:ss");
}
public static String getStringFromDate(String date) {
if (date == null) {
return "";
}
Date d = getDateFromString(date, "yyyy-MM-dd HH:mm:ss");
return format(d, "yyyy-MM-dd HH:mm:ss");
}
public static String getStringFromDate(Date date, String format) {
return format(date, format);
}
public static String format(Date date, String format) {
String result = "";
try {
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(format);
result = df.format(date);
}
} catch (Exception e) {
}
return result;
}
public static Date getDatetimeFromString(String dateStr) {
return getDateFromString(dateStr, "yyyy-MM-dd HH:mm:ss");
}
public static Date getTimeFromString(String dateStr) {
return getDateFromString(dateStr, "HH:mm");
}
public static Date getDateFromString(String dateStr) {
return getDateFromString(dateStr, "yyyy-MM-dd");
}
public static Date getDateTimeFromString(String dateStr) {
return getDateFromString(dateStr, "yyyy-MM-dd HH:mm:ss");
}
public static Date strToDate(String dateTime, Date curDate) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
dateTime = sdf.format(curDate) + " " + dateTime;
Date date = null;
if (dateTime != null && !dateTime.trim().isEmpty()) {
try {
date = simpleDateFormat.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
}
return date;
}
public static Date getDateFromString(String dateStr, String format) {
if (StringUtils.isBlank(dateStr)) {
return null;
}
Date date = new Date();
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static Date addDate(Date date, int year, int month, int day, int hour, int minute, int second) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(c.YEAR, year);
c.add(c.MONTH, month);
c.add(c.DAY_OF_MONTH, day);
c.add(c.HOUR, hour);
c.add(c.MINUTE, minute);
c.add(c.SECOND, second);
Date temp_date = c.getTime();
return temp_date;
}
public static Timestamp date2TimeStamp(Date date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp ts = new Timestamp(((Date) sdf.parseObject(sdf.format(date))).getTime());
return ts;
}
public static int getDaysBetween2Date(String date1, String date2) throws ParseException {
int days = 0;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
long to = df.parse(date2).getTime();
long from = df.parse(date1).getTime();
days = (int) ((to - from) / (1000 * 60 * 60 * 24));
return days;
}
public static Date getBefore(Date date, Integer n) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - n);
return calendar.getTime();
}
public static Date getAfter(Date date, Integer n) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + n);
return calendar.getTime();
}
public static int getDaysBetween2Date(Date date1, Date date2) throws ParseException {
String d1 = DateUtil.getStringFromDate(date1, "yyyy-MM-dd");
String d2 = DateUtil.getStringFromDate(date2, "yyyy-MM-dd");
return getDaysBetween2Date(d1, d2);
}
public static double getHoursBetween2Date(Date date1, Date date2) {
double hours = 0;
hours = (double) (Double.parseDouble((date2.getTime() - date1.getTime()) + "") / (1000 * 60 * 60));
return hours;
}
public static int getMinutesBetween2Date(Date date1, Date date2) throws ParseException {
int minutes = 0;
if (date2.getTime() < date1.getTime()) {
return 0;
}
minutes = (int) ((date2.getTime() - date1.getTime()) / (1000 * 60));
return minutes;
}
public static int getYear(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.YEAR);
}
public static int getMonth(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.MONTH) + 1;
}
public static int getDay(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.DAY_OF_MONTH);
}
public static int getHour(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.HOUR_OF_DAY);
}
public static int getMinute(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.MINUTE);
}
public static int getSecond(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.SECOND);
}
public static long getMillis(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
public static synchronized Calendar getLastDayOfMonth(Calendar gc) {
switch (gc.get(Calendar.MONTH)) {
case 0:
gc.set(Calendar.DAY_OF_MONTH, 31);
break;
case 1:
gc.set(Calendar.DAY_OF_MONTH, 28);
break;
case 2:
gc.set(Calendar.DAY_OF_MONTH, 31);
break;
case 3:
gc.set(Calendar.DAY_OF_MONTH, 30);
break;
case 4:
gc.set(Calendar.DAY_OF_MONTH, 31);
break;
case 5:
gc.set(Calendar.DAY_OF_MONTH, 30);
break;
case 6:
gc.set(Calendar.DAY_OF_MONTH, 31);
break;
case 7:
gc.set(Calendar.DAY_OF_MONTH, 31);
break;
case 8:
gc.set(Calendar.DAY_OF_MONTH, 30);
break;
case 9:
gc.set(Calendar.DAY_OF_MONTH, 31);
break;
case 10:
gc.set(Calendar.DAY_OF_MONTH, 30);
break;
case 11:
gc.set(Calendar.DAY_OF_MONTH, 31);
break;
}
if ((gc.get(Calendar.MONTH) == Calendar.FEBRUARY)
&& (isLeapYear(gc.get(Calendar.YEAR)))) {
gc.set(Calendar.DAY_OF_MONTH, 29);
}
return gc;
}
public static synchronized Date getFirstDayOfMonth(Date date) {
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
gc.set(Calendar.DAY_OF_MONTH, 1);
return gc.getTime();
}
public static synchronized String toOraString(Date theDate, boolean hasTime) {
DateFormat theFormat;
if (hasTime) {
theFormat = getOraDateTimeFormat();
} else {
theFormat = getOraDateFormat();
}
return toString(theDate, theFormat);
}
public static synchronized String toString(Date theDate, boolean hasTime) {
DateFormat theFormat;
if (hasTime) {
theFormat = getDateTimeFormat();
} else {
theFormat = getDateFormat();
}
return toString(theDate, theFormat);
}
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy");
public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("MM/dd/yyyy HH:mm");
public static final SimpleDateFormat DATE_TIME_EXTENDED_FORMAT = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
private static final SimpleDateFormat ORA_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
public static final SimpleDateFormat ORA_DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");
public static final SimpleDateFormat ORA_DATE_TIME_EXTENDED_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
public static synchronized DateFormat getDateFormat() {
SimpleDateFormat theDateFormat = (SimpleDateFormat)
DATE_FORMAT.clone();
theDateFormat.setLenient(false);
return theDateFormat;
}
public static synchronized DateFormat getDateTimeFormat() {
SimpleDateFormat theDateTimeFormat = (SimpleDateFormat) DATE_TIME_FORMAT
.clone();
theDateTimeFormat.setLenient(false);
return theDateTimeFormat;
}
public static synchronized DateFormat getOraDateFormat() {
SimpleDateFormat theDateFormat = (SimpleDateFormat) ORA_DATE_FORMAT
.clone();
theDateFormat.setLenient(false);
return theDateFormat;
}
public static synchronized DateFormat getOraDateTimeFormat() {
SimpleDateFormat theDateTimeFormat = (SimpleDateFormat)
ORA_DATE_TIME_FORMAT.clone();
theDateTimeFormat.setLenient(false);
return theDateTimeFormat;
}
public static synchronized String toString(Date theDate,
DateFormat theDateFormat) {
if (theDate == null)
return "";
return theDateFormat.format(theDate);
}
public static synchronized boolean isLeapYear() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
return isLeapYear(year);
}
public static synchronized boolean isLeapYear(int year) {
if ((year % 400) == 0)
return true;
else if ((year % 4) == 0) {
if ((year % 100) == 0)
return false;
else return true;
} else return false;
}
public static synchronized boolean isLeapYear(Date date) {
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
int year = gc.get(Calendar.YEAR);
return isLeapYear(year);
}
public static synchronized boolean isLeapYear(Calendar gc) {
int year = gc.get(Calendar.YEAR);
return isLeapYear(year);
}
public static synchronized Date getNextMonth(Date date) {
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
gc.add(Calendar.MONTH, 1);
return gc.getTime();
}
public static synchronized Date getNextDay(Date date) {
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
gc.add(Calendar.DATE, 1);
return gc.getTime();
}
public static synchronized Date getNextWeek(Date date) {
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
gc.add(Calendar.DATE, 7);
return gc.getTime();
}
public static synchronized boolean compare2Date(String date1, String date2) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = sdf.parse(date1);
Date d2 = sdf.parse(date2);
if (d2.getTime() - d1.getTime() > 0) {
return true;
} else {
return false;
}
}
public static synchronized boolean compare2Date(Date date1, Date date2) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = sdf.parse(sdf.format(date1));
Date d2 = sdf.parse(sdf.format(date2));
if (d2.getTime() - d1.getTime() > 0) {
return true;
} else {
return false;
}
}
public static synchronized boolean compare2DateNoHMS(String date1, String date2) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = sdf.parse(date1);
Date d2 = sdf.parse(date2);
if (d2.getTime() - d1.getTime() > 0) {
return true;
} else {
return false;
}
}
public static synchronized boolean compare2DateNoHMS(Date date1, Date date2) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = sdf.parse(sdf.format(date1));
Date d2 = sdf.parse(sdf.format(date2));
if (d2.getTime() - d1.getTime() > 0) {
return true;
} else {
return false;
}
}
public static synchronized Date getOtherDay(Date date, int n) {
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
gc.add(Calendar.DATE, n);
return gc.getTime();
}
public static int getWeekOfDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w <= 0)
w = 7;
return w;
}
public static String getUpWeekOfDate(Date date) {
int i = getWeekOfDate(date);
switch (i) {
case 1:
return "一";
case 2:
return "二";
case 3:
return "三";
case 4:
return "四";
case 5:
return "五";
case 6:
return "六";
case 7:
return "七";
default:
return "error";
}
}
public static String getTimeAllFromString(Date createDate) throws ParseException {
SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss");
return sd.format(createDate);
}
}
获取指定日期几天后的时间
public static Date getDateAfter(Date date,Integer day) {
Calendar now = Calendar.getInstance();
now.setTime(date);
now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
return now.getTime();
}