packagecom.drew.util;
importjava.time.DayOfWeek;
importjava.time.LocalDate;
importjava.time.LocalDateTime;
importjava.time.LocalTime;
importjava.time.MonthDay;
importjava.time.Period;
importjava.time.format.DateTimeFormatter;
importjava.time.format.FormatStyle;
importjava.time.temporal.ChronoUnit;
importjava.time.temporal.TemporalAdjusters;
importjava.util.List;
importcom.google.common.collect.Lists;
publicclassDateUtil8 {
privatestaticfinalStringYEAR="year";
privatestaticfinalStringMONTH="month";
privatestaticfinalStringWEEK="week";
privatestaticfinalStringDAY="day";
privatestaticfinalStringHOUR="hour";
privatestaticfinalStringMINUTE="minute";
privatestaticfinalStringSECOND="second";
privatestaticfinalStringMONDAY="MONDAY";privatestaticfinalStringTUESDAY="TUESDAY";privatestaticfinalStringWEDNESDAY="WEDNESDAY";privatestaticfinalStringTHURSDAY="THURSDAY";privatestaticfinalStringFRIDAY="FRIDAY";privatestaticfinalStringSATURDAY="SATURDAY";privatestaticfinalStringSUNDAY="SUNDAY";privatestaticfinalDateTimeFormatteryyyyMMdd_EN=DateTimeFormatter.ofPattern("yyyy-MM-dd");
privatestaticfinalDateTimeFormatteryyyyMMddHH_EN=DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
privatestaticfinalDateTimeFormatteryyyyMMddHHmm_EN=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
privatestaticfinalDateTimeFormatteryyyyMMddHHmmss_EN=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
privatestaticfinalDateTimeFormatterHHmmss_EN=DateTimeFormatter.ofPattern("HH:mm:ss");
privatestaticfinalDateTimeFormatteryyyyMMdd_CN=DateTimeFormatter.ofPattern("yyyy年MM月dd日");
privatestaticfinalDateTimeFormatteryyyyMMddHH_CN=DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时");
privatestaticfinalDateTimeFormatteryyyyMMddHHmm_CN=DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分");
privatestaticfinalDateTimeFormatteryyyyMMddHHmmss_CN=DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分ss秒");
privatestaticfinalDateTimeFormatterHHmmss_CN=DateTimeFormatter.ofPattern("HH时mm分ss秒");
privatestaticfinalDateTimeFormattershotDate=DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
privatestaticfinalDateTimeFormatterfullDate=DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
privatestaticfinalDateTimeFormatterlongDate=DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
privatestaticfinalDateTimeFormattermediumDate=DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
publicstaticStringgetNowDate_EN() {
returnString.valueOf(LocalDate.now());
}
publicstaticStringgetNowTime_EN() {
returnLocalDateTime.now().format(yyyyMMddHHmmss_EN);
}
publicstaticStringgetNowTime_EN_yMdH() {
returnLocalDateTime.now().format(yyyyMMddHH_EN);
}
publicstaticStringgetNowTime_CN_yMdH() {
returnLocalDateTime.now().format(yyyyMMddHH_CN);
}
publicstaticStringgetNowTime_EN_yMdHm() {
returnLocalDateTime.now().format(yyyyMMddHHmm_EN);
}
publicstaticStringgetNowTime_CN_yMdHm() {
returnLocalDateTime.now().format(yyyyMMddHHmm_CN);
}
publicstaticStringgetNowTime_CN_HHmmss() {
returnLocalDateTime.now().format(HHmmss_CN);
}
publicstaticStringgetTime(StringformatStr) {
returnLocalDateTime.now().format(DateTimeFormatter.ofPattern(formatStr));
}
publicstaticStringgetNowDate_CN() {
returnLocalDate.now().format(yyyyMMdd_CN);
}
publicstaticStringgetNowTime_CN() {
returnLocalDateTime.now().format(yyyyMMddHHmmss_CN);
}
publicstaticStringgetNowLocalTime_shot() {
returnLocalDateTime.now().format(shotDate);
}
publicstaticStringgetNowLocalTime_full() {
returnLocalDateTime.now().format(fullDate);
}
publicstaticStringgetNowLocalTime_long() {
returnLocalDateTime.now().format(longDate);
}
publicstaticStringgetNowLocalTime_medium() {
returnLocalDateTime.now().format(mediumDate);
}
publicstaticIntegergetNodeTime(Stringnode) {
LocalDateTimetoday=LocalDateTime.now();
IntegerresultNode=null;
switch (node) {
caseYEAR:
resultNode=today.getYear();
break;
caseMONTH:
resultNode=today.getMonthValue();
break;
caseWEEK:
resultNode=transformWeekEN2Num(String.valueOf(today.getDayOfWeek()));
break;
caseDAY:
resultNode=today.getDayOfMonth();
break;
caseHOUR:
resultNode=today.getHour();
break;
caseMINUTE:
resultNode=today.getMinute();
break;
caseSECOND:
resultNode=today.getSecond();
break;
default:
resultNode=today.getDayOfYear();
break;
}
returnresultNode;
}
publicstaticinttransformWeekEN2Num(StringenWeek) {
if (MONDAY.equals(enWeek)) {
return1;
} elseif (TUESDAY.equals(enWeek)) {
return2;
} elseif (WEDNESDAY.equals(enWeek)) {
return3;
} elseif (THURSDAY.equals(enWeek)) {
return4;
} elseif (FRIDAY.equals(enWeek)) {
return5;
} elseif (SATURDAY.equals(enWeek)) {
return6;
} elseif (SUNDAY.equals(enWeek)) {
return7;
} else {
return-1;
}
}
publicstaticStringgetAfterOrPreNowTime(Stringnode, Longnum) {
LocalDateTimenow=LocalDateTime.now();
if (HOUR.equals(node)) {
returnnow.plusHours(num).format(yyyyMMddHHmmss_EN);
} elseif (DAY.equals(node)) {
returnnow.plusDays(num).format(yyyyMMddHHmmss_EN);
} elseif (WEEK.equals(node)) {
returnnow.plusWeeks(num).format(yyyyMMddHHmmss_EN);
} elseif (MONTH.equals(node)) {
returnnow.plusMonths(num).format(yyyyMMddHHmmss_EN);
} elseif (YEAR.equals(node)) {
returnnow.plusYears(num).format(yyyyMMddHHmmss_EN);
} elseif (MINUTE.equals(node)) {
returnnow.plusMinutes(num).format(yyyyMMddHHmmss_EN);
} elseif (SECOND.equals(node)) {
returnnow.plusSeconds(num).format(yyyyMMddHHmmss_EN);
} else {
return"Node is Error!";
}
}
publicstaticStringgetAfterOrPreNowTimePlus(DateTimeFormatterdtf, Stringnode, Longnum) {
LocalDateTimenow=LocalDateTime.now();
if (HOUR.equals(node)) {
returnnow.plusHours(num).format(dtf);
} elseif (DAY.equals(node)) {
returnnow.plusDays(num).format(dtf);
} elseif (WEEK.equals(node)) {
returnnow.plusWeeks(num).format(dtf);
} elseif (MONTH.equals(node)) {
returnnow.plusMonths(num).format(dtf);
} elseif (YEAR.equals(node)) {
returnnow.plusYears(num).format(dtf);
} elseif (MINUTE.equals(node)) {
returnnow.plusMinutes(num).format(dtf);
} elseif (SECOND.equals(node)) {
returnnow.plusSeconds(num).format(dtf);
} else {
return"Node is Error!";
}
}
publicstaticStringgetAfterOrPreNowTimeSimp(Stringnode, Longnum) {
LocalTimenow=LocalTime.now();
if (HOUR.equals(node)) {
returnnow.plusHours(num).format(HHmmss_EN);
} elseif (MINUTE.equals(node)) {
returnnow.plusMinutes(num).format(HHmmss_EN);
} elseif (SECOND.equals(node)) {
returnnow.plusSeconds(num).format(HHmmss_EN);
} else {
return"Node is Error!";
}
}
publicstaticbooleanisBirthday(intmonth, intdayOfMonth) {
MonthDaybirthDay=MonthDay.of(month, dayOfMonth);
MonthDaycurMonthDay=MonthDay.from(LocalDate.now());if (birthDay.equals(curMonthDay)) {
returntrue;
}
returnfalse;
}
publicstaticStringgetAfterOrPreDayDate(intindex) {
returnLocalDate.now().plus(index, ChronoUnit.DAYS).format(yyyyMMdd_EN);
}
publicstaticStringgetAfterOrPreWeekDate(intindex) {
returnLocalDate.now().plus(index, ChronoUnit.WEEKS).format(yyyyMMdd_EN);
}
publicstaticStringgetAfterOrPreMonthDate(intindex) {
returnLocalDate.now().plus(index, ChronoUnit.MONTHS).format(yyyyMMdd_EN);
}
publicstaticStringgetAfterOrPreYearDate(intindex) {
returnLocalDate.now().plus(index, ChronoUnit.YEARS).format(yyyyMMdd_EN);
}
publicstaticStringgetAfterOrPreDate(Stringdate, Stringnode, intindex) {
date=date.trim();
if (DAY.equals(node)) {
returnLocalDate.parse(date).plus(index, ChronoUnit.DAYS).format(yyyyMMdd_EN);
} elseif (WEEK.equals(node)) {
returnLocalDate.parse(date).plus(index, ChronoUnit.WEEKS).format(yyyyMMdd_EN);
} elseif (MONTH.equals(node)) {
returnLocalDate.parse(date).plus(index, ChronoUnit.MONTHS).format(yyyyMMdd_EN);
} elseif (YEAR.equals(node)) {
returnLocalDate.parse(date).plus(index, ChronoUnit.YEARS).format(yyyyMMdd_EN);
} else {
return"Wrong date format!";
}
}
publicstaticbooleanisLeapYear(Stringdate) {
returnLocalDate.parse(date.trim()).isLeapYear();
}
publicstaticintperidCount(Stringdate1, Stringdate2, Stringnode) {
date1=date1.trim();
date2=date2.trim();
if (DAY.equals(node)) {
returnPeriod.between(LocalDate.parse(date1), LocalDate.parse(date2)).getDays();
} elseif (MONTH.equals(node)) {
returnPeriod.between(LocalDate.parse(date1), LocalDate.parse(date2)).getMonths();
} elseif (YEAR.equals(node)) {
returnPeriod.between(LocalDate.parse(date1), LocalDate.parse(date2)).getYears();
} else {
return0;
}
}
publicstaticList<String>getPieDateRange(StringstartDate, StringendDate, Stringperiod) {
List<String>result=Lists.newArrayList();
LocalDateend=LocalDate.parse(endDate, yyyyMMdd_EN);
LocalDatestart=LocalDate.parse(startDate, yyyyMMdd_EN);
LocalDatetmp=start;
switch (period) {
caseDAY:
while (start.isBefore(end) ||start.isEqual(end)) {
result.add(start.toString());
start=start.plusDays(1);
}
break;
caseWEEK:
while (tmp.isBefore(end) ||tmp.isEqual(end)) {
if (tmp.plusDays(6).isAfter(end)) {
result.add(tmp.toString() +","+end);
} else {
result.add(tmp.toString() +","+tmp.plusDays(6));
}
tmp=tmp.plusDays(7);
}
break;
caseMONTH:
while (tmp.isBefore(end) ||tmp.isEqual(end)) {
LocalDatelastDayOfMonth=tmp.with(TemporalAdjusters.lastDayOfMonth());
if (lastDayOfMonth.isAfter(end)) {
result.add(tmp.toString() +","+end);
} else {
result.add(tmp.toString() +","+lastDayOfMonth);
}
tmp=lastDayOfMonth.plusDays(1);
}
break;
caseYEAR:
while (tmp.isBefore(end) ||tmp.isEqual(end)) {
LocalDatelastDayOfYear=tmp.with(TemporalAdjusters.lastDayOfYear());
if (lastDayOfYear.isAfter(end)) {
result.add(tmp.toString() +","+end);
} else {
result.add(tmp.toString() +","+lastDayOfYear);
}
tmp=lastDayOfYear.plusDays(1);
}
break;
default:
break;
}
returnresult;
}
publicstaticStringgetLastDayOfMonth(StringcurDate, booleanfirstOrLast) {
if (firstOrLast) {
returnLocalDate.parse(curDate, yyyyMMdd_EN).with(TemporalAdjusters.firstDayOfMonth()).toString();
} else {
returnLocalDate.parse(curDate, yyyyMMdd_EN).with(TemporalAdjusters.lastDayOfMonth()).toString();
}
}
publicstaticStringgetLastDayOfYear(StringcurDate, booleanfirstOrLast) {
if (firstOrLast) {
returnLocalDate.parse(curDate, yyyyMMdd_EN).with(TemporalAdjusters.firstDayOfYear()).toString();
} else {
returnLocalDate.parse(curDate, yyyyMMdd_EN).with(TemporalAdjusters.lastDayOfYear()).toString();
}
}
publicstaticStringgetNextWeekDate(StringcurDay, intdayOfWeek, booleanisContainCurDay) {
dayOfWeek=dayOfWeek<1||dayOfWeek>7?1 : dayOfWeek;
if (isContainCurDay) {
returnLocalDate.parse(curDay).with(TemporalAdjusters.nextOrSame(DayOfWeek.of(dayOfWeek))).toString();
} else {
returnLocalDate.parse(curDay).with(TemporalAdjusters.next(DayOfWeek.of(dayOfWeek))).toString();
}
}
publicstaticStringgetPreWeekDate(StringcurDay, intdayOfWeek, booleanisCurDay) {
dayOfWeek=dayOfWeek<1||dayOfWeek>7?1 : dayOfWeek;
if (isCurDay) {
returnLocalDate.parse(curDay).with(TemporalAdjusters.previousOrSame(DayOfWeek.of(dayOfWeek))).toString();
} else {
returnLocalDate.parse(curDay).with(TemporalAdjusters.previous(DayOfWeek.of(dayOfWeek))).toString();
}
}
publicstaticStringgetFirstOrLastWeekDate(StringcurDay, intdayOfWeek, booleanlastOrFirst) {
dayOfWeek=dayOfWeek<1||dayOfWeek>7?1 : dayOfWeek;
if (lastOrFirst) {
returnLocalDate.parse(curDay).with(TemporalAdjusters.lastInMonth(DayOfWeek.of(dayOfWeek))).toString();
} else {
returnLocalDate.parse(curDay).with(TemporalAdjusters.firstInMonth(DayOfWeek.of(dayOfWeek))).toString();
}
}
publicstaticvoidmain(String[] args) {
System.out.println("===================");
}
}