JAVA时间工具类【DateTimeFormatter】
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;
/**
*
* DateTimeFormatter 线程安全的时间工具类
* JDK 1.8 以上使用
* @author Abner G
* @date 2022/3/10 23:25
*/
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();
/**
* 获取当前时间或者日期,根据传入格式器确定
* @param dateTimeFormatter
* @return
*/
public static String getCurrentLocalDate(DateTimeFormatter dateTimeFormatter){
return dateTimeFormatter.format(LocalDateTime.now());
}
/**
* 获取目标日期的季度,根据传入格式器确定
* @param targetDate
* @param dateTimeFormatter
* @return 2022年第1季度
*/
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();
}
/**
* 获取目标日期的半年度,根据传入格式器确定
* @param targetDate
* @param dateTimeFormatter
* @return
*/
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();
}
/**
* 判断是否为闰年
* @param localDate
* @return
*/
public static boolean isLeapYear(LocalDate localDate){
return localDate.isLeapYear();
}
/**
* 判断是否为闰年
* @param localDate
* @return
*/
public static boolean isLeapYear(String localDate){
return isLeapYear(LocalDateTime.parse(localDate,DATE_FORMATTER).toLocalDate());
}
/**
* 时间格式是否正确,默认yyyy-MM-dd
* @param localData
* @param dateTimeFormatter
* @return
*/
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;
}
}
/**
* 目标日期月份的开始时间
* 默认yyyy-MM-dd
* @param targetDate
* @return
*/
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;
}
/**
* 目标日期月份的结束时间
* 默认yyyy-MM-dd
* @param targetDate
* @param dateTimeFormatter
* @return
*/
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 =null;
if (parse.getMonthValue()==12){
beginDateTime = LocalDate.of(parse.getYear()+1, 1, 1);
}else {
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;
}
/**
* 目标日期年的开始时间
* 默认yyyy-MM-dd
* @param targetDate
* @param dateTimeFormatter
* @return
*/
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;
}
/**
* 目标日期年的结束时间
* 默认yyyy-MM-dd
* @param targetDate
* @param dateTimeFormatter
* @return
*/
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 =null;
if (parse.getMonthValue()==12){
beginDateTime = LocalDate.of(parse.getYear()+1, 1, 1);
}else {
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;
}
/**
* 目标日期季度的开始时间
* 默认yyyy-MM-dd
* @param targetDate
* @param dateTimeFormatter
* @return
*/
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;
}
/**
* 目标日期季度的结束时间
* 默认yyyy-MM-dd
* @param targetDate
* @param dateTimeFormatter
* @return
*/
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;
}
/**
* 目标日期 前/后半年的开始时间
* 默认yyyy-MM-dd
* @param targetDate
* @param dateTimeFormatter
* @return
*/
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;
}
/**
* 获取目标日期的 前/后半年的结束时间
* 默认yyyy-MM-dd
* @param dateTimeFormatter
* @return
*/
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;
}
/**
* 获取目标开始结束时间区间月度List
* 默认yyyy-MM-dd
* @param startDateStr
* @param endDateStr
* @param dateTimeFormatter
* @return
*/
public static List<DataInfo> getMonthDateList(String startDateStr, String endDateStr,DateTimeFormatter dateTimeFormatter){
List<DataInfo> 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;
}
/**
* 获取目标开始结束时间区间季度List
* 默认yyyy-MM-dd
* @param startDateStr
* @param endDateStr
* @param dateTimeFormatter
* @return
*/
public static List<DataInfo> getQuarterDateList(String startDateStr, String endDateStr,DateTimeFormatter dateTimeFormatter){
List<DataInfo> 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;
}
/**
* 获取目标开始结束时间区间半年度List
* 默认yyyy-MM-dd
* @param startDateStr
* @param endDateStr
* @param dateTimeFormatter
* @return
*/
public static List<DataInfo> getHalfYearDateList(String startDateStr, String endDateStr,DateTimeFormatter dateTimeFormatter){
List<DataInfo> 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;
}
/**
* 获取目标开始结束时间区间年度List
* 默认yyyy-MM-dd
* @param startDateStr
* @param endDateStr
* @param dateTimeFormatter
* @return
*/
public static List<DataInfo> getYearDateList(String startDateStr, String endDateStr,DateTimeFormatter dateTimeFormatter){
List<DataInfo> 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;
}
/**
* 日期加上一个数,根据field不同加不同值,
* field为 ChronoUnit.*
* @param targetDate
* @param dateTimeFormatter
* @param number
* @param field
* @return
*/
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;
}
/**
* 日期减去一个数,根据field不同减不同值
* field参数为ChronoUnit.*
* @param targetDate
* @param dateTimeFormatter
* @param number
* @param field
* @return
*/
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;
}
/**
* 获取两个日期的差【年月日】
* field参数为ChronoUnit.*
* @param startDateStr
* @param endDateStr
* @param dateTimeFormatter
* @param field
* @return
*/
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;
}
/**
* 日期相隔秒
* @param startDateTime
* @param endDateTime
* @return
*/
public static long periodHours(LocalDateTime startDateTime, LocalDateTime endDateTime) {
return Duration.between(startDateTime, endDateTime).get(ChronoUnit.SECONDS);
}
/**
* 日期相隔天数
* @param startDate
* @param endDate
* @return
*/
public static long periodDays(LocalDate startDate, LocalDate endDate) {
return startDate.until(endDate, ChronoUnit.DAYS);
}
/**
* 日期相隔周数
* @param startDate
* @param endDate
* @return
*/
public static long periodWeeks(LocalDate startDate, LocalDate endDate) {
return startDate.until(endDate, ChronoUnit.WEEKS);
}
/**
* 日期相隔月数
* @param startDate
* @param endDate
* @return
*/
public static long periodMonths(LocalDate startDate, LocalDate endDate) {
return startDate.until(endDate, ChronoUnit.MONTHS);
}
/**
* 日期相隔年数
* @param startDate
* @param endDate
* @return
*/
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 + '\'' +
'}';
}
}