package com.yxb.base.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
public class DateUtil extends DateUtils {
public static String[] parsePatterns = new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"};
public DateUtil() {
}
public static String getDate() {
return getDate("yyyy-MM-dd");
}
public static String getDate(String pattern) {
return DateFormatUtils.format(new Date(), pattern);
}
public static String formatDate(Date date, Object... pattern) {
String formatDate = null;
if (pattern != null && pattern.length > 0) {
formatDate = DateFormatUtils.format(date, pattern[0].toString());
} else {
formatDate = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
}
return formatDate;
}
public static String formatDateString(String date, Object... pattern) {
String formatDate = null;
try {
if (pattern != null && pattern.length > 0) {
formatDate = DateFormatUtils.format(formatDate(date, "yyyy-MM-dd HH:mm:ss"), pattern[0].toString());
} else {
formatDate = DateFormatUtils.format(formatDate(date, "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd");
}
return formatDate;
} catch (Exception var4) {
return formatDate;
}
}
public static Date formatDate(String date, String format) {
try {
if (StringUtil.isEmpty(format)) {
format = "yyyy-MM-dd HH:mm:ss";
}
return (new SimpleDateFormat(format)).parse(date);
} catch (Exception var3) {
var3.printStackTrace();
return new Date();
}
}
public static String formatDateTime3(long timestemp) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date(timestemp));
}
public static String getTime() {
return formatDate(new Date(), "HH:mm:ss");
}
public static String getDateTime() {
return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
}
public static String getDateTime2() {
return formatDate(new Date(), "yyyyMMddHHmmss");
}
public static String getYear() {
return formatDate(new Date(), "yyyy");
}
public static String getMonth() {
return formatDate(new Date(), "MM");
}
public static String getMonth(Date date) {
return formatDate(date, "MM");
}
public static String getMonth(String datestr) {
return formatDate(parseDate(datestr), "MM");
}
public static String getDay() {
return formatDate(new Date(), "dd");
}
public static String getStringWeek() {
return formatDate(new Date(), "E");
}
public static int getIntWeek(Date date) {
Calendar cal = Calendar.getInstance();
if (date == null) {
cal.setTime(new Date(System.currentTimeMillis()));
} else {
cal.setTime(date);
}
int tempWeek = cal.get(7);
return tempWeek == 1 ? 7 : tempWeek - 1;
}
public static Date parseDate(Object str) {
if (str == null) {
return null;
} else {
try {
return parseDate(str.toString(), parsePatterns);
} catch (ParseException var2) {
return null;
}
}
}
public static long pastDays(Date date) {
long t = (new Date()).getTime() - date.getTime();
return t / 86400000L;
}
public static String[] getMonthDayArray(String datestr) {
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM");
Calendar rightNow = Calendar.getInstance();
try {
rightNow.setTime(simpleDate.parse(datestr.substring(0, 4) + "-" + datestr.substring(4, 6)));
} catch (ParseException var6) {
var6.printStackTrace();
}
int day = rightNow.getActualMaximum(5);
String[] days = new String[day];
for(int i = 0; i < day; ++i) {
if (i < 10) {
if (i == 9) {
days[i] = datestr.substring(4, 6) + "-" + (i + 1);
} else {
days[i] = datestr.substring(4, 6) + "-0" + (i + 1);
}
} else {
days[i] = datestr.substring(4, 6) + "-" + (i + 1);
}
}
return days;
}
public static int getMonthDays(String datestr) {
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM");
Calendar rightNow = Calendar.getInstance();
try {
rightNow.setTime(simpleDate.parse(datestr));
} catch (ParseException var4) {
var4.printStackTrace();
}
return rightNow.getActualMaximum(5);
}
public static Date getDateStart(Date date) {
Date datetemp = date;
if (date == null) {
return null;
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
datetemp = sdf.parse(formatDate(date, "yyyy-MM-dd") + " 00:00:00");
} catch (ParseException var4) {
var4.printStackTrace();
}
return datetemp;
}
}
public static Date getDateEnd(Date date) {
Date datetemp = date;
if (date == null) {
return null;
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
datetemp = sdf.parse(formatDate(date, "yyyy-MM-dd") + " 23:59:59");
} catch (ParseException var4) {
var4.printStackTrace();
}
return datetemp;
}
}
public static Long getStringTimeDifference(String before, String after) {
return getTimeDifference(parseDate(before), parseDate(after));
}
public static Long getTimeDifference(Date before, Date after) {
return (after.getTime() - before.getTime()) / 1000L;
}
public static int daysBetween(Date start, Date end) {
Calendar calst = Calendar.getInstance();
Calendar caled = Calendar.getInstance();
calst.setTime(start);
caled.setTime(end);
calst.set(11, 0);
calst.set(12, 0);
calst.set(13, 0);
caled.set(11, 0);
caled.set(12, 0);
caled.set(13, 0);
return ((int)(caled.getTime().getTime() / 1000L) - (int)(calst.getTime().getTime() / 1000L)) / 3600 / 24;
}
public static int daysBetween(String before, String after) {
Date beforeDate = new Date();
Date afterDate = new Date();
DateFormat df = DateFormat.getDateInstance();
try {
beforeDate = df.parse(before);
afterDate = df.parse(after);
} catch (ParseException var7) {
var7.printStackTrace();
}
Calendar calst = Calendar.getInstance();
Calendar caled = Calendar.getInstance();
calst.setTime(beforeDate);
caled.setTime(afterDate);
calst.set(11, 0);
calst.set(12, 0);
calst.set(13, 0);
caled.set(11, 0);
caled.set(12, 0);
caled.set(13, 0);
return ((int)(caled.getTime().getTime() / 1000L) - (int)(calst.getTime().getTime() / 1000L)) / 3600 / 24;
}
public static int dayBetween(String smdate, String bdate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / 86400000L;
return Integer.parseInt(String.valueOf(between_days));
}
public static long getTodaySurplusTime() {
Date nowDate = new Date();
return (getDateEnd(nowDate).getTime() - nowDate.getTime()) / 1000L;
}
public static long dateDiff(String startTime, String endTime, String format, String str) {
SimpleDateFormat sd = new SimpleDateFormat(format);
long nd = 86400000L;
long nh = 3600000L;
long nm = 60000L;
long ns = 1000L;
long day = 0L;
long hour = 0L;
long min = 0L;
long sec = 0L;
try {
long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
day = diff / nd;
hour = diff % nd / nh + day * 24L;
min = diff % nd % nh / nm + day * 24L * 60L;
sec = diff % nd % nh % nm / ns;
return str.equalsIgnoreCase("h") ? hour : min;
} catch (ParseException var24) {
var24.printStackTrace();
return str.equalsIgnoreCase("h") ? hour : min;
}
}
public static String getFormatDateString(String datestr) {
return datestr.substring(0, 4) + "-" + datestr.substring(4, 6) + "-" + datestr.substring(6, 8) + " " + datestr.substring(8, 10) + ":" + datestr.substring(10, 12) + ":" + datestr.substring(12, 14);
}
public static boolean compare(Date oldDate, Date newDate) {
return oldDate.getTime() > newDate.getTime();
}
public static boolean isSameDay(String sDate1, String sDate2) {
sDate1 = sDate1.substring(0, 10);
sDate2 = sDate2.substring(0, 10);
return sDate1.equals(sDate2);
}
public static String getStringBeforeDay(Integer day) {
return formatDate(getBeforeDay(new Date(), day), "yyyy-MM-dd");
}
public static String getStringBeforeDay(Date currentDate, Integer day) {
return formatDate(getBeforeDay(currentDate, day), "yyyy-MM-dd");
}
public static Date getBeforeDay(Date currentDate, Integer day) {
return day == null ? null : getBeforeDateTime(currentDate, day * 24 * 60);
}
public static String getStringBeforeDateTime(Date currentDate, Integer min) {
return formatDate(getBeforeDateTime(currentDate, min), "yyyy-MM-dd HH:mm:ss");
}
public static Date getBeforeDateTime(Date currentDate, Integer min) {
if (currentDate != null && min != null) {
Long current = currentDate.getTime();
return new Date(current - Long.parseLong(min + "") * 60L * 1000L);
} else {
return null;
}
}
public static long getLastTime() {
long milliSecondsLeftToday = 86400000L - DateUtils.getFragmentInMilliseconds(Calendar.getInstance(), 5);
return milliSecondsLeftToday == 0L ? 0L : milliSecondsLeftToday / 1000L;
}
public static String getFrontMonth(int month) {
Calendar cal = Calendar.getInstance();
cal.add(2, -month);
return formatDate(cal.getTime(), "yyyy-MM");
}
public static String getBeforeHour(int hour) {
Calendar cal = Calendar.getInstance();
cal.add(10, -hour);
return formatDate(cal.getTime(), "yyyy-MM-dd HH:mm:ss");
}
public static String getBeforeHour(Date nowDate, int hour) {
Calendar cal = Calendar.getInstance();
cal.setTime(nowDate);
cal.add(10, -hour);
return formatDate(cal.getTime(), "yyyy-MM-dd HH:mm:ss");
}
public static String[] getStartToEndHour(String startTime, String endTime) {
Date startDate = parseDate(startTime);
Date endDate = parseDate(endTime);
return getStartToEndHour(startDate, endDate);
}
public static String[] getStartToEndHour(Date startTime, Date endTime) {
long time = endTime.getTime() - startTime.getTime();
int hour = (int)(time / 1000L / 60L / 60L);
String[] hours = new String[hour];
for(int i = 0; i < hour; ++i) {
hours[i] = getBeforeHour(startTime, -i);
}
return hours;
}
public static String[] getBeforeDayToNowDay(int day) {
String[] days = new String[day + 1];
for(int i = day; i >= 0; --i) {
days[day - i] = getStringBeforeDay(i);
}
return days;
}
public static String[] getStartToNowDay(String startTime) {
Date startDate = parseDate(startTime);
long time = (new Date()).getTime() - startDate.getTime();
int day = (int)(time / 1000L / 60L / 60L / 24L);
String[] days = new String[day];
for(int i = 0; i < day; ++i) {
days[i] = getStringBeforeDay(startDate, -i);
}
return days;
}
public static String[] getNowBeforeMonth(int month) {
String[] months = new String[month + 1];
Calendar cal = null;
for(int i = 0; i <= month; ++i) {
cal = Calendar.getInstance();
cal.add(2, -(month - i));
months[i] = formatDate(cal.getTime(), "yyyy-MM");
}
return months;
}
public static String[] getBeforeMonthToAMounthBefore(int month) {
String[] months = new String[month];
Calendar cal = null;
for(int i = 0; i < month; ++i) {
cal = Calendar.getInstance();
cal.add(2, -(month - i));
months[i] = formatDate(cal.getTime(), "yyyy-MM");
}
return months;
}
public static String getBeforeMonth(int month) {
Calendar cal = Calendar.getInstance();
cal.add(2, -month);
return formatDate(cal.getTime(), "yyyy-MM");
}
public static String getLastDayOfMonth(String month) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
Date date = null;
try {
date = dateFormat.parse(month);
} catch (Exception var5) {
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(2, 1);
calendar.add(5, -1);
Date voucherDate = calendar.getTime();
return formatDate(voucherDate, "yyyy-MM-dd");
}
}