import java
.sql
.Timestamp
;
import java
.text
.DateFormat
;
import java
.text
.ParseException
;
import java
.text
.SimpleDateFormat
;
import java
.time
.LocalDate
;
import java
.time
.temporal
.ChronoUnit
;
import java
.util
.*
;
public class DateUtil {
public static final String DAFAULT_DATE_FORMAT
= "yyyy-M-d";
public static final String DATE_FORMAT
= "yyyy-MM-dd";
public static final String DAFAULT_DATETIME_FORMAT
= "yyyy-M-d HH:mm:ss";
public static final String DATETIME_FORMAT
= "yyyy-MM-dd HH:mm:ss";
public static final String DAFAULT_TIME_FORMAT
= "HH:mm:ss";
public static final long DAFAULT_MS
= 86400000;
public static String
format(Calendar c
, String pattern
) {
Calendar calendar
;
if (c
!= null
) {
calendar
= c
;
} else {
calendar
= Calendar
.getInstance();
}
if (pattern
== null
|| pattern
.equals("")) {
pattern
= DATETIME_FORMAT
;
}
SimpleDateFormat sdf
= new SimpleDateFormat(pattern
);
return sdf
.format(calendar
.getTime());
}
public static String
format(Date date
, String pattern
) {
Date tempDate
= null
;
if (date
!= null
) {
tempDate
= date
;
} else {
tempDate
= Calendar
.getInstance().getTime();
}
if (pattern
== null
|| pattern
.equals("")) {
pattern
= DATETIME_FORMAT
;
}
SimpleDateFormat sdf
= new SimpleDateFormat(pattern
);
return sdf
.format(tempDate
);
}
public static String
getDataFormat(Date date
, String
... patterns
) {
if (patterns
.length
== 0) {
SimpleDateFormat sdf
= new SimpleDateFormat(DATETIME_FORMAT
);
return sdf
.format(date
);
} else {
SimpleDateFormat sdf
= new SimpleDateFormat(patterns
[0]);
return sdf
.format(date
);
}
}
public static Date
getParseDate(String date
, String
... patterns
) throws ParseException
{
if (patterns
.length
== 0) {
SimpleDateFormat sdf
= new SimpleDateFormat(DATETIME_FORMAT
);
return sdf
.parse(date
);
} else {
SimpleDateFormat sdf
= new SimpleDateFormat(patterns
[0]);
return sdf
.parse(date
);
}
}
public static Date
getLastDateSpan(Date date
, int dayNum
) {
long dateMsLong
= date
.getTime() - (dayNum
* DAFAULT_MS
);
return new Date(dateMsLong
);
}
public static Date
getNextDateSpan(Date date
, int dayNum
) {
SimpleDateFormat sdf
= new SimpleDateFormat(DATETIME_FORMAT
);
long dateMsLong
= date
.getTime() + dayNum
* 1000 * 60 * 60;
return new Date(dateMsLong
);
}
public static boolean judgeDate(Date date1
, Date date2
) {
return date1
.getTime() > date2
.getTime();
}
public static List
<String> getBetweenDate(String startTime
, String endTime
) {
SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
List
<String> list
= new ArrayList<String>();
try {
Date startDate
= sdf
.parse(startTime
);
Date endDate
= sdf
.parse(endTime
);
Calendar calendar
= Calendar
.getInstance();
while (startDate
.getTime() <= endDate
.getTime()) {
list
.add(sdf
.format(startDate
));
calendar
.setTime(startDate
);
calendar
.add(Calendar
.DATE
, 1);
startDate
= calendar
.getTime();
}
} catch (ParseException e
) {
e
.printStackTrace();
}
return list
;
}
public static List
<String> getBetweenDatess(String startTime
, String endTime
) {
SimpleDateFormat sdf
= new SimpleDateFormat(DATETIME_FORMAT
);
List
<String> list
= new ArrayList<String>();
try {
Date startDate
= sdf
.parse(startTime
);
Date endDate
= sdf
.parse(endTime
);
Calendar calendar
= Calendar
.getInstance();
while (startDate
.getTime() <= endDate
.getTime()) {
list
.add(sdf
.format(startDate
));
calendar
.setTime(startDate
);
calendar
.add(Calendar
.DATE
, 1);
startDate
= calendar
.getTime();
}
} catch (ParseException e
) {
e
.printStackTrace();
}
return list
;
}
public static List
<String> getBetween(String startTime
, String endTime
) {
SimpleDateFormat sdf
= new SimpleDateFormat(DATETIME_FORMAT
);
List
<String> list
= new ArrayList<String>();
try {
Date startDate
= sdf
.parse(startTime
);
Date endDate
= sdf
.parse(endTime
);
Calendar calendar
= Calendar
.getInstance();
while (startDate
.getTime() <= endDate
.getTime()) {
list
.add(sdf
.format(startDate
));
calendar
.setTime(startDate
);
calendar
.add(Calendar
.DATE
, 1);
startDate
= calendar
.getTime();
}
list
.add(sdf
.format(endDate
));
} catch (ParseException e
) {
e
.printStackTrace();
}
return list
;
}
public static List
<String> getHourDate(String startTime
, String endTime
) {
SimpleDateFormat sdf
= new SimpleDateFormat(DATETIME_FORMAT
);
List
<String> list
= new ArrayList<String>();
try {
Date startDate
= sdf
.parse(startTime
);
Date endDate
= sdf
.parse(endTime
);
Calendar calendar
= Calendar
.getInstance();
while (startDate
.getTime() <= endDate
.getTime()) {
list
.add(sdf
.format(startDate
));
calendar
.setTime(startDate
);
calendar
.add(Calendar
.HOUR
, +1);
startDate
= calendar
.getTime();
}
list
.add(sdf
.format(endDate
));
} catch (ParseException e
) {
e
.printStackTrace();
}
return list
;
}
public static List
<String> getHourDate2(String startTime
, String endTime
) {
SimpleDateFormat sdf
= new SimpleDateFormat(DATETIME_FORMAT
);
SimpleDateFormat sdf2
= new SimpleDateFormat("yyyy-MM-dd HH");
List
<String> list
= new ArrayList<String>();
try {
Date startDate
= sdf
.parse(startTime
);
Date endDate
= sdf
.parse(endTime
);
Calendar calendar
= Calendar
.getInstance();
while (startDate
.getTime() <= endDate
.getTime()) {
list
.add(sdf2
.format(startDate
));
calendar
.setTime(startDate
);
calendar
.add(Calendar
.HOUR
, +1);
startDate
= calendar
.getTime();
}
} catch (ParseException e
) {
e
.printStackTrace();
}
return list
;
}
static long until(LocalDate startDate
, LocalDate endDate
) {
return startDate
.until(endDate
, ChronoUnit
.DAYS
);
}
public static List
<String> getLast24HourDate() {
List
<String> Last24Hour
= new ArrayList<>();
Date nowDate
= new Date();
Last24Hour
.add(getDataFormat(nowDate
, DATETIME_FORMAT
));
Date lastDayDate
= new Date(nowDate
.getTime() - DAFAULT_MS
);
Last24Hour
.add(getDataFormat(lastDayDate
, DATETIME_FORMAT
));
return Last24Hour
;
}
public static List
<String> getLast24HourDate2(Date nowDate
) {
List
<String> Last24Hour
= new ArrayList<>();
Last24Hour
.add(getDataFormat(nowDate
, DATETIME_FORMAT
));
Date lastDayDate
= new Date(nowDate
.getTime() - DAFAULT_MS
);
Last24Hour
.add(getDataFormat(lastDayDate
, DATETIME_FORMAT
));
return Last24Hour
;
}
public static List
<String> getLastDay24Hour() {
List
<String> Last24Hour
= new ArrayList<>();
Date lastDate
= new Date(System
.currentTimeMillis() - DAFAULT_MS
);
Last24Hour
.add(getDataFormat(lastDate
, "yyyy-MM-dd") + " 00:00:00");
Last24Hour
.add(getDataFormat(lastDate
, "yyyy-MM-dd") + " 23:59:59");
return Last24Hour
;
}
public static List
<String> getLastDay24Hour(int num
) {
List
<String> Last24Hour
= new ArrayList<>();
Date lastDate
= new Date(System
.currentTimeMillis() - DAFAULT_MS
* num
);
Last24Hour
.add(getDataFormat(lastDate
, "yyyy-MM-dd") + " 00:00:00");
Last24Hour
.add(getDataFormat(lastDate
, "yyyy-MM-dd") + " 23:59:59");
return Last24Hour
;
}
public static List
<String> getLastWeek() {
List
<String> Last24Hour
= new ArrayList<>();
Date lastDate
= new Date(System
.currentTimeMillis() - DAFAULT_MS
);
Date LastWeek
= new Date(lastDate
.getTime() - DAFAULT_MS
* 7);
Last24Hour
.add(getDataFormat(lastDate
, "yyyy-MM-dd") + " 00:00:00");
Last24Hour
.add(getDataFormat(LastWeek
, "yyyy-MM-dd") + " 23:59:59");
return Last24Hour
;
}
public static List
<String> getLast7DayDate() {
List
<String> Last7Day
= new ArrayList<>();
Date nowDate
= new Date();
Last7Day
.add(getDataFormat(nowDate
, DATETIME_FORMAT
));
Date lastDayDate
= new Date(nowDate
.getTime() - DAFAULT_MS
* 7);
Last7Day
.add(getDataFormat(lastDayDate
, DATETIME_FORMAT
));
return Last7Day
;
}
public static Date
geLastWeekMonday(Date date
) {
Calendar cal
= Calendar
.getInstance();
cal
.setTime(getThisWeekMonday(date
));
cal
.add(Calendar
.DATE
, -7);
return cal
.getTime();
}
public static Date
getThisWeekMonday(Date date
) {
Calendar cal
= Calendar
.getInstance();
cal
.setTime(date
);
int dayWeek
= cal
.get(Calendar
.DAY_OF_WEEK
);
if (1 == dayWeek
) {
cal
.add(Calendar
.DAY_OF_MONTH
, -1);
}
cal
.setFirstDayOfWeek(Calendar
.MONDAY
);
int day
= cal
.get(Calendar
.DAY_OF_WEEK
);
cal
.add(Calendar
.DATE
, cal
.getFirstDayOfWeek() - day
);
return cal
.getTime();
}
public static Date
getNextWeekMonday(Date date
) {
SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
Calendar cal
= Calendar
.getInstance();
cal
.setTime(getThisWeekMonday(date
));
cal
.add(Calendar
.DATE
, 7);
return cal
.getTime();
}
public static String
getLast7DayDate2(int days
) {
Calendar calendar
= Calendar
.getInstance();
calendar
.set(Calendar
.DAY_OF_YEAR
, calendar
.get(Calendar
.DAY_OF_YEAR
) - days
);
Date today
= calendar
.getTime();
SimpleDateFormat format
= new SimpleDateFormat("yyyy-MM-dd");
return format
.format(today
);
}
public static String
returnDate(int count
) {
Calendar strDate
= Calendar
.getInstance();
strDate
.add(strDate
.DATE
, count
);
SimpleDateFormat sdf
= new SimpleDateFormat("MM-dd");
return sdf
.format(strDate
.getTime());
}
public static String
getNextMonday(int count
) {
Calendar strDate
= Calendar
.getInstance();
int day
= getDay(strDate
);
strDate
.add(Calendar
.DATE
, -(day
- 2));
strDate
.add(Calendar
.WEEK_OF_YEAR
, count
);
return format(strDate
, "yyyy-MM-dd");
}
public static String
getNextSunday(int count
) {
Calendar strDate
= Calendar
.getInstance();
int day
= getDay(strDate
);
strDate
.add(Calendar
.DATE
, 8 - day
);
strDate
.add(Calendar
.WEEK_OF_YEAR
, count
);
return format(strDate
, "yyyy-MM-dd");
}
public static String
getNextMonthFirst(int count
) {
Calendar strDate
= Calendar
.getInstance();
int day
= getDate(strDate
);
strDate
.add(Calendar
.DATE
, -(day
- 1));
strDate
.add(Calendar
.MONTH
, count
);
return format(strDate
, "yyyy-MM-dd");
}
public static String
getNextMonthEnd(int count
) {
Calendar strDate
= Calendar
.getInstance();
int day
= getDate(strDate
);
strDate
.add(Calendar
.DATE
, -day
);
strDate
.add(Calendar
.MONTH
, count
+ 1);
return format(strDate
, "yyyy-MM-dd");
}
public static Date
getLastDay(int days
) {
Calendar c
= Calendar
.getInstance();
c
.add(Calendar
.DAY_OF_MONTH
, -days
);
return c
.getTime();
}
public static Date
getLastMonth(int days
) {
Calendar c
= Calendar
.getInstance();
c
.add(Calendar
.MONTH
, -days
);
return c
.getTime();
}
private static List
<String> getLastMonthList() {
Calendar begin
= Calendar
.getInstance();
begin
.setTime(new Date(new Date().getTime() - DAFAULT_MS
));
begin
.add(Calendar
.MONTH
, -1);
begin
.add(Calendar
.DATE
, +1);
Date result
= begin
.getTime();
Calendar end
= Calendar
.getInstance();
Long startTime
= begin
.getTimeInMillis();
Long endTime
= end
.getTimeInMillis() - DAFAULT_MS
;
List dates
= new ArrayList<>();
Long time
= startTime
;
int i
= 0;
while (time
<= endTime
) {
Date d
= new Date(time
);
DateFormat df
= new SimpleDateFormat("yyyy-MM-dd");
dates
.add(i
, df
.format(d
));
i
++;
time
+= DAFAULT_MS
;
}
return dates
;
}
public static List
<String> getMonth(String quicklTime
) {
List
<String> month
= new ArrayList<>();
SimpleDateFormat sdf2
= new SimpleDateFormat("yyyy");
switch (quicklTime
) {
case "一月":
month
.add(sdf2
.format(new Date()) + "-01-01 00:00:00");
month
.add(sdf2
.format(new Date()) + "-01-" + getDaysByYearMonth(Integer
.valueOf(sdf2
.format(new Date())), 1) + " 23:59:59");
break;
case "二月":
month
.add(sdf2
.format(new Date()) + "-02-01 00:00:00");
month
.add(sdf2
.format(new Date()) + "-02-" + getDaysByYearMonth(Integer
.valueOf(sdf2
.format(new Date())), 2) + " 23:59:59");
break;
case "三月":
month
.add(sdf2
.format(new Date()) + "-03-01 00:00:00");
month
.add(sdf2
.format(new Date()) + "-03-" + getDaysByYearMonth(Integer
.valueOf(sdf2
.format(new Date())), 1) + " 23:59:59");
break;
case "四月":
month
.add(sdf2
.format(new Date()) + "-04-01 00:00:00");
month
.add(sdf2
.format(new Date()) + "-04-" + getDaysByYearMonth(Integer
.valueOf(sdf2
.format(new Date())), 1) + " 23:59:59");
break;
case "五月":
month
.add(sdf2
.format(new Date()) + "-05-01 00:00:00");
month
.add(sdf2
.format(new Date()) + "-05-" + getDaysByYearMonth(Integer
.valueOf(sdf2
.format(new Date())), 1) + " 23:59:59");
break;
case "六月":
month
.add(sdf2
.format(new Date()) + "-06-01 00:00:00");
month
.add(sdf2
.format(new Date()) + "-06-" + getDaysByYearMonth(Integer
.valueOf(sdf2
.format(new Date())), 1) + " 23:59:59");
break;
case "七月":
month
.add(sdf2
.format(new Date()) + "-07-01 00:00:00");
month
.add(sdf2
.format(new Date()) + "-07-" + getDaysByYearMonth(Integer
.valueOf(sdf2
.format(new Date())), 1) + " 23:59:59");
break;
case "八月":
month
.add(sdf2
.format(new Date()) + "-08-01 00:00:00");
month
.add(sdf2
.format(new Date()) + "-08-" + getDaysByYearMonth(Integer
.valueOf(sdf2
.format(new Date())), 1) + " 23:59:59");
break;
case "九月":
month
.add(sdf2
.format(new Date()) + "-09-01 00:00:00");
month
.add(sdf2
.format(new Date()) + "-09-" + getDaysByYearMonth(Integer
.valueOf(sdf2
.format(new Date())), 1) + " 23:59:59");
break;
case "十月":
month
.add(sdf2
.format(new Date()) + "-10-01 00:00:00");
month
.add(sdf2
.format(new Date()) + "-10-" + getDaysByYearMonth(Integer
.valueOf(sdf2
.format(new Date())), 1) + " 23:59:59");
break;
case "十一月":
month
.add(sdf2
.format(new Date()) + "-11-01 00:00:00");
month
.add(sdf2
.format(new Date()) + "-11-" + getDaysByYearMonth(Integer
.valueOf(sdf2
.format(new Date())), 1) + " 23:59:59");
break;
case "十二月":
month
.add(sdf2
.format(new Date()) + "-12-01 00:00:00");
month
.add(sdf2
.format(new Date()) + "-12-" + getDaysByYearMonth(Integer
.valueOf(sdf2
.format(new Date())), 1) + " 23:59:59");
break;
}
return month
;
}
public static String
getDaysByYearMonth(int year
, int month
) {
Calendar a
= Calendar
.getInstance();
a
.set(Calendar
.YEAR
, year
);
a
.set(Calendar
.MONTH
, month
- 1);
a
.set(Calendar
.DATE
, 1);
a
.roll(Calendar
.DATE
, -1);
int maxDate
= a
.get(Calendar
.DATE
);
return String
.valueOf(maxDate
);
}
public static Date
currentDate() {
return Calendar
.getInstance().getTime();
}
public static int getYear(Calendar c
) {
if (c
!= null
) {
return c
.get(Calendar
.YEAR
);
} else {
return Calendar
.getInstance().get(Calendar
.YEAR
);
}
}
public static int getMonth(Calendar c
) {
if (c
!= null
) {
return c
.get(Calendar
.MONTH
) + 1;
} else {
return Calendar
.getInstance().get(Calendar
.MONTH
) + 1;
}
}
public static int getDate(Calendar c
) {
if (c
!= null
) {
return c
.get(Calendar
.DATE
);
} else {
return Calendar
.getInstance().get(Calendar
.DATE
);
}
}
public static int getDay(Calendar c
) {
if (c
!= null
) {
return c
.get(Calendar
.DAY_OF_WEEK
);
} else {
return Calendar
.getInstance().get(Calendar
.DAY_OF_WEEK
);
}
}
public static int getHour(Calendar c
) {
if (c
!= null
) {
return c
.get(Calendar
.HOUR_OF_DAY
);
} else {
return Calendar
.getInstance().get(Calendar
.HOUR_OF_DAY
);
}
}
public static int getMinute(Calendar c
) {
if (c
!= null
) {
return c
.get(Calendar
.MINUTE
);
} else {
return Calendar
.getInstance().get(Calendar
.MINUTE
);
}
}
public static int getSecond(Calendar c
) {
if (c
!= null
) {
return c
.get(Calendar
.SECOND
);
} else {
return Calendar
.getInstance().get(Calendar
.SECOND
);
}
}
public static Calendar
beforeNDays(Calendar c
, int n
) {
long offset
= n
* DAFAULT_MS
;
Calendar calendar
= null
;
if (c
!= null
) {
calendar
= c
;
} else {
calendar
= Calendar
.getInstance();
}
calendar
.setTimeInMillis(calendar
.getTimeInMillis() - offset
);
return calendar
;
}
public static Calendar
afterNDays(Calendar c
, int n
) {
long offset
= n
* DAFAULT_MS
;
Calendar calendar
= null
;
if (c
!= null
) {
calendar
= c
;
} else {
calendar
= Calendar
.getInstance();
}
calendar
.setTimeInMillis(calendar
.getTimeInMillis() + offset
);
return calendar
;
}
public static Date
addMonths(Date date
, int addMonths
) {
Calendar calendar
= Calendar
.getInstance();
calendar
.setTime(date
);
calendar
.add(Calendar
.MONTH
, addMonths
);
return calendar
.getTime();
}
public static Date
addMonths(int addMonths
) {
return addMonths(new Date(), addMonths
);
}
public static Date
addDays(Date date
, int addDays
) {
Calendar calendar
= Calendar
.getInstance();
calendar
.setTime(date
);
calendar
.add(Calendar
.DATE
, addDays
);
return calendar
.getTime();
}
public static Date
addDays(int addDays
) {
return addDays(new Date(), addDays
);
}
public static Date
addHours(Date date
, int addHours
) {
Calendar calendar
= Calendar
.getInstance();
calendar
.setTime(date
);
calendar
.add(Calendar
.HOUR
, addHours
);
return calendar
.getTime();
}
public static Date
addHours(int addHours
) {
return addHours(new Date(), addHours
);
}
public static Date
addMinutes(Date date
, int addMinutes
) {
Calendar calendar
= Calendar
.getInstance();
calendar
.setTime(date
);
calendar
.add(Calendar
.MINUTE
, addMinutes
);
return calendar
.getTime();
}
public static Date
addMinutes(int addMinutes
) {
return addMinutes(new Date(), addMinutes
);
}
public static Date
addSeconds(Date date
, int addSeconds
) {
Calendar calendar
= Calendar
.getInstance();
calendar
.setTime(date
);
calendar
.add(Calendar
.SECOND
, addSeconds
);
return calendar
.getTime();
}
public static Date
addSeconds(int addSeconds
) {
return addSeconds(new Date(), addSeconds
);
}
public static Calendar
yesterday(Calendar c
) {
long offset
= 1 * DAFAULT_MS
;
Calendar calendar
= null
;
if (c
!= null
) {
calendar
= c
;
} else {
calendar
= Calendar
.getInstance();
}
calendar
.setTimeInMillis(calendar
.getTimeInMillis() - offset
);
return calendar
;
}
public static Calendar
tomorrow(Calendar c
) {
long offset
= 1 * DAFAULT_MS
;
Calendar calendar
= null
;
if (c
!= null
) {
calendar
= c
;
} else {
calendar
= Calendar
.getInstance();
}
calendar
.setTimeInMillis(calendar
.getTimeInMillis() + offset
);
return calendar
;
}
public static Calendar
before(Calendar c
, long offset
) {
Calendar calendar
= null
;
if (c
!= null
) {
calendar
= c
;
} else {
calendar
= Calendar
.getInstance();
}
calendar
.setTimeInMillis(calendar
.getTimeInMillis() - offset
);
return calendar
;
}
public static Calendar
after(Calendar c
, long offset
) {
Calendar calendar
= null
;
if (c
!= null
) {
calendar
= c
;
} else {
calendar
= Calendar
.getInstance();
}
calendar
.setTimeInMillis(calendar
.getTimeInMillis() - offset
);
return calendar
;
}
public static String
format(Calendar c
, String pattern
) {
Calendar calendar
;
if (c
!= null
) {
calendar
= c
;
} else {
calendar
= Calendar
.getInstance();
}
if (pattern
== null
|| pattern
.equals("")) {
pattern
= DATETIME_FORMAT
;
}
SimpleDateFormat sdf
= new SimpleDateFormat(pattern
);
return sdf
.format(calendar
.getTime());
}
public static String
format(Date date
, String pattern
) {
Date tempDate
= null
;
if (date
!= null
) {
tempDate
= date
;
} else {
tempDate
= Calendar
.getInstance().getTime();
}
if (pattern
== null
|| pattern
.equals("")) {
pattern
= DATETIME_FORMAT
;
}
SimpleDateFormat sdf
= new SimpleDateFormat(pattern
);
return sdf
.format(tempDate
);
}
public static Calendar
date2Calendar(Date d
) {
Calendar c
= Calendar
.getInstance();
c
.setTime(d
);
return c
;
}
public static Date
calendar2Date(Calendar c
) {
return c
.getTime();
}
public static Timestamp
date2Timestamp(Date d
) {
return new Timestamp(d
.getTime());
}
public static Timestamp
calendar2Timestamp(Calendar c
) {
return new Timestamp(c
.getTimeInMillis());
}
public static Calendar
timestamp2Calendar(Timestamp ts
) {
Calendar c
= Calendar
.getInstance();
c
.setTime(ts
);
return c
;
}
public static String
getTimeString() {
return format(Calendar
.getInstance(), DATETIME_FORMAT
);
}
public static Calendar
pars2Calender(String s
) {
Timestamp ts
= Timestamp
.valueOf(s
);
return timestamp2Calendar(ts
);
}
public static String
getLessMinute(String time
, int Less
) {
DateFormat fmt
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date
= null
;
try {
date
= fmt
.parse(time
);
date
= addMinutes(date
, Less
);
} catch (ParseException e
) {
e
.printStackTrace();
}
String endTime
= fmt
.format(date
);
return endTime
;
}
}