package org.atlp.utils;
import org.apache.commons.lang3.time.DateUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* @Author: zhangchq
* @CreateTime: 2018-09-19 09:54
* @Decription: 日期转化工具类
*/
public class DateTimeUtil extends DateUtils {
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",
"yyyyMMdd", "yyyyMMdd HH:mm:ss", "yyyy年MM月dd日HH:mm"};
/**
* 日期型字符串转化为日期
*/
public static Date parseDate(Object str) {
if (str== null) {
return null;
}
try {
return parseDate(str.toString(), parsePatterns);
} catch (ParseException e) {
return null;
}
}
/*===========================================日期格式化======================================================*/
// 基本方法
public static String dateFormat2String(Date date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
// 日期转换年
public static String dateFormat2YString(Date date) {
return DateTimeUtil.dateFormat2String(date, "yyyy");
}
// 日期转换年中文
public static String dateFormat2YZhString(Date date) {
return DateTimeUtil.dateFormat2String(date, "yyyy年");
}
// 日期转换年月
public static String dateFormat2YmString(Date date) {
return DateTimeUtil.dateFormat2String(date, "yyyy-MM");
}
// 日期转换年月中文
public static String dateFormat2YmZhString(Date date) {
return DateTimeUtil.dateFormat2String(date, "yyyy年MM月");
}
// 日期转换年月日
public static String dateFormat2YmdString(Date date) {
return DateTimeUtil.dateFormat2String(date, "yyyy-MM-dd");
}
// 日期转换年月日中文
public static String dateFormat2YmdZhString(Date date) {
return DateTimeUtil.dateFormat2String(date, "yyyy年MM月dd日");
}
// 日期转换年月日时
public static String dateFormat2YmdHString(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
return sdf.format(date);
}
// 日期转换年月日时中文
public static String dateFormat2YmdHZhString(Date date) {
return DateTimeUtil.dateFormat2String(date, "yyyy年MM月dd日 HH时");
}
// 日期转换年月日时分
public static String dateFormat2YmdHmString(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return sdf.format(date);
}
// 日期转换年月日时分中文
public static String dateFormat2YmdHmZhString(Date date) {
return DateTimeUtil.dateFormat2String(date, "yyyy年MM月dd日 HH时mm分");
}
// 日期转换为年月日数字
public static String dateFormat2YmdInteger(Date date) {
return DateTimeUtil.dateFormat2String(date, "yyyyMMdd");
}
/*==================================计算日期差=========================================*/
/**
* 计算两个日期之间的差(毫秒)
* @param minDate 前一个日期
* @param maxDate 后一个日期
* @return
*/
public static long workDateBetweenMillion(Date minDate, Date maxDate) {
return maxDate.getTime() - minDate.getTime();
}
/**
* 计算两个日期之间差(小时)
* @param pDate 前一个日期
* @param nDate 后一个日期
* @return
*/
public static int workDateBetweenHour(Date pDate, Date nDate) {
long pTime = pDate.getTime();
long nTime = nDate.getTime();
return (int) ((nTime - pTime)/(1000 * 60 * 60));
}
/**
* 计算两个日期之间的差(天数)
* @param minDate 前一个日期
* @param maxDate 后一个日期
* @return
*/
public static int workDateBetweenDays(Date minDate, Date maxDate) {
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(minDate);
max.setTime(maxDate);
//得到两个日期相差的天数
return ((int) (max.getTime().getTime() / 1000) - (int) (min.getTime().getTime() / 1000)) / 3600 / 24;
}
/**
* 计算两个日期之间的差(月份)
* @param minDate
* @param maxDate
* @return
*/
public static int workDateBetweenMonths(Date minDate, Date maxDate) {
Calendar bef = Calendar.getInstance();
Calendar aft = Calendar.getInstance();
bef.setTime(minDate);
aft.setTime(maxDate);
int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
return Math.abs(month + result) + 1;
}
/**
* 获取前几年的年份select
*/
public static List<String> getPreviousYearList(int size) {
List<String> list = new ArrayList<String>();
// 今年年份
String nowYear = DateTimeUtil.getDateYear(new Date());
Integer currentYear = Integer.parseInt(nowYear);
// 循环装入数组
for (int i = 0; i <= size; i++) {
Integer year = currentYear --;
list.add(year.toString());
}
return list;
}
/**
* 获取后几年的年份select
*/
public static List<String> getAfterYearList(int size) {
List<String> list = new ArrayList<>();
// 今年年份
String nowYear = DateTimeUtil.getDateYear(new Date());
Integer currentYear = Integer.parseInt(nowYear);
// 循环装入数组
for (int i = 0; i <= size; i++) {
Integer year = currentYear ++;
list.add(year.toString());
}
return list;
}
/**
* 获得某年月最后一天
* @param year 年份
* @param month 月份
* @return
*/
public static Date getLastDayOfYearMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR, year);
//设置月份
cal.set(Calendar.MONTH, month- 1);
//获取某月最大天数
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
String lastDayOfMonth = dateFormat2String(cal.getTime(), "yyyy-MM-dd");
return parseDate(lastDayOfMonth);
}
/**
* 获得某年月最后一天
* @param year 年份
* @param month 月份
* @return
*/
public static Date getLastDayByYearAndMonth(int year, int month) {
String[] pingString = new String[]{"31","28","31","30","31","30","31","31","30","31","30","31"};
String[] runString = new String[]{"31","29","31","30","31","30","31","31","30","31","30","31"};
// 判断年份是平、闰年
String lastDay = "", ymd = year+ "-" + (month< 10 ? "0" + month: month+ "");
if (year% 4 != 0 || year% 100 == 0 && year% 400 != 0) {
lastDay = pingString[--month];
} else {
lastDay = runString[--month];
}
ymd = ymd + "-" + lastDay;
return parseDate(ymd);
}
/**
* 获取最后一天
* @param date
* @return
*/
public static Date getLastDayByDate(Date date) {
int year = Integer.parseInt(dateFormat2YString(date));
int month = Integer.parseInt(dateFormat2String(date, "MM"));
return getLastDayByYearAndMonth(year, month);
}
/**
* 某个日期的第一天
* @param date
* @return
*/
public static Date getFirstDayByDate(Date date) {
String ymString = dateFormat2YmString(date);
ymString = ymString + "-01";
return parseDate(ymString);
}
// 截取日期所在年份
public static String getDateYear(Date date) {
return dateFormat2String(date, "yyyy");
}
// 截取日期所在月份
public static String getDateMonth(Date date) {
return dateFormat2String(date, "MM");
}
/**
* 上月
*/
public static Date getPreviousMonth(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.MONTH, -1);
return c.getTime();
}
/**
* 取得某天某点的时间
* @param day 某天日期
* @param hour 某时
* @return
*/
public static Date getDateByDayHour(String day, String hour) {
// 默认今天
if (AtlpUtil.isEmpty(day)) {
day= dateFormat2YmdString(new Date());
}
String date = day+ " " + hour+ ":00:00";
return parseDate(date);
}
/*================================增加天==================================*/
/**
* 某一天增加天
*/
public static Date addDayByDate(Date date, int day) {
Calendar sDate = Calendar.getInstance();
sDate.setTime(date);
sDate.add(Calendar.DATE, day);
return sDate.getTime();
}
/**
* 增加天
*/
public static Date addDayByNow(int day) {
return DateTimeUtil.addDayByDate(new Date(), day);
}
/**
* 增加小时
*/
public static Date addHour(int hour) {
Calendar nowTime = Calendar.getInstance();
nowTime.setTime(new Date());
nowTime.add(Calendar.HOUR_OF_DAY, hour);
return nowTime.getTime();
}
public static void main(String[] args) {
String date = "2019.6.4";
System.out.println(DateTimeUtil.parseDate(date));
}
}