package com.hzks.common.utils;
import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.time.DateFormatUtils;
/**
* 时间工具类
*
* @author hzks
*/
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 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 getMillis(final Date date)
{
return parseDateToStr(YYYYMMDDHHMMSS, 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 + "分钟";
}
/**
* 两个日期相差天数
* @param dateStart 开始日期
* @param dateEnd 结束日期
* @return 相差天数
*/
public static int getDiscrepantDays(Date dateStart, Date dateEnd) {
return (int) ((dateEnd.getTime() - dateStart.getTime()) / 1000 / 60 / 60 / 24);
}
/**
* 日期往后推固定天数
* @param date 日期
* @param day 天数
* @return 结果
*/
public static Date addDays(Date date, int day) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
//把日期往后增加day天.整数往后推,负数往前移动
calendar.add(Calendar.DATE,day);
//这个时间就是日期往后推一天的结果
return calendar.getTime();
}
/**
* 取得当月天数
* */
public static int getCurrentMonthLastDay()
{
Calendar a = Calendar.getInstance();
//把日期设置为当月第一天
a.set(Calendar.DATE, 1);
//日期回滚一天,也就是最后一天
a.roll(Calendar.DATE, -1);
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
/**
* 比较时间大小
* @param beginTime
* @param endTime
*/
public static boolean compareDate(String beginTime, String endTime) {
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date sd1=df.parse(beginTime);
Date sd2=df.parse(endTime);
return sd1.before(sd2);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
/**
* 一月之前
*
* @param date
* @return
*/
public static Date getYesterMonthday(Date date) {
Calendar yesterday = Calendar.getInstance();
yesterday.setTime(date);
yesterday.add(Calendar.MONTH, -1);
return yesterday.getTime();
}
// 一月之后
public static Date getNextDate(Date date) {
Calendar today = Calendar.getInstance();
today.setTime(date);
today.add(Calendar.DATE, 1);
return today.getTime();
}
/**
* 获取上个月月份
*/
public static String getLastMonth() {
LocalDate today = LocalDate.now();
today = today.minusMonths(1);
DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM");
return formatters.format(today);
}
/**
* 判断过期日期 是否 过期
* @param endTime 过期日期
* @param currentTime 当前日期
*/
public static boolean compareDate(Date endTime, Date currentTime) {
// DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getTime())
return endTime.compareTo(currentTime)<0;
}
/**
* 获取某月的最后一天
*
*/
public static String getLastDayOfMonth(Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
//设置年份
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);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
public static String DateToString(Date dDate, int nFormat) {
String resString = "";
try {
String sFormat = "";
if(nFormat == 1) {
sFormat = "yyyy-MM-dd";
} else if(nFormat == 2) {
sFormat = "yyyy-MM-dd HH:mm:ss";
} else if(nFormat == 3) {
sFormat = "yyyy-MM-dd EEE";
} else if(nFormat == 4) {
sFormat = "yyyy-MM-dd HH:mm";
} else if(nFormat == 5) {
sFormat = "yyyy-MM-dd HH:mm:ss.sss";
} else if(nFormat == 6) {
sFormat = "yyyy-MM-ddTHH:mm:ss";
} else if(nFormat == 7) {
sFormat = "yyyy-MM-dd'T'HH:mm:ssXXX";
}else {
sFormat = "yyyy-MM-dd";
}
SimpleDateFormat simDateFormat = new SimpleDateFormat(sFormat);
resString = simDateFormat.format(dDate);
} catch (Exception e) {
return resString;
}
return resString;
}
/**
* 获取之前一整月的的 开始和结束日期 如( queryDate >= 当前日期 ?获取上个月的 :获取当前的 )
* @param queryDate 要获取的月份日期 yyyy-MM
* @return 开始和结束日期 如 :( 2021-8-1 到 2021-8-31)
*/
public static Map<String,String> getBeforeStartEndDate(String queryDate){
Map map=new HashMap();
String startTime = "";
String endTime = "";
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM");
String dateTime =simpleDateFormat.format(new Date());
try {
SimpleDateFormat d=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date thisTime= simpleDateFormat.parse(dateTime);
Date startDate= simpleDateFormat.parse(queryDate);
if (startDate.before(thisTime) ){
startTime=d.format(startDate);
// 获取月份最后一天 的 日期
endTime=getLastDayOfMonth(startDate);
}else {
// 获取当前日期的上个月日期
startDate= simpleDateFormat.parse(getLastMonth());
startTime= d.format(startDate);
endTime= getLastDayOfMonth(startDate);
System.out.println("当月工资未到发放时间,默认当前日期的上个月!");
}
} catch (ParseException e) {
e.printStackTrace();
}
map.put("startTime",startTime);
map.put("endTime",endTime);
return map;
}
/**
* 一天中的天数
*/
public static long millionSecondsOfDay = 86400000;
/**
* 得到两个日期之间的天数,两头不算,取出数据后,可以根据需要再加
*
* @param date1
* @param date2
* @return
* @author hsshao
*/
public static int getDay(Date date1, Date date2) {
Long d2 = date2.getTime();
Long d1 = date1.getTime();
return (int) ((d2 - d1) / millionSecondsOfDay);
}
/**
*@Author ht
*@Date 2020/12/2 15:31
* 当前时间前多少天
*/
public static String getPastDate(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String result = format.format(today);
return result;
}
/**
* @Date 10:32
* @author Administrator
* @Description 获取当前月的第一天
*/
public static Date getFirstDateOfCurrentDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
// return format.format(c.getTime());
return c.getTime();
}
/**
* 一个月平均七天的日期,不满七天 的,按剩余的日期计算
* 列如:2021-08-01 - 2021-08-07 2021-08-08 - 2021-08-15... 以此类推
*/
public static List<Map<String,String>> getHebdomadList() throws ParseException {
List<Map<String,String>> list=new ArrayList<>();
String nyr= null;
SimpleDateFormat d=new SimpleDateFormat("yyyy-MM-dd");
Date ds=d.parse(d.format(new Date()));
int dayCount=(getDay(getFirstDateOfCurrentDate(), ds)+1);
int dayAverage=dayCount%7;
if (dayAverage==0){
dayAverage = dayCount/7;
}else {
dayAverage = (dayCount/7)+1;
}
for(int i=1;i<=dayAverage;i++){
Map<String,String> map=new HashMap<>();
if(i<dayAverage){
int day_1=(dayCount - 7*i)+1;
if (nyr == null){
nyr=new SimpleDateFormat("yyyy-MM-dd").format(getFirstDateOfCurrentDate());
System.out.println(nyr+" ~ "+getPastDate(day_1));
map.put("startTime",nyr);
map.put("endTime",getPastDate(day_1));
nyr = getPastDate(day_1);
}else {
System.out.println(nyr+" ~ "+getPastDate(day_1));
map.put("startTime",nyr);
map.put("endTime",getPastDate(day_1));
nyr = getPastDate(day_1);
}
}else {
System.out.println(getPastDate(0));
map.put("startTime",nyr);
map.put("endTime",getPastDate(0));
}
list.add(map);
}
return list;
}
public static String dateTo0String(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(date).trim();
}
/**
* 计算当前月有多少自然日、有多少工作日、有几周
* Map<String, Integer> map = getMonthInfo(Calendar.getInstance());
* System.err.println("------------" + map.get("year") + "年" + map.get("month") + "月-------------");
* System.err.println("总天数:" + map.get("daysAmount"));
* System.err.println("工作日天数:" + map.get("workDaysAmount"));
* System.err.println("周数:" + map.get("weeksAmount"));
*/
private static Map getMonthInfo(Calendar calendar) {
Map<Object, Integer> map = new HashedMap();
int workDays = 0;
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
try {
calendar.set(Calendar.DATE, 1);//从每月1号开始
for (int i = 0; i < days; i++) {
int day = calendar.get(Calendar.DAY_OF_WEEK);
if (!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)) {
workDays++;
}
calendar.add(Calendar.DATE, 1);
}
map.put("workDaysAmount", workDays);//工作日
map.put("year", calendar.get(Calendar.YEAR));//实时年份
map.put("month", calendar.get(Calendar.MONTH));//实时月份
map.put("daysAmount", days);//自然日
map.put("weeksAmount", calendar.getActualMaximum(Calendar.WEEK_OF_MONTH));//周
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
public static void main(String[] args) throws ParseException {
Map map=new HashMap();
/*获取当前月的所有日期*/
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
// 获取当前月
int month = calendar.get(Calendar.MONTH) + 1;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(int i=1;i<=getCurrentMonthLastDay();i++) {
String startTime = sdf.format(sdf.parse((year + "-" + month + "-" + i + " 00:00:00")));
String endTime = sdf.format(sdf.parse(year + "-" + month + "-" + i + " 23:59:59"));
if (dateTo0String(new Date()).equals(new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("yyyy-MM-dd").parse((year + "-" + month + "-" + i))))) {
System.out.println("111111");
break;
}else {
System.out.println("2222222");
}
}
}
}
|