package cn.tmplat.mes.utils;
import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
/**
-
时间工具类
-
@author zfLiu
*/
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
private static final String APPID=“8035110”;
private static final String APPKEY=“fc3794dac563e258b0b2b2c92296d025”;
private static final String BASEURL=“http://xpapi.wefeed.me”;
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 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”};/**
- 获取当前Date型日期
- @return Date() 当前日期
*/
public static Date getNowDate()
{
return new Date();
}
/**
- 获取当前日期, 默认格式为yyyy-MM-dd
- @return String
*/
public static String getDate()
{
return dateTimeNow(YYYY_MM_DD);
}
public static final String getTime()
{
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
}public static final String dateTimeNow()
{
return dateTimeNow(YYYYMMDDHHMMSS);
}public static final String dateTimeNow(final String format)
{
return parseDateToStr(format, new Date());
}public static final String dateTime(final Date date)
{
return parseDateToStr(YYYY_MM_DD, date);
}public static final String parseDateToStr(final String format, final Date date)
{
return new SimpleDateFormat(format).format(date);
}public static final Date dateTime(final String format, final String ts)
{
try
{
return new SimpleDateFormat(format).parse(ts);
}
catch (ParseException e)
{
throw new RuntimeException(e);
}
}/**
- 日期路径 即年/月/日 如2018/08/08
*/
public static final String datePath()
{
Date now = new Date();
return DateFormatUtils.format(now, “yyyy/MM/dd”);
}
/**
- 日期路径 即年/月/日 如20180808
*/
public static final 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 ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return day + “天” + hour + “小时” + min + “分钟”;
}
/* - 计算两个时间差
/
public static Long getDateDiff(Date endDate, Date nowDate)
{
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
SimpleDateFormat dateFormat=new SimpleDateFormat(“yyyy-MM-dd”);
// Calendar startCalendar = Calendar.getInstance();
// startCalendar.setTime(endDate);
// Calendar endCalendar = Calendar.getInstance();
// endCalendar.setTime(nowDate);
// dateFormat.parse()
LocalDate nowLocalDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate endLocalDate = nowDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println(“now:”+nowLocalDate);
System.out.println(“end:”+endLocalDate);
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = java.sql.Date.valueOf(nowLocalDate).getTime() - java.sql.Date.valueOf(endLocalDate).getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return day ;
}
/* - 根据传入的日期和天数相加获取应还时间
- @param day
- @return
*/
public static Date getDateAdd(int day){
Date date=new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(Calendar.DATE,day); //把日期往后增加一天,整数 往后推,负数往前移动
date=calendar.getTime(); //这个时间就是日期往后推一天的结果
return date;
}
public static Date getDateAdd(Date date,int day){
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(Calendar.DATE,day); //把日期往后增加一天,整数 往后推,负数往前移动
date=calendar.getTime(); //这个时间就是日期往后推一天的结果
String dateType = getDateType(date);
Date result = getDateType(dateType);
return result;
}
public static Date getDateType(String date){
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date parse = null;
try {
parse = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
public static String getDateType(Date date){
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String format = sdf.format(date);
return format;}
/**
- 获取当前时间的12点
- @param
- @return
/
public static Date getFirstDate(){
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
String format = sdf.format(new Date())+" 00:00:00";
Date parse =null;
try {
parse = sdf.parse(format);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
/* - 获取当前时间的12点
- @param
- @return
/
public static Date getDate(String date){
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date parse =null;
try {
parse = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
/* - 获取当前时间的一天日期不含时分秒
- @return
*/
public static Date getEndDate(){
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
String format = sdf.format(new Date())+" 23:59:59";
Date parse =null;
try {
parse = sdf.parse(format);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
/**
- 计算两个时间差多少分钟
*/
public static long getDateMin(String startTime, String endTime) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);
LocalDateTime startDate = LocalDateTime.parse(startTime, formatter);
LocalDateTime endDate = LocalDateTime.parse(endTime, formatter);
// 日期区间
long days = ChronoUnit.DAYS.between(startDate, endDate);
//月
long month = ChronoUnit.MONTHS.between(startDate, endDate);
//年
long year = ChronoUnit.YEARS.between(startDate, endDate);
//时
long hour = ChronoUnit.HOURS.between(startDate, endDate);
//分
long min = ChronoUnit.MINUTES.between(startDate, endDate);
return min;
}
/**
- 计算两个时间差多少小时
*/
// public static long getDateHour(String startTime, String endTime) {
// DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);
// LocalDateTime startDate = LocalDateTime.parse(startTime, formatter);
// LocalDateTime endDate = LocalDateTime.parse(endTime, formatter);
// // 日期区间
// long days = ChronoUnit.DAYS.between(startDate, endDate);
// //月
// long month = ChronoUnit.MONTHS.between(startDate, endDate);
// //年
// long year = ChronoUnit.YEARS.between(startDate, endDate);
// long hour = ChronoUnit.HOURS.between(startDate, endDate);
// long min = ChronoUnit.MINUTES.between(startDate, endDate);
// return hour;
// }
// 根据传入日期 日期格式转换
public static Date getNowDate(Date currentTime) {
if (currentTime!=null){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
Date currentTime_2 = null;
try {
currentTime_2 = formatter.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return currentTime_2;
}else {
return null;
}
}
/**
* 获取今天
* @return
*/
public static String getNowTime() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(new Date());
}
/**
* 获取当前时间年月
* @return
*/
public static String getYearAndMonth() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
return formatter.format(new Date());
}
/**
* 判断日期是不是本月最后一天
*/
public static boolean isLastDayOfMonth(Date date){
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH)
== calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
/**
* 获取下月时间年月
* @return
*/
public static String getGetNextMonth() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Date currentDate = null;
try {
currentDate = sdf.parse(sdf.format(new Date()));
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
return sdf.format(calendar.getTime());
}
/**
* 获取明年
* @return
*/
public static int getForTheNextYear() {
Calendar c = Calendar.getInstance();
c.setTime(new java.util.Date());
int year = c.get(Calendar.YEAR);
c.setTime(new Date());
c.add(Calendar.YEAR, +1);
int C = c.get(Calendar.YEAR);
return C;
}
/**
* 获取今年
* @return
*/
public static int getThisYear() {
Calendar c = Calendar.getInstance();
c.setTime(new java.util.Date());
int year = c.get(Calendar.YEAR);
c.setTime(new Date());
int C = c.get(Calendar.YEAR);
return C;
}
/**
* 获取当前时间不含时分秒
* @return
*/
public static String getTimeYMD(String date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
return formatter.format(formatter.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取当前时间的前一天日期不含时分秒
* @return
*/
public static String getYesterday() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, -1);
Date time = calendar.getTime();
return df.format(time);
}
/**
* 获取当前时间的前一天日期不含时分秒
* @return
*/
// public static String getTomorrowday() {
// SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd”);
// Date date = new Date();
// Calendar calendar = Calendar.getInstance();
// calendar.setTime(date);
// calendar.add(Calendar.DATE, +1);
// Date time = calendar.getTime();
// return df.format(time);
// }
/**
* 当前时间 不含时分秒
* @return
*/
public static String getTomorrowday() {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
date = calendar.getTime();
return DateUtil.format(date, "yyyy-MM-dd");
}
public static void main(String[] args) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, -1);
Date time = calendar.getTime();
System.out.println(df.format(time));
return;
}
/* String a =“YLDX|DX51D+AM|00|1000|1|0|0”;
String[] split = a.split("\|");
StringBuilder sb=new StringBuilder();
if (!split[4].contains(".")){
split[4]= split[4].replaceAll(split[4],split[4]+".0");
}
for (String s : split) {
sb.append(s).append("|");
}
System.out.println(sb.toString().substring(0,sb.length()-1));*/
/SimpleDateFormat dateFormat=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String date =“2020-07-25 00:00:00”;
Date parse = dateFormat.parse(date);
Date a = new Date();
System.out.println( getDateDiff(a,parse));/
// System.out.println( DateUtils.dateTime(DateUtils.YYYY_MM_DD ,DateUtils.getDate()));
}