时间工具类
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
@Slf4j
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
public static String YYYY = "yyyy";
public static String YYYY_MM = "yyyy-MM";
public static String YYYY_MM_DD = "yyyy-MM-dd";
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
private static final String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
public static Date getNowDate()
{
return new Date();
}
public static String getDate()
{
return dateTimeNow(YYYY_MM_DD);
}
public static String getTime()
{
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
}
public static String dateTimeNow()
{
return dateTimeNow(YYYYMMDDHHMMSS);
}
public static String dateTimeNow(final String format)
{
return parseDateToStr(format, new Date());
}
public static String dateTime(final Date date)
{
return parseDateToStr(YYYY_MM_DD, date);
}
public static String parseDateToStr(final String format, final Date date)
{
return new SimpleDateFormat(format).format(date);
}
public static Date dateTime(final String format, final String ts)
{
try
{
return new SimpleDateFormat(format).parse(ts);
}
catch (ParseException e)
{
throw new RuntimeException(e);
}
}
public static String datePath()
{
Date now = new Date();
return DateFormatUtils.format(now, "yyyy/MM/dd");
}
public static String dateTime()
{
Date now = new Date();
return DateFormatUtils.format(now, "yyyyMMdd");
}
public static Date parseDate(Object str)
{
if (str == null)
{
return null;
}
try
{
return parseDate(str.toString(), parsePatterns);
}
catch (ParseException e)
{
return null;
}
}
public static Date getServerStartDate()
{
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
return new Date(time);
}
public static String getDatePoor(Date endDate, Date nowDate)
{
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
long diff = endDate.getTime() - nowDate.getTime();
long day = diff / nd;
long hour = diff % nd / nh;
long min = diff % nd % nh / nm;
return day + "天" + hour + "小时" + min + "分钟";
}
public String getQuarterByDate(String date) throws ParseException{
if(Objects.equals(date, "") || date == null){
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date datePar = sdf.parse(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(datePar);
String year = String.valueOf(calendar.get(Calendar.YEAR));
int mouth = calendar.get(Calendar.MONTH);
if(mouth<=3){
return year + "年第一季度";
}else if(mouth<=6){
return year + "年第二季度";
}else if(mouth<=9){
return year + "年第三季度";
}else {
return year + "年第四季度";
}
}
public static String getQuarterByDate(Date date){
if(date == null){
return "";
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
String year = String.valueOf(calendar.get(Calendar.YEAR));
int mouth = calendar.get(Calendar.MONTH);
if(mouth<=3){
return "1";
}else if(mouth<=6){
return "2";
}else if(mouth<=9){
return "3";
}else {
return "4";
}
}
public static String getMonthByDate(Date date){
if(date == null){
return "";
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int mouth = calendar.get(Calendar.MONTH);
return mouth+"";
}
public static int compareDate(String date1, String date2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dt1 = df.parse(date1);
Date dt2 = df.parse(date2);
return Long.compare(dt1.getTime(), dt2.getTime());
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
public static int compareDate(String date1, Date date2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dt1 = df.parse(date1);
return Long.compare(dt1.getTime(), date2.getTime());
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
public static int compareDateWithDate(String date1, Date date2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date dt1 = df.parse(date1);
return Long.compare(dt1.getTime(), date2.getTime());
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
public static Date getLastDate(Integer year, Integer month) {
String monthStr = "";
if (month>9){
monthStr = month.toString();
}else {
monthStr = "0" + month;
}
Date date = new Date();
try {
date = new SimpleDateFormat(YYYY_MM).parse(year + "-" + monthStr+"-01");
}catch (Exception e){
log.info("转换失败,msg:{}",e.getMessage());
}
Calendar cale = Calendar.getInstance();
cale.setTime(date);
cale.add(Calendar.MONTH, -1);
cale.set(Calendar.DAY_OF_MONTH, 1);
return cale.getTime();
}
public static Date getNextDate(Integer year, Integer month) {
String monthStr = "";
if (month>9){
monthStr = month.toString();
}else {
monthStr = "0" + month;
}
Date date = new Date();
try {
date = new SimpleDateFormat(YYYY_MM).parse(year + "-" + monthStr+"-01");
}catch (Exception ignored){
}
Calendar cale = Calendar.getInstance();
cale.setTime(date);
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 1);
return cale.getTime();
}
public static int getDiffDays(Date beginDate, Date endDate) {
if (beginDate == null || endDate == null) {
throw new IllegalArgumentException("getDiffDays param is null!");
}
long diff = (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60 * 24);
return Long.valueOf(diff).intValue();
}
}