import org.springframework.util.StringUtils;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import static org.apache.logging.log4j.util.Strings.isBlank;
public class DateUtil {
public static final TimeZone tz = TimeZone.getTimeZone("GMT+8:00");
public static final SimpleDateFormat MAX_FULL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
public static final ThreadLocal<SimpleDateFormat> FULL = new ThreadLocal<SimpleDateFormat>();
public static final ThreadLocal<SimpleDateFormat> NORMAL = new ThreadLocal<SimpleDateFormat>();
public static ThreadLocal<SimpleDateFormat> YEAR = new ThreadLocal<SimpleDateFormat>();
public static final SimpleDateFormat FULL_HOUR = new SimpleDateFormat("HH:mm:ss");
public static ThreadLocal<SimpleDateFormat> SIMPLE_HOUR = new ThreadLocal<SimpleDateFormat>();
public static ThreadLocal<SimpleDateFormat> MONTH = new ThreadLocal<SimpleDateFormat>();
public static final SimpleDateFormat MONTH_DAY = new SimpleDateFormat("MM月dd日");
public static ThreadLocal<SimpleDateFormat> T_MONTH_DAY = new ThreadLocal<SimpleDateFormat>();
public static ThreadLocal<SimpleDateFormat> YEAR_MONTH = new ThreadLocal<SimpleDateFormat>();
public static final SimpleDateFormat MONTH_DAY_HOUR = new SimpleDateFormat("MM-dd HH:mm");
public static final int SHOW_TYPE_SIMPLE = 0;
public static final int SHOW_TYPE_COMPLEX = 1;
public static final int SHOW_TYPE_ALL = 2;
public static final int SHOW_TYPE_CALL_LOG = 3;
public static final int SHOW_TYPE_CALL_DETAIL = 4;
public static final long ONESECOND = 1000;
public static final long ONEMINUTE = 60000;
public static final long TENMINUTE = 600000;
public static final long ONEHOUR = 3600000;
public static final long TWOHOUR = 7200000;
public static final long ONEDAY = 86400000;
public static final long TWELVEHOUR = 43200000;
/**
* 定义线程变量
*
* @return
*/
public static SimpleDateFormat getYEAR() {
if (YEAR.get() == null) {
YEAR.set(new SimpleDateFormat("yyyy-MM-dd"));
}
return YEAR.get();
}
public static SimpleDateFormat getMONTH() {
if (MONTH.get() == null) {
MONTH.set(new SimpleDateFormat("yyyy-MM"));
}
return MONTH.get();
}
public static SimpleDateFormat getMONTH_DAY() {
if (T_MONTH_DAY.get() == null) {
T_MONTH_DAY.set(new SimpleDateFormat("MM-dd"));
}
return T_MONTH_DAY.get();
}
public static SimpleDateFormat getYEAR_MONTH() {
if (YEAR_MONTH.get() == null) {
YEAR_MONTH.set(new SimpleDateFormat("yyyy年MM月"));
}
return YEAR_MONTH.get();
}
public static SimpleDateFormat getFULL() {
if (FULL.get() == null) {
FULL.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
}
return FULL.get();
}
public static SimpleDateFormat getSIMPLE_HOUR() {
if (SIMPLE_HOUR.get() == null) {
SIMPLE_HOUR.set(new SimpleDateFormat("HH:mm"));
}
return SIMPLE_HOUR.get();
}
public static SimpleDateFormat getNORMAL() {
if (NORMAL.get() == null) {
NORMAL.set(new SimpleDateFormat("yyyy-MM-dd HH:mm"));
}
return NORMAL.get();
}
/**
* 获取当前日期 (yyyy-MM-dd HH:mm:ss)
*
* @return
*/
public static String getTodayFullDate() {
return getFULL().format(new Date());
}
public static String getMonthDate() {
Date curDate = new Date(System.currentTimeMillis());
return getMONTH().format(curDate);
}
/**
* 验证日期字符串是否正确
*
* @param str
* @param format
* @return
*/
public static boolean isValidDate(String str, SimpleDateFormat format) {
boolean convertSuccess = true;
try {
format.setLenient(false);
format.parse(str);
} catch (ParseException e) {
convertSuccess = false;
}
return convertSuccess;
}
public static long getTime(String date, SimpleDateFormat format) {
try {
return (format.parse(date)).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
public static Date getNowDate() {
Date curDate = new Date(System.currentTimeMillis());
return curDate;
}
public static Date getOffLineDate() {
Calendar nowTime = Calendar.getInstance();
nowTime.add(Calendar.MINUTE, -1);
return nowTime.getTime();
}
public static Date getPreDateByDay(int dayNumber) {
Calendar nowTime = Calendar.getInstance();
nowTime.add(Calendar.DAY_OF_MONTH, -dayNumber);
return nowTime.getTime();
}
/**
* 获取几天后的当前时间
*
* @param dayNumber
* @return
*/
public static Date getNextDateByDay(int dayNumber) {
Calendar nowTime = Calendar.getInstance();
nowTime.add(Calendar.DAY_OF_MONTH, dayNumber);
return nowTime.getTime();
}
/**
* 获取几个月后的当前时间
*
* @param number
* @return
*/
public static Date getAfterMonth(int number) {
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, number);
return c.getTime();
}
public static Date getAfterMonth(Date date, int number) {
Calendar c = new GregorianCalendar();
c.setTime(date);
c.add(Calendar.MONTH, number);
return c.getTime();
}
public static Date getAfterMinute(int number) {
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, number);
return c.getTime();
}
public static Date getPreMinute(int number) {
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, -number);
return c.getTime();
}
/**
* 将日期格式化
*
* @param dateL 日期格式yyyy-MM-dd HH:mm:ss
* @return
*/
public static String formatDateTime(String dateL, SimpleDateFormat sorFormat, SimpleDateFormat desFormat) {
String result = "";
if (isBlank(dateL)) {
return result;
}
Date date = parseDate(dateL, sorFormat);
result = desFormat.format(date);
return result;
}
public static String formatDateTime(Date dateTime, SimpleDateFormat desFormat) {
String result = "";
result = desFormat.format(dateTime);
return result;
}
/**
* 将日期字符串转成日期
*
* @param strDate 字符串日期
* @return java.util.date日期类型
*/
public static Date parseDate(String strDate, SimpleDateFormat format) {
DateFormat dateFormat = format;
Date returnDate = null;
try {
returnDate = dateFormat.parse(strDate);
} catch (ParseException e) {
}
return returnDate;
}
/**
* 获取指定日期 (yyyy-MM-dd HH:mm:ss.SSSSSS)
*
* @return
*/
public static String getMaxFullDate(long msgTime) {
Date data = new Date(msgTime);
return MAX_FULL.format(data);
}
public static long getMaxFullDate(String time) {
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(MAX_FULL.parse(time));
return calendar.getTimeInMillis();
} catch (ParseException e) {
System.out.println(e.getMessage());
}
return 0;
}
/**
* 获取指定日期 (yyyy-MM-dd HH:mm:ss)
*
* @param msgTime
* @return
*/
public static String getFullDate(long msgTime) {
Date data = new Date(msgTime);
return getFULL().format(data);
}
public static long getFullDate(String time) {
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(getFULL().parse(time));
return calendar.getTimeInMillis();
} catch (ParseException e) {
System.out.println(e.getMessage());
}
return 0;
}
public static String getCurrentTime(SimpleDateFormat df) {
if (df == null) {
return "";
}
Date curDate = new Date(System.currentTimeMillis());
return df.format(curDate);
}
public static String getCurrentTime() {
Date curDate = new Date(System.currentTimeMillis());
return getYEAR().format(curDate);
}
public static long getCurrentLongTime() {
Date curDate = new Date(System.currentTimeMillis());
String formatDate = getYEAR().format(curDate);
try {
return (getYEAR().parse(formatDate)).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
/*时间戳转换成字符窜*/
public static String getYear(long time) {
Date d = new Date(time);
return getYEAR().format(d);
}
/*时间戳转换成字符窜*/
public static String getHMS(long time) {
Date d = new Date(time * 1000);
return FULL_HOUR.format(d);
}
/*时间戳转换成字符窜*/
public static String getHM(long time) {
Date d = new Date(time * 1000);
return SIMPLE_HOUR.get().format(d);
}
public static String getYMD(long timetemp) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timetemp * 1000);
String time = getYEAR().format(timetemp * 1000);
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
time += " 星期日";
} else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
time += " 星期六";
} else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
time += " 星期五";
} else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
time += " 星期四";
} else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {
time += " 星期三";
} else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
time += " 星期二";
} else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
time += " 星期一";
}
return time;
}
/**
* 判断当前日期是星期几
*
* @param pTime 设置的需要判断的时间 //格式如2012-09-08
* @return dayForWeek 判断结果
* @Exception 发生异常
*/
public static String getWeek(String pTime) {
String Week = "";
Calendar c = Calendar.getInstance();
try {
c.setTime(getFULL().parse(pTime));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
Week += "天";
}
if (c.get(Calendar.DAY_OF_WEEK) == 2) {
Week += "一";
}
if (c.get(Calendar.DAY_OF_WEEK) == 3) {
Week += "二";
}
if (c.get(Calendar.DAY_OF_WEEK) == 4) {
Week += "三";
}
if (c.get(Calendar.DAY_OF_WEEK) == 5) {
Week += "四";
}
if (c.get(Calendar.DAY_OF_WEEK) == 6) {
Week += "五";
}
if (c.get(Calendar.DAY_OF_WEEK) == 7) {
Week += "六";
}
if (StringUtils.isEmpty(Week)) {
return "";
}
return "星期" + Week;
}
/**
* 标胶两个时间字符串的大小
*
* @param DATE1
* @param DATE2
* @return
*/
public static int CompareDate(String DATE1, String DATE2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(getTodayFullDate());
if (DATE2 != null && !DATE2.contains("false")) {
dt2 = df.parse(DATE2);
}
if (dt1.getTime() > dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
/**
* 标胶两个时间字符串的大小
*
* @param DATE1
* @param DATE2
* @return
*/
public static int CompareDate(String DATE1, String DATE2, DateFormat df) {
if (df == null) {
return 0;
}
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(getTodayFullDate());
if (DATE2 != null && !DATE2.contains("false")) {
dt2 = df.parse(DATE2);
}
if (dt1.getTime() > dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
/**
* 计算当前日期
*
* @return
*/
public static int[] getCurrentDate() {
Calendar calendar = Calendar.getInstance();
return new int[]{calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH)};
}
public static String generateTime(long time) {
int totalSeconds = (int) (time / 1000);
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds) : String.format("%02d:%02d", minutes, seconds);
}
public static String getDateString(long time, int type) {
Calendar c = Calendar.getInstance();
c = Calendar.getInstance(tz);
c.setTimeInMillis(time);
long currentTime = System.currentTimeMillis();
Calendar current_c = Calendar.getInstance();
current_c = Calendar.getInstance(tz);
current_c.setTimeInMillis(currentTime);
int currentYear = current_c.get(Calendar.YEAR);
int currentHour = current_c.get(Calendar.HOUR_OF_DAY);
int currentMinute = current_c.get(Calendar.MINUTE);
int currentSecond = current_c.get(Calendar.SECOND);
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH) + 1;
int d = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
long diffTime = currentTime - time;
long diffDay = currentTime - getCurrentLongTime();
String dateStr = "";
if (diffTime < diffDay && diffTime > 0) {
if (type == SHOW_TYPE_SIMPLE) {
dateStr = (hour < 10 ? "0" + hour : hour) + ":"
+ (minute < 10 ? "0" + minute : minute);
} else if (type == SHOW_TYPE_COMPLEX) {
dateStr = "今天 " + (hour < 10 ? "0" + hour : hour) + ":"
+ (minute < 10 ? "0" + minute : minute);
} else if (type == SHOW_TYPE_CALL_LOG) {
int op = 0;
if ((hour + 1) == currentHour) {//小于1小时
int compare = (currentMinute + (60 - minute));
if (compare >= 60) {
dateStr = "今天 " + (hour < 10 ? "0" + hour : hour) + ":"
+ (minute < 10 ? "0" + minute : minute);
} else {
op = currentMinute + (60 - minute);
if (op <= 0) {
dateStr = "刚刚";
} else {
dateStr = op + "分钟前";
}
}
} else if (hour == currentHour) {//小于1分钟
if (minute + 1 == currentMinute || minute == currentMinute) {
dateStr = "刚刚";
} else {
op = currentMinute - minute;
if (op <= 0) {
dateStr = "刚刚";
} else {
dateStr = op + "分钟前";
}
}
} else {
dateStr = "今天 " + (hour < 10 ? "0" + hour : hour) + ":"
+ (minute < 10 ? "0" + minute : minute);
}
} else if (type == SHOW_TYPE_CALL_DETAIL) {
dateStr = "今天 ";
} else {
dateStr = (hour < 10 ? "0" + hour : hour) + ":"
+ (minute < 10 ? "0" + minute : minute) + ":"
+ (second < 10 ? "0" + second : second);
}
} else if (diffTime < (diffDay + ONEDAY) && diffTime > 0) {
if (type == SHOW_TYPE_SIMPLE || type == SHOW_TYPE_CALL_DETAIL) {
dateStr = "昨天 ";
} else if (type == SHOW_TYPE_COMPLEX) {
dateStr = "昨天 " + (hour < 10 ? "0" + hour : hour) + ":"
+ (minute < 10 ? "0" + minute : minute);
} else if (type == SHOW_TYPE_CALL_LOG) {
dateStr = "昨天 " + (hour < 10 ? "0" + hour : hour) + ":"
+ (minute < 10 ? "0" + minute : minute);
} else {
dateStr = "昨天 " + (hour < 10 ? "0" + hour : hour) + ":"
+ (minute < 10 ? "0" + minute : minute) + ":"
+ (second < 10 ? "0" + second : second);
}
} else if (y == currentYear) {
if (type == SHOW_TYPE_SIMPLE) {
dateStr = (m < 10 ? "0" + m : m) + "/" + (d < 10 ? "0" + d : d);
} else if (type == SHOW_TYPE_COMPLEX) {
dateStr = (m < 10 ? "0" + m : m) + "月" + (d < 10 ? "0" + d : d)
+ "日";
} else if (type == SHOW_TYPE_CALL_LOG || type == SHOW_TYPE_COMPLEX) {
dateStr = (m < 10 ? "0" + m : m) + /* 月 */"/"
+ (d < 10 ? "0" + d : d) + /* 日 */" "
+ (hour < 10 ? "0" + hour : hour) + ":"
+ (minute < 10 ? "0" + minute : minute);
} else if (type == SHOW_TYPE_CALL_DETAIL) {
dateStr = y + "/" + (m < 10 ? "0" + m : m) + "/"
+ (d < 10 ? "0" + d : d);
} else {
dateStr = (m < 10 ? "0" + m : m) + "月" + (d < 10 ? "0" + d : d)
+ "日 " + (hour < 10 ? "0" + hour : hour) + ":"
+ (minute < 10 ? "0" + minute : minute) + ":"
+ (second < 10 ? "0" + second : second);
}
} else {
if (type == SHOW_TYPE_SIMPLE) {
dateStr = y + "/" + (m < 10 ? "0" + m : m) + "/"
+ (d < 10 ? "0" + d : d);
} else if (type == SHOW_TYPE_COMPLEX) {
dateStr = y + "年" + (m < 10 ? "0" + m : m) + "月"
+ (d < 10 ? "0" + d : d) + "日";
} else if (type == SHOW_TYPE_CALL_LOG || type == SHOW_TYPE_COMPLEX) {
dateStr = y + /* 年 */"/" + (m < 10 ? "0" + m : m) + /* 月 */"/"
+ (d < 10 ? "0" + d : d) + /* 日 */" "/*
* + (hour < 10
* ? "0" + hour
* : hour) + ":"
* + (minute <
* 10 ? "0" +
* minute :
* minute)
*/;
} else if (type == SHOW_TYPE_CALL_DETAIL) {
dateStr = y + "/" + (m < 10 ? "0" + m : m) + "/"
+ (d < 10 ? "0" + d : d);
} else {
dateStr = y + "年" + (m < 10 ? "0" + m : m) + "月"
+ (d < 10 ? "0" + d : d) + "日 "
+ (hour < 10 ? "0" + hour : hour) + ":"
+ (minute < 10 ? "0" + minute : minute) + ":"
+ (second < 10 ? "0" + second : second);
}
}
return dateStr;
}
public static String getBSJDateString(long time, int type) {
//原时间
Calendar c = Calendar.getInstance();
c = Calendar.getInstance(tz);
c.setTimeInMillis(time);
//当前时间
long currentTime = System.currentTimeMillis();
Calendar current_c = Calendar.getInstance();
current_c = Calendar.getInstance(tz);
current_c.setTimeInMillis(currentTime);
int currentYear = current_c.get(Calendar.YEAR);
int currentMonth = current_c.get(Calendar.MONTH) + 1;
int currentDay = current_c.get(Calendar.DAY_OF_MONTH);
int currentHour = current_c.get(Calendar.HOUR_OF_DAY);
int currentMinute = current_c.get(Calendar.MINUTE);
int currentSecond = current_c.get(Calendar.SECOND);
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH) + 1;
int d = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
long diffTime = currentTime - time;
long diffDay = currentTime - getCurrentDayTime();
String dateStr = "";
switch (type) {
/*
一个分钟内 显示 多少秒前
一个小时内 显示 多少分钟前
一天之内 显示 多少小时前
15天之内 显示 多少天前
之后显示年月日 时分秒
*/
case SHOW_TYPE_ALL:
if (diffTime < 0) {
dateStr = "刚刚发布";
} else if (diffTime < ONEMINUTE) { //一分钟内
dateStr = diffTime / ONESECOND + "秒前";
} else if (ONEMINUTE < diffTime && diffTime < ONEHOUR) {
dateStr = diffTime / ONEMINUTE + "分钟前";
} else if (ONEHOUR < diffTime && diffTime < ONEDAY) {
dateStr = diffTime / ONEHOUR + "小时前";
} else if (ONEDAY < diffTime && diffTime < 15 * ONEDAY) {
dateStr = (diffTime / ONEDAY) + "天前";
} else {
dateStr = getStrTime(time, getFULL());
}
break;
default:
break;
}
return dateStr;
}
public static String getDodgemDateString(Date date, int type) {
if (date == null) {
return "很久前";
}
//原时间
Calendar c = Calendar.getInstance();
c = Calendar.getInstance(tz);
c.setTimeInMillis(date.getTime());
//当前时间
long currentTime = System.currentTimeMillis();
Calendar current_c = Calendar.getInstance();
current_c = Calendar.getInstance(tz);
current_c.setTimeInMillis(currentTime);
int currentYear = current_c.get(Calendar.YEAR);
int currentMonth = current_c.get(Calendar.MONTH) + 1;
int currentDay = current_c.get(Calendar.DAY_OF_MONTH);
int currentHour = current_c.get(Calendar.HOUR_OF_DAY);
int currentMinute = current_c.get(Calendar.MINUTE);
int currentSecond = current_c.get(Calendar.SECOND);
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH) + 1;
int d = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
long diffTime = currentTime - date.getTime();
long diffDay = currentTime - getCurrentDayTime();
String dateStr = "";
switch (type) {
/*
一个分钟内 显示 多少秒前
一个小时内 显示 多少分钟前
一天之内 显示 多少小时前
15天之内 显示 多少天前
之后显示年月日 时分秒
*/
case SHOW_TYPE_ALL:
if (diffTime < 0) {
dateStr = "刚刚";
} else if (diffTime < ONEMINUTE) { //一分钟内
dateStr = diffTime / ONESECOND + "秒前";
} else if (ONEMINUTE < diffTime && diffTime < ONEHOUR) {
dateStr = diffTime / ONEMINUTE + "分钟前";
} else if (ONEHOUR < diffTime && diffTime < ONEDAY) {
dateStr = diffTime / ONEHOUR + "小时前";
} else if (ONEDAY < diffTime && diffTime < 30 * ONEDAY) {
dateStr = (diffTime / ONEDAY) + "天前";
} else {
dateStr = "很久前";
}
break;
default:
dateStr = getStrTime(date.getTime(), getFULL());
break;
}
return dateStr;
}
public static String getStrTime(long date, SimpleDateFormat format) {
String re_StrTime = "";
re_StrTime = format.format(new Date(date));
return re_StrTime;
}
public static long getCurrentDayTime() {
Date d = new Date(System.currentTimeMillis());
String formatDate = getYEAR().format(d);
try {
return (getYEAR().parse(formatDate)).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
public static boolean isEqualMonth(long l1, long r1) {
try {
Date l1D = new Date(l1);
String lMonth = getYEAR().format(l1D);
Date r1D = new Date(r1);
String rMonth = getYEAR().format(r1D);
if (lMonth.equals(rMonth)) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
//获取当前小时的开始时间
public static Date getCurrentHourBegin() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
//获取当前小时的结束时间
public static Date getCurrentHourEnd() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 999);
return cal.getTime();
}
//获取当天的开始时间
public static Date getDayBegin() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
//获取当天的结束时间
public static Date getDayEnd() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 999);
return cal.getTime();
}
//获取之前n天的开始时间
public static Date getBeginDayOfPre(Date selDate, int preNum) {
Calendar cal = new GregorianCalendar();
cal.setTime(selDate);
cal.add(Calendar.DAY_OF_MONTH, -preNum);
return getStartTimeOfDay(cal.getTime());
}
//获取之前n天的结束时间
public static Date getEndDayOfPre(Date selDate, int preNum) {
Calendar cal = new GregorianCalendar();
cal.setTime(selDate);
cal.add(Calendar.DAY_OF_MONTH, -preNum);
return getEndTimeOfDay(cal.getTime());
}
//获取之后n天的开始时间
public static Date getBeginDayOfNext(Date selDate, int nextNum) {
Calendar cal = new GregorianCalendar();
cal.setTime(selDate);
cal.add(Calendar.DAY_OF_MONTH, nextNum);
return getStartTimeOfDay(cal.getTime());
}
//获取之后n天的结束时间
public static Date getEndDayOfNext(Date selDate, int nextNum) {
Calendar cal = new GregorianCalendar();
cal.setTime(selDate);
cal.add(Calendar.DAY_OF_MONTH, nextNum);
return getEndTimeOfDay(cal.getTime());
}
//获取之前n天的开始时间
public static Date getBeginDayOfPre(int preNum) {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, -preNum);
return cal.getTime();
}
//获取之后n天的开始时间
public static Date getBeginDayOfNext(int nextNum) {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, nextNum);
return cal.getTime();
}
//获取之后n天的结束时间
public static Date getEndDayOfNext(int nextNum) {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayEnd());
cal.add(Calendar.DAY_OF_MONTH, nextNum);
return cal.getTime();
}
//获取昨天的开始时间
public static Date getBeginDayOfYesterday() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
//获取明天的结束时间
public static Date getEndDayOfYesterDay() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayEnd());
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
//获取明天的开始时间
public static Date getBeginDayOfTomorrow() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
//获取明天的结束时间
public static Date getEndDayOfTomorrow() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayEnd());
cal.add(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
//获取本周的开始时间
@SuppressWarnings("unused")
public static Date getBeginDayOfWeek() {
Date date = new Date();
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek);
return getDayStartTime(cal.getTime());
}
//获取本周的结束时间
public static Date getEndDayOfWeek() {
Calendar cal = Calendar.getInstance();
cal.setTime(getBeginDayOfWeek());
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return getDayEndTime(weekEndSta);
}
//获取上周的开始时间
@SuppressWarnings("unused")
public static Date getBeginDayOfLastWeek() {
Date date = new Date();
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek - 7);
return getDayStartTime(cal.getTime());
}
//获取上周的结束时间
public static Date getEndDayOfLastWeek() {
Calendar cal = Calendar.getInstance();
cal.setTime(getBeginDayOfLastWeek());
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return getDayEndTime(weekEndSta);
}
//获取本月的开始时间
public static Date getBeginDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
return getDayStartTime(calendar.getTime());
}
//获取本月的结束时间
public static Date getEndDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(), getNowMonth() - 1, day);
return getDayEndTime(calendar.getTime());
}
/**
* @param date 2019-08
* @return
*/
//获取指定月的开始时间
public static Date getBeginDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.set(getDateYear(date), getDateMonth(date) - 1, 1);
return getDayStartTime(calendar.getTime());
}
//获取指定月的结束时间
public static Date getEndDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.set(getDateYear(date), getDateMonth(date) - 1, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getDateYear(date), getDateMonth(date) - 1, day);
return getDayEndTime(calendar.getTime());
}
public static Date getBeginPreDayOfMonth(int preNum) {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
calendar.add(Calendar.MONTH, -preNum);
return getDayStartTime(calendar.getTime());
}
public static Date getBeginNextDayOfMonth(int nextNum) {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
calendar.add(Calendar.MONTH, nextNum);
return getDayStartTime(calendar.getTime());
}
//获取上月的开始时间
public static Date getBeginDayOfLastMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 2, 1);
return getDayStartTime(calendar.getTime());
}
//获取上月的结束时间
public static Date getEndDayOfLastMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 2, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(), getNowMonth() - 2, day);
return getDayEndTime(calendar.getTime());
}
//获取本年的开始时间
public static Date getBeginDayOfYear() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, getNowYear());
// cal.set
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DATE, 1);
return getDayStartTime(cal.getTime());
}
//获取本年的结束时间
public static Date getEndDayOfYear() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, getNowYear());
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.DATE, 31);
return getDayEndTime(cal.getTime());
}
//获取某个日期的开始时间
public static Timestamp getDayStartTime(Date d) {
Calendar calendar = Calendar.getInstance();
if (null != d) {
calendar.setTime(d);
}
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return new Timestamp(calendar.getTimeInMillis());
}
public static Date getDayStartTime(String startTime) throws ParseException {
Date d = getYEAR().parse(startTime);
Calendar calendar = Calendar.getInstance();
if (null != d) {
calendar.setTime(d);
}
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
//获取某个日期的结束时间
public static Timestamp getDayEndTime(Date d) {
Calendar calendar = Calendar.getInstance();
if (null != d) {
calendar.setTime(d);
}
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return new Timestamp(calendar.getTimeInMillis());
}
public static Date getDayEndTime(String endTime) throws ParseException {
return getDayEndTime(endTime, true);
}
public static Date getDayEndTime(String endTime, boolean isHaveMS) throws ParseException {
Date d = getYEAR().parse(endTime);
Calendar calendar = Calendar.getInstance();
if (null != d) {
calendar.setTime(d);
}
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
if (isHaveMS) {
calendar.set(Calendar.MILLISECOND, 999);
}
return calendar.getTime();
}
//获取今年是哪一年
public static Integer getNowYear() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return Integer.valueOf(gc.get(Calendar.YEAR));
}
//获取本月是哪一月
public static int getNowMonth() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return gc.get(Calendar.MONTH) + 1;
}
//获取当天是本月的哪一天
public static int getNowDay() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return gc.get(Calendar.DAY_OF_MONTH);
}
public static Integer getDateYear(Date date) {
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return Integer.valueOf(gc.get(Calendar.YEAR));
}
//获取本月是哪一月
public static int getDateMonth(Date date) {
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return gc.get(Calendar.MONTH) + 1;
}
//获取当天是本月的哪一天
public static int getDateDay(Date date) {
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return gc.get(Calendar.DAY_OF_MONTH);
}
//两个日期相减得到的天数
public static int getDiffDays(Date beginDate, Date endDate) {
if (beginDate == null || endDate == null) {
throw new IllegalArgumentException("getDiffDays param is null!");
}
long diff = (endDate.getTime() - beginDate.getTime())
/ (1000 * 60 * 60 * 24);
int days = new Long(diff).intValue();
return days;
}
public static void main(String[] args) {
Date time=new Date();
System.out.println(time);
Date endtime=getBeginDayOfPre(5);
System.out.println(endtime);
System.out.println(getDiffDays(time,endtime));
}
//两个日期相减得到的毫秒数
public static long dateDiff(Date beginDate, Date endDate) {
long date1ms = beginDate.getTime();
long date2ms = endDate.getTime();
return date2ms - date1ms;
}
//获取两个日期中的最大日期
public static Date max(Date beginDate, Date endDate) {
if (beginDate == null) {
return endDate;
}
if (endDate == null) {
return beginDate;
}
if (beginDate.after(endDate)) {
return beginDate;
}
return endDate;
}
//获取两个日期中的最小日期
public static Date min(Date beginDate, Date endDate) {
if (beginDate == null) {
return endDate;
}
if (endDate == null) {
return beginDate;
}
if (beginDate.after(endDate)) {
return endDate;
}
return beginDate;
}
//返回某月该季度的第一个月
public static Date getFirstSeasonDate(Date date) {
final int[] SEASON = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int sean = SEASON[cal.get(Calendar.MONTH)];
cal.set(Calendar.MONTH, sean * 3 - 3);
return cal.getTime();
}
//返回某个日期下几天的日期
public static Date getNextDay(Date date, int i) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
return cal.getTime();
}
//返回某个日期前几天的日期
public static Date getFrontDay(Date date, int i) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
return cal.getTime();
}
//获取某年某月到某年某月按天的切片日期集合(间隔天数的集合)
@SuppressWarnings({"rawtypes", "unchecked"})
public static List getTimeList(int beginYear, int beginMonth, int endYear,
int endMonth, int k) {
List list = new ArrayList();
if (beginYear == endYear) {
for (int j = beginMonth; j <= endMonth; j++) {
list.add(getTimeList(beginYear, j, k));
}
} else {
{
for (int j = beginMonth; j < 12; j++) {
list.add(getTimeList(beginYear, j, k));
}
for (int i = beginYear + 1; i < endYear; i++) {
for (int j = 0; j < 12; j++) {
list.add(getTimeList(i, j, k));
}
}
for (int j = 0; j <= endMonth; j++) {
list.add(getTimeList(endYear, j, k));
}
}
}
return list;
}
//获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)
@SuppressWarnings({"unchecked", "rawtypes"})
public static List getTimeList(int beginYear, int beginMonth, int k) {
List list = new ArrayList();
Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
int max = begincal.getActualMaximum(Calendar.DATE);
for (int i = 1; i < max; i = i + k) {
list.add(begincal.getTime());
begincal.add(Calendar.DATE, k);
}
begincal = new GregorianCalendar(beginYear, beginMonth, max);
list.add(begincal.getTime());
return list;
}
/**
* 获取 指定时间 段内 按天分片
*
* @param beginYear 开始年
* @param beginMonth 开始月
* @param beginDay 开始日
* @param endYear 结束年
* @param endMonth 结束月
* @param endDay 结束日
* @param k 分片间隔
* @return
*/
public static List<Date> getDayList(int beginYear, int beginMonth, int beginDay,
int endYear, int endMonth, int endDay, int k) {
List<Date> list = new ArrayList();
Calendar begincal = new GregorianCalendar(beginYear, beginMonth, beginDay);
Calendar endcal = new GregorianCalendar(endYear, endMonth, endDay);
if (!begincal.before(endcal)) {
//开始时间大于结束时间 直接返回
return list;
}
//是不是同一年
if (beginYear == endYear) {
for (int j = beginMonth; j <= endMonth; j++) {
if (j == beginMonth) {
int startDay = beginDay;
int tempEndDay = 1;
if (j == endMonth) {
tempEndDay = endDay;
} else {
tempEndDay = begincal.getActualMaximum(Calendar.DATE);
}
list.addAll(getOneMonthDayList(beginYear, j, startDay, tempEndDay, k));
} else {
int startDay = 1;
int tempEndDay = 1;
if (j == endMonth) {
tempEndDay = endDay;
} else {
Calendar currentCal = new GregorianCalendar(beginYear, j - 1, 1);
tempEndDay = currentCal.getActualMaximum(Calendar.DATE);
}
list.addAll(getOneMonthDayList(beginYear, j, startDay, tempEndDay, k));
}
}
} else {
for (int j = beginMonth; j < 12; j++) {
if (j == beginMonth) {
int startDay = beginDay;
int tempEndDay = begincal.getActualMaximum(Calendar.DATE);
list.addAll(getOneMonthDayList(beginYear, j, startDay, tempEndDay, k));
} else {
int startDay = 1;
Calendar currentCal = new GregorianCalendar(beginYear, j, 1);
int tempEndDay = currentCal.getActualMaximum(Calendar.DATE);
list.addAll(getOneMonthDayList(beginYear, j, startDay, tempEndDay, k));
}
}
for (int i = beginYear + 1; i < endYear; i++) {
for (int j = 1; j <= 12; j++) {
int startDay = 1;
Calendar currentCal = new GregorianCalendar(i, j, 1);
int tempEndDay = currentCal.getActualMaximum(Calendar.DATE);
list.addAll(getOneMonthDayList(i, j, startDay, tempEndDay, k));
}
}
for (int j = 0; j <= endMonth; j++) {
if (j == endMonth) {
int startDay = 1;
int tempEndDay = endDay;
list.addAll(getOneMonthDayList(endYear, j, startDay, tempEndDay, k));
} else {
int startDay = 1;
Calendar currentCal = new GregorianCalendar(endYear, j, 1);
int tempEndDay = currentCal.getActualMaximum(Calendar.DATE);
list.addAll(getOneMonthDayList(endYear, j, startDay, tempEndDay, k));
}
}
}
return list;
}
public static List<Date> getOneMonthDayList(int year, int month, int beginDay, int endDay, int k) {
List<Date> list = new ArrayList();
Calendar begincal = new GregorianCalendar(year, month, beginDay);
int max = begincal.getActualMaximum(Calendar.DATE);
if (endDay > max) { //本月最大天数
endDay = max;
}
for (int i = beginDay; i < endDay; i = i + k) {
list.add(begincal.getTime());
begincal.add(Calendar.DATE, k);
}
list.add(begincal.getTime());
return list;
}
/**
* 获取 指定 时间段内 按月分片
*
* @param beginYear 开始年
* @param beginMonth 开始月
* @param endYear 结束年
* @param endMonth 结束月
* @return
*/
public static List<Date> getMonthList(int beginYear, int beginMonth,
int endYear, int endMonth) {
List<Date> list = new ArrayList();
Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
Calendar endcal = new GregorianCalendar(endYear, endMonth, 1);
if (!begincal.before(endcal)) {
//开始时间大于结束时间 直接返回
return list;
}
if (beginYear == endYear) {
//同一年
for (int i = beginMonth; i <= endMonth; i++) {
list.add(begincal.getTime());
begincal.add(Calendar.MONTH, 1);
}
} else {
for (int i = beginMonth; i < 12; i++) {
list.add(begincal.getTime());
begincal.add(Calendar.MONTH, 1);
}
for (int i = beginYear + 1; i < endYear; i++) {
Calendar currentCal = new GregorianCalendar(i, 0, 1);
for (int j = 0; j < 12; j++) {
list.add(currentCal.getTime());
currentCal.add(Calendar.MONTH, 1);
}
}
Calendar currentCal = new GregorianCalendar(endYear, 0, 1);
for (int j = 0; j <= endMonth; j++) {
list.add(currentCal.getTime());
currentCal.add(Calendar.MONTH, 1);
}
}
return list;
}
public static Date getStartTimeOfDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static Date getEndTimeOfDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
// calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTime();
}
/**
* 获取红包过期描述字段
*
* @param date 过期时间
* @param type 显示方式
* @return
*/
public static String getRedPacketExpireDateString(Date date, int type) {
if (date == null) {
return "永久有效";
}
//原时间
Calendar c = Calendar.getInstance();
c = Calendar.getInstance(tz);
c.setTimeInMillis(date.getTime());
//当前时间
long currentTime = System.currentTimeMillis();
long diffTime = date.getTime() - currentTime;
//long diffDay = currentTime - getCurrentDayTime();
String dateStr = "";
switch (type) {
/*
一个分钟内 显示 多少秒前
一个小时内 显示 多少分钟前
一天之内 显示 多少小时前
15天之内 显示 多少天前
之后显示年月日 时分秒
*/
case SHOW_TYPE_ALL:
if (diffTime < 0) {
dateStr = "已过期";
} else if (diffTime < ONEMINUTE) { //一分钟内
dateStr = diffTime / ONESECOND + "秒后过期";
} else if (ONEMINUTE < diffTime && diffTime < ONEHOUR) {
dateStr = diffTime / ONEMINUTE + "分钟后过期";
} else if (ONEHOUR < diffTime && diffTime < ONEDAY) {
dateStr = diffTime / ONEHOUR + "小时后过期";
} else if (ONEDAY < diffTime && diffTime < 30 * ONEDAY) {
dateStr = (diffTime / ONEDAY) + "天后过期";
} else {
dateStr = "很久之后过期";
}
break;
default:
dateStr = getStrTime(date.getTime(), getFULL());
break;
}
return dateStr;
}
public static String getQrCodeExpireDateString(Date date, int type) {
if (date == null) {
return "永久有效";
}
//原时间
Calendar c = Calendar.getInstance();
c = Calendar.getInstance(tz);
c.setTimeInMillis(date.getTime());
//当前时间
long currentTime = System.currentTimeMillis();
long diffTime = date.getTime() - currentTime;
//long diffDay = currentTime - getCurrentDayTime();
String dateStr = "";
switch (type) {
/*
一个分钟内 显示 多少秒前
一个小时内 显示 多少分钟前
一天之内 显示 多少小时前
15天之内 显示 多少天前
之后显示年月日 时分秒
*/
case SHOW_TYPE_ALL:
if (diffTime < 0) {
dateStr = "已过期";
} else if (ONEMINUTE < diffTime && diffTime < ONEHOUR) {
dateStr = "即将过期";
} else if (ONEHOUR < diffTime && diffTime < TWELVEHOUR) {
dateStr = diffTime / ONEHOUR + "小时后过期";
}
break;
default:
dateStr = getStrTime(date.getTime(), getFULL());
break;
}
return dateStr;
}
public static int getQrCodeExpireState(Date date, int type) {
//原时间
Calendar c = Calendar.getInstance();
c = Calendar.getInstance(tz);
c.setTimeInMillis(date.getTime());
//当前时间
long currentTime = System.currentTimeMillis();
long diffTime = date.getTime() - currentTime;
//long diffDay = currentTime - getCurrentDayTime();
int dateStr = 2;
switch (type) {
/*
一个分钟内 显示 多少秒前
一个小时内 显示 多少分钟前
一天之内 显示 多少小时前
15天之内 显示 多少天前
之后显示年月日 时分秒
*/
case SHOW_TYPE_ALL:
if (diffTime < 0) {
dateStr = 0;
} else if (ONEMINUTE < diffTime && diffTime < ONEHOUR) {
dateStr = 1;
} else if (ONEHOUR < diffTime && diffTime < TWELVEHOUR) {
dateStr = 1;
}
break;
default:
dateStr = 2;
break;
}
return dateStr;
}
/**
* 得到指定日期的一天的的最后时刻23:59:59
*
* @param date
* @return
*/
public static Date getFinallyTime(String date) {
date += " 23:59:59";
try {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
} catch (Exception e) {
return null;
}
}
/**
* 得到指定日期的一天的开始时刻00:00:00
*
* @param date
* @return
*/
public static Date getStartTiem(String date) {
date += " 00:00:00";
try {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
} catch (Exception e) {
return null;
}
}
/**
* 判断开始时间是否 小(早)于结束时间
*
* @param startTime
* @param endTime
* @return
*/
public static boolean isBefore(Date startTime, Date endTime) {
return startTime.before(endTime);
}
/**
* 获取当前日期是星期几<br>
*
* @param dt
* @return 当前日期是星期几
*/
public static String getWeekOfDate(Date dt) {
// String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0) {
w = 0;
}
return weekDays[w];
}
public static String getNowDat2String() {
String strDateFormet = "yyyyMMddHHmmss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strDateFormet);
String format = simpleDateFormat.format(getNowDate());
return format;
}
最全的时间工具类
最新推荐文章于 2023-06-13 18:13:32 发布