【踩坑日常】–SimpleDateFormat,在SimpleDateFormat中踩坑之后,使用DateTimeFormatter改进JAVA时间工具类
JDK 版本1.8以上
代码:
支持 获取开始结束时间区间的【月,季,半年,年】时间List支持 获取指定日期的【月,季,半年,年】开始结束日期获取日期之间的【天,月,年】时间差
package test;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class DateTimeHelp {
public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HHmmss");
public static final DateTimeFormatter YEAR_MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");
public static final DateTimeFormatter SHORT_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyMMdd");
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static final DateTimeFormatter DATE_FORMATTER_CHINESE = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
public static final DateTimeFormatter SHORT_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyMMddHHmmss");
public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static final DateTimeFormatter DATETIME_FORMATTER_CHINESE = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
private final static DateTimeFormatter MonTH = DateTimeFormatter.ofPattern("MM");
private final static DateTimeFormatter DAY = DateTimeFormatter.ofPattern("dd");
private final static DateTimeFormatter YEAR = DateTimeFormatter.ofPattern("yyyy年");
public static final LocalDateTime now=LocalDateTime.now();
public static String getCurrentLocalDate(DateTimeFormatter dateTimeFormatter){
return dateTimeFormatter.format(LocalDateTime.now());
}
public static String getCurrentQuarter(String targetDate,DateTimeFormatter dateTimeFormatter){
StringBuffer stringBuffer = new StringBuffer();
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try{
LocalDate parse = LocalDate.parse(targetDate, dateTimeFormatter);
int monthValue = parse.getMonthValue();
int quarter=1;
if (monthValue >= 1 && monthValue <= 3) {
quarter=1;
} else if (monthValue >= 4 && monthValue <= 6) {
quarter=2;
} else if (monthValue >= 7 && monthValue <= 9) {
quarter=3;
} else if (monthValue >= 10 && monthValue <= 12) {
quarter=4;
}
stringBuffer.append(parse.getYear()).append("年第").append(quarter).append("季度");
}catch (Exception e){
e.printStackTrace();
}
return stringBuffer.toString();
}
public static String getCurrentHalfYear(String targetDate,DateTimeFormatter dateTimeFormatter){
StringBuffer stringBuffer = new StringBuffer();
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try{
LocalDate parse = LocalDate.parse(targetDate, dateTimeFormatter);
int monthValue = parse.getMonthValue();
String halfYear="上半年";
if (monthValue >= 1 && monthValue <= 6) {
halfYear="上半年";
} else if (monthValue >= 7 && monthValue <= 12) {
halfYear="下半年";
}
stringBuffer.append(parse.getYear()).append("年").append(halfYear);
}catch (Exception e){
e.printStackTrace();
}
return stringBuffer.toString();
}
public static boolean isLeapYear(LocalDate localDate){
return localDate.isLeapYear();
}
public static boolean isLeapYear(String localDate){
return isLeapYear(LocalDateTime.parse(localDate,DATE_FORMATTER).toLocalDate());
}
public static boolean isProper(String localData,DateTimeFormatter dateTimeFormatter){
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try{
LocalDateTime.parse(localData,dateTimeFormatter);
return true;
}catch (Exception e){
return false;
}
}
public static String getCurrentMonthBeginTime(String targetDate,DateTimeFormatter dateTimeFormatter){
String beginTime=null;
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try{
LocalDate parse = LocalDate.parse(targetDate, dateTimeFormatter);
LocalDate beginDateTime = LocalDate.of(parse.getYear(), parse.getMonthValue(), 1);
beginTime = dateTimeFormatter.format(beginDateTime);
}catch (Exception e){
e.printStackTrace();
}
return beginTime;
}
public static String getCurrentMonthEndTime(String targetDate,DateTimeFormatter dateTimeFormatter){
String endTime=null;
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try{
LocalDate parse = LocalDate.parse(targetDate, dateTimeFormatter);
LocalDate beginDateTime = LocalDate.of(parse.getYear(), parse.getMonthValue()+1, 1);
LocalDate endLocalDate = beginDateTime.plusDays(-1);
endTime = dateTimeFormatter.format(endLocalDate);
}catch (Exception e){
e.printStackTrace();
}
return endTime;
}
public static String getCurrentYearBeginTime(String targetDate,DateTimeFormatter dateTimeFormatter){
String beginTime=null;
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try{
LocalDate parse = LocalDate.parse(targetDate, dateTimeFormatter);
LocalDate beginDateTime = LocalDate.of(parse.getYear(), 1, 1);
beginTime = dateTimeFormatter.format(beginDateTime);
}catch (Exception e){
e.printStackTrace();
}
return beginTime;
}
public static String getCurrentYearEndTime(String targetDate,DateTimeFormatter dateTimeFormatter){
String endTime=null;
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try{
LocalDate parse = LocalDate.parse(targetDate, dateTimeFormatter);
LocalDate beginDateTime = LocalDate.of(parse.getYear()+1, 1, 1);
LocalDate endLocalDate = beginDateTime.plusDays(-1);
endTime = dateTimeFormatter.format(endLocalDate);
}catch (Exception e){
e.printStackTrace();
}
return endTime;
}
public static String getCurrentQuarterBeginTime(String targetDate,DateTimeFormatter dateTimeFormatter){
String beginTime=null;
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try{
LocalDate parse = LocalDate.parse(targetDate, dateTimeFormatter);
int currentMonth=parse.getMonthValue();
int quarterMonth=currentMonth;
if (currentMonth >= 1 && currentMonth <= 3) {
quarterMonth=1;
} else if (currentMonth >= 4 && currentMonth <= 6) {
quarterMonth=4;
} else if (currentMonth >= 7 && currentMonth <= 9) {
quarterMonth=7;
} else if (currentMonth >= 10 && currentMonth <= 12) {
quarterMonth=10;
}
LocalDate beginDateTime = LocalDate.of(parse.getYear(), quarterMonth, 1);
beginTime = dateTimeFormatter.format(beginDateTime);
}catch (Exception e){
e.printStackTrace();
}
return beginTime;
}
public static String getCurrentQuarterEndTime(String targetDate,DateTimeFormatter dateTimeFormatter){
String endTime=null;
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try{
LocalDate parse = LocalDate.parse(targetDate, dateTimeFormatter);
int currentMonth=parse.getMonthValue();
int quarterMonth=currentMonth;
int quarterDay=parse.getDayOfMonth();
if (currentMonth >= 1 && currentMonth <= 3) {
quarterMonth=3;
quarterDay=31;
} else if (currentMonth >= 4 && currentMonth <= 6) {
quarterMonth=6;
quarterDay=30;
} else if (currentMonth >= 7 && currentMonth <= 9) {
quarterMonth=9;
quarterDay=30;
} else if (currentMonth >= 10 && currentMonth <= 12) {
quarterMonth=12;
quarterDay=31;
}
LocalDate endDateTime = LocalDate.of(parse.getYear(), quarterMonth, quarterDay);
endTime = dateTimeFormatter.format(endDateTime);
}catch (Exception e){
e.printStackTrace();
}
return endTime;
}
public static String getHalfYearBeginTime(String targetDate,DateTimeFormatter dateTimeFormatter){
String beginTime=null;
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try{
LocalDate parse = LocalDate.parse(targetDate, dateTimeFormatter);
int currentMonth=parse.getMonthValue();
int quarterMonth=currentMonth;
if (currentMonth >= 1 && currentMonth <= 6) {
quarterMonth=1;
} else if (currentMonth >= 7 && currentMonth <= 12) {
quarterMonth=7;
}
LocalDate beginDateTime = LocalDate.of(parse.getYear(), quarterMonth, 1);
beginTime = dateTimeFormatter.format(beginDateTime);
}catch (Exception e){
e.printStackTrace();
}
return beginTime;
}
public static String getHalfYearEndTime(String targetDate,DateTimeFormatter dateTimeFormatter){
String endTime=null;
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try{
LocalDate parse = LocalDate.parse(targetDate, dateTimeFormatter);
int currentMonth=parse.getMonthValue();
int quarterMonth=currentMonth;
int quarterDay=parse.getDayOfMonth();
if (currentMonth >= 1 && currentMonth <= 6) {
quarterMonth=6;
quarterDay=30;
} else if (currentMonth >= 7 && currentMonth <= 12) {
quarterMonth=12;
quarterDay=31;
}
LocalDate endDateTime = LocalDate.of(parse.getYear(), quarterMonth, quarterDay);
endTime = dateTimeFormatter.format(endDateTime);
}catch (Exception e){
e.printStackTrace();
}
return endTime;
}
public static List getMonthDateList(String startDateStr, String endDateStr,DateTimeFormatter dateTimeFormatter){
List dateList = new ArrayList<>();
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try {
LocalDate startLocalDate = LocalDate.parse(startDateStr, dateTimeFormatter);
String currentMonthEndTime = getCurrentMonthEndTime(endDateStr, dateTimeFormatter);
LocalDate endLocalDate = LocalDate.parse(currentMonthEndTime, dateTimeFormatter);
while (startLocalDate.isBefore(endLocalDate)||startLocalDate.isEqual(endLocalDate)){
String name=startLocalDate.getYear()+"-"+MONTH.format(startLocalDate);
DataInfo dataInfo = new DataInfo(name,
getCurrentMonthBeginTime(dateTimeFormatter.format(startLocalDate), dateTimeFormatter),
getCurrentMonthEndTime(dateTimeFormatter.format(startLocalDate), dateTimeFormatter)
);
dateList.add(dataInfo);
startLocalDate = startLocalDate.plusMonths(1);
}
}catch (Exception e){
e.printStackTrace();
}
return dateList;
}
public static List getQuarterDateList(String startDateStr, String endDateStr,DateTimeFormatter dateTimeFormatter){
List dateList = new ArrayList<>();
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try {
String currentQuarterBeginTime = getCurrentQuarterBeginTime(startDateStr, dateTimeFormatter);
LocalDate startLocalDate = LocalDate.parse(currentQuarterBeginTime, dateTimeFormatter);
String currentQuarterEndTime = getCurrentQuarterEndTime(endDateStr, dateTimeFormatter);
LocalDate endLocalDate = LocalDate.parse(currentQuarterEndTime, dateTimeFormatter);
while (startLocalDate.isBefore(endLocalDate)||startLocalDate.isEqual(endLocalDate)){
String name = getCurrentQuarter(dateTimeFormatter.format(startLocalDate), dateTimeFormatter);
DataInfo dataInfo = new DataInfo(name,
getCurrentQuarterBeginTime(dateTimeFormatter.format(startLocalDate), dateTimeFormatter),
getCurrentQuarterEndTime(dateTimeFormatter.format(startLocalDate), dateTimeFormatter)
);
dateList.add(dataInfo);
startLocalDate = startLocalDate.plusMonths(3);
}
}catch (Exception e){
e.printStackTrace();
}
return dateList;
}
public static List getHalfYearDateList(String startDateStr, String endDateStr,DateTimeFormatter dateTimeFormatter){
List dateList = new ArrayList<>();
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try {
String currentQuarterBeginTime = getHalfYearBeginTime(startDateStr, dateTimeFormatter);
LocalDate startLocalDate = LocalDate.parse(currentQuarterBeginTime, dateTimeFormatter);
String currentQuarterEndTime = getHalfYearEndTime(endDateStr, dateTimeFormatter);
LocalDate endLocalDate = LocalDate.parse(currentQuarterEndTime, dateTimeFormatter);
while (startLocalDate.isBefore(endLocalDate)||startLocalDate.isEqual(endLocalDate)){
String name = getCurrentHalfYear(dateTimeFormatter.format(startLocalDate), dateTimeFormatter);
DataInfo dataInfo = new DataInfo(name,
getHalfYearBeginTime(dateTimeFormatter.format(startLocalDate), dateTimeFormatter),
getHalfYearEndTime(dateTimeFormatter.format(startLocalDate), dateTimeFormatter)
);
dateList.add(dataInfo);
startLocalDate = startLocalDate.plusMonths(6);
}
}catch (Exception e){
e.printStackTrace();
}
return dateList;
}
public static List getYearDateList(String startDateStr, String endDateStr,DateTimeFormatter dateTimeFormatter){
List dateList = new ArrayList<>();
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try {
String currentQuarterBeginTime = getCurrentYearBeginTime(startDateStr, dateTimeFormatter);
LocalDate startLocalDate = LocalDate.parse(currentQuarterBeginTime, dateTimeFormatter);
String currentQuarterEndTime = getCurrentYearEndTime(endDateStr, dateTimeFormatter);
LocalDate endLocalDate = LocalDate.parse(currentQuarterEndTime, dateTimeFormatter);
while (startLocalDate.isBefore(endLocalDate)||startLocalDate.isEqual(endLocalDate)){
String name = YEAR.format(startLocalDate);
DataInfo dataInfo = new DataInfo(name,
getCurrentYearBeginTime(dateTimeFormatter.format(startLocalDate), dateTimeFormatter),
getCurrentYearEndTime(dateTimeFormatter.format(startLocalDate), dateTimeFormatter)
);
dateList.add(dataInfo);
startLocalDate = startLocalDate.plusYears(1);
}
}catch (Exception e){
e.printStackTrace();
}
return dateList;
}
public static String plus(String targetDate,DateTimeFormatter dateTimeFormatter,long number, TemporalUnit field){
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try {
LocalDate parse = LocalDate.parse(targetDate, dateTimeFormatter);
LocalDate plus = parse.plus(number, field);
return dateTimeFormatter.format(plus);
}catch (Exception e){
Exception exception;
}
return targetDate;
}
public static String minu(String targetDate,DateTimeFormatter dateTimeFormatter,long number, TemporalUnit field){
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try {
LocalDate parse = LocalDate.parse(targetDate, dateTimeFormatter);
LocalDate minu = parse.minus(number, field);
return dateTimeFormatter.format(minu);
}catch (Exception e){
Exception exception;
}
return targetDate;
}
public static long betweenTwoTime(String startDateStr, String endDateStr,DateTimeFormatter dateTimeFormatter,ChronoUnit field){
if (dateTimeFormatter==null){
dateTimeFormatter=DATE_FORMATTER;
}
try {
Period period = Period.between(LocalDate.parse(startDateStr, dateTimeFormatter), LocalDate.parse(endDateStr, dateTimeFormatter));
if (field == ChronoUnit.YEARS)
return period.getYears();
if (field == ChronoUnit.MONTHS)
return period.getYears() * 12 + period.getMonths();
else
return period.getDays();
}catch (Exception e){
e.printStackTrace();
}
return 0L;
}
public static long periodHours(LocalDateTime startDateTime, LocalDateTime endDateTime) {
return Duration.between(startDateTime, endDateTime).get(ChronoUnit.SECONDS);
}
public static long periodDays(LocalDate startDate, LocalDate endDate) {
return startDate.until(endDate, ChronoUnit.DAYS);
}
public static long periodWeeks(LocalDate startDate, LocalDate endDate) {
return startDate.until(endDate, ChronoUnit.WEEKS);
}
public static long periodMonths(LocalDate startDate, LocalDate endDate) {
return startDate.until(endDate, ChronoUnit.MONTHS);
}
public static long periodYears(LocalDate startDate, LocalDate endDate) {
return startDate.until(endDate, ChronoUnit.YEARS);
}
public static void main(String[] args) {
System.out.println(betweenTwoTime("2000-03-11","2022-03-11",null,ChronoUnit.MONTHS));
}
}
class DataInfo{
private String name;
private String beginTime;
private String endTime;
public DataInfo(String name, String beginTime, String endTime) {
this.name = name;
this.beginTime = beginTime;
this.endTime = endTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBeginTime() {
return beginTime;
}
public void setBeginTime(String beginTime) {
this.beginTime = beginTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
@Override
public String toString() {
return "DataInfo{" +
"name='" + name + ''' +
", beginTime='" + beginTime + ''' +
", endTime='" + endTime + ''' +
'}';
}
}



