This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
galaxy-k18-galaxy-service/src/main/java/com/nis/util/DateUtils.java

552 lines
16 KiB
Java
Raw Normal View History

2017-12-19 14:55:52 +08:00
/**
* Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
*/
package com.nis.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
2017-12-19 14:55:52 +08:00
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
2017-12-19 14:55:52 +08:00
import java.util.Map;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.log4j.Logger;
2018-12-20 17:32:09 +08:00
import com.zdjizhi.utils.StringUtil;
2017-12-19 14:55:52 +08:00
/**
* 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
2018-12-23 20:46:19 +08:00
*
2017-12-19 14:55:52 +08:00
* @author ThinkGem
* @version 2014-4-15
*/
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
public static final Logger logger = Logger.getLogger(DateUtils.class);
2018-12-23 20:46:19 +08:00
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" };
2017-12-19 14:55:52 +08:00
/**
* 得到当前日期字符串 格式yyyy-MM-dd
*/
public static String getDate() {
return getDate("yyyy-MM-dd");
}
2018-12-23 20:46:19 +08:00
2017-12-19 14:55:52 +08:00
/**
* 得到当前日期字符串 格式yyyy-MM-dd pattern可以为"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String getDate(String pattern) {
return DateFormatUtils.format(new Date(), pattern);
}
2018-12-23 20:46:19 +08:00
2017-12-19 14:55:52 +08:00
/**
* 得到日期字符串 默认格式yyyy-MM-dd pattern可以为"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String formatDate(Date date, Object... pattern) {
String formatDate = null;
if (pattern != null && pattern.length > 0) {
formatDate = DateFormatUtils.format(date, pattern[0].toString());
} else {
formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
}
return formatDate;
}
2018-12-23 20:46:19 +08:00
2017-12-19 14:55:52 +08:00
/**
* 得到日期时间字符串转换格式yyyy-MM-dd HH:mm:ss
*/
public static String formatDateTime(Date date) {
return formatDate(date, "yyyy-MM-dd HH:mm:ss");
}
/**
* 得到当前时间字符串 格式HH:mm:ss
*/
public static String getTime() {
return formatDate(new Date(), "HH:mm:ss");
}
/**
* 得到当前日期和时间字符串 格式yyyy-MM-dd HH:mm:ss
*/
public static String getDateTime() {
return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
}
/**
* 得到当前年份字符串 格式yyyy
*/
public static String getYear() {
return formatDate(new Date(), "yyyy");
}
/**
* 得到当前月份字符串 格式MM
*/
public static String getMonth() {
return formatDate(new Date(), "MM");
}
/**
* 得到当天字符串 格式dd
*/
public static String getDay() {
return formatDate(new Date(), "dd");
}
/**
* 得到当前星期字符串 格式E星期几
*/
public static String getWeek() {
return formatDate(new Date(), "E");
}
2018-12-23 20:46:19 +08:00
2017-12-19 14:55:52 +08:00
/**
2018-12-23 20:46:19 +08:00
* 日期型字符串转化为日期 格式 { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
* "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy.MM.dd",
* "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
2017-12-19 14:55:52 +08:00
*/
public static Date parseDate(Object str) {
2018-12-23 20:46:19 +08:00
if (str == null) {
2017-12-19 14:55:52 +08:00
return null;
}
try {
return parseDate(str.toString(), parsePatterns);
} catch (ParseException e) {
return null;
}
}
/**
* 获取过去的天数
2018-12-23 20:46:19 +08:00
*
2017-12-19 14:55:52 +08:00
* @param date
* @return
*/
public static long pastDays(Date date) {
2018-12-23 20:46:19 +08:00
long t = new Date().getTime() - date.getTime();
return t / (24 * 60 * 60 * 1000);
2017-12-19 14:55:52 +08:00
}
/**
* 获取过去的小时
2018-12-23 20:46:19 +08:00
*
2017-12-19 14:55:52 +08:00
* @param date
* @return
*/
public static long pastHour(Date date) {
2018-12-23 20:46:19 +08:00
long t = new Date().getTime() - date.getTime();
return t / (60 * 60 * 1000);
2017-12-19 14:55:52 +08:00
}
2018-12-23 20:46:19 +08:00
2017-12-19 14:55:52 +08:00
/**
* 获取过去的分钟
2018-12-23 20:46:19 +08:00
*
2017-12-19 14:55:52 +08:00
* @param date
* @return
*/
public static long pastMinutes(Date date) {
2018-12-23 20:46:19 +08:00
long t = new Date().getTime() - date.getTime();
return t / (60 * 1000);
2017-12-19 14:55:52 +08:00
}
2018-12-23 20:46:19 +08:00
2017-12-19 14:55:52 +08:00
/**
* 转换为时间,::.毫秒
2018-12-23 20:46:19 +08:00
*
2017-12-19 14:55:52 +08:00
* @param timeMillis
* @return
*/
2018-12-23 20:46:19 +08:00
public static String formatDateTime(long timeMillis) {
long day = timeMillis / (24 * 60 * 60 * 1000);
long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
}
2017-12-19 14:55:52 +08:00
/**
* 获取两个日期之间的天数
*
* @param before
* @param after
* @return
*/
public static double getDistanceOfTwoDate(Date before, Date after) {
long beforeTime = before.getTime();
long afterTime = after.getTime();
return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
}
2018-12-23 20:46:19 +08:00
2017-12-19 14:55:52 +08:00
/**
*
* @Description: 按类型获取默认时间
* @author (zdx)
* @date 2018年8月28日 下午4:53:40
* @param startTime
* @param endTime
* @param localLen
* @param type
* @return
* @throws Exception
2017-12-19 14:55:52 +08:00
*/
2018-12-23 20:46:19 +08:00
public static Map<String, String> getLocalTime(String startTime, String endTime, Long localLen, String type)
throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Map<String, String> timeMap = new HashMap<String, String>();
if (StringUtil.isEmpty(type)) {
type = "minute";
2017-12-19 14:55:52 +08:00
}
2018-12-23 20:46:19 +08:00
// 实时报表统计默认查询最近5分钟
if ("minute".equals(type) && startTime == null && endTime == null) {
Calendar cal = Calendar.getInstance();
timeMap.put("endTime", sdf.format(cal.getTime()));
2018-12-23 20:46:19 +08:00
if (localLen != null) {
cal.add(Calendar.MINUTE, -localLen.intValue());
2018-12-23 20:46:19 +08:00
} else {
cal.add(Calendar.MINUTE, -5);
}
timeMap.put("startTime", sdf.format(cal.getTime()));
2018-12-23 20:46:19 +08:00
logger.info("实时报表统计默认开始时间条件:" + timeMap.get("startTime"));
logger.info("默认结束时间条件:" + timeMap.get("endTime"));
} else if ("hour".equals(type) && startTime == null && endTime == null) {// 小时报表默认查询最近一小时的数据
Calendar cal = Calendar.getInstance();
timeMap.put("endTime", sdf.format(cal.getTime()));
cal.add(Calendar.HOUR_OF_DAY, -1);
timeMap.put("startTime", sdf.format(cal.getTime()));
2018-12-23 20:46:19 +08:00
logger.info("小时报默认开始时间条件:" + timeMap.get("startTime"));
logger.info("小时报默认结束时间条件:" + timeMap.get("endTime"));
} else if ("daily".equals(type) && startTime == null && endTime == null) {// 日报表默认查询最近一天的数据
Calendar cal = Calendar.getInstance();
timeMap.put("endTime", sdf.format(cal.getTime()));
cal.add(Calendar.DAY_OF_YEAR, -1);
timeMap.put("startTime", sdf.format(cal.getTime()));
2018-12-23 20:46:19 +08:00
logger.info("日报默认开始时间条件:" + timeMap.get("startTime"));
logger.info("日报默认结束时间条件:" + timeMap.get("endTime"));
} else if ("month".equals(type) && startTime == null && endTime == null) {// 月报表默认查询最近一月的数据
Calendar cal = Calendar.getInstance();
timeMap.put("endTime", sdf.format(cal.getTime()));
2018-12-23 20:46:19 +08:00
cal.add(Calendar.MONTH, -1);
2017-12-19 14:55:52 +08:00
timeMap.put("startTime", sdf.format(cal.getTime()));
2018-12-23 20:46:19 +08:00
logger.info("月报默认开始时间条件:" + timeMap.get("startTime"));
logger.info("月报默认结束时间条件:" + timeMap.get("endTime"));
2017-12-19 14:55:52 +08:00
}
return timeMap;
2017-12-19 14:55:52 +08:00
}
2018-12-23 20:46:19 +08:00
2018-12-20 17:32:09 +08:00
/**
2018-12-23 20:46:19 +08:00
* 将短时间格式字符串转换为时间 yyyy-MM-dd
*
* @param strDate
* @return
*/
2018-12-20 17:32:09 +08:00
/**
* 将短时间格式字符串转换为指定时间
*
* @param strDate
2018-12-23 20:46:19 +08:00
* @param dateFormat
* "yyyy-MM-dd HH:mm:ss" 此参数为空默认为"yyyy-MM-dd"
2018-12-20 17:32:09 +08:00
* @return
2018-12-23 20:46:19 +08:00
* @throws ParseException
2018-12-20 17:32:09 +08:00
*/
2018-12-23 20:46:19 +08:00
public static Date strToDate(String strDate, String dateFormat) throws ParseException {
if (dateFormat == null || "".equals(dateFormat)) {
dateFormat = "yyyy-MM-dd";
}
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Date strtodate = sdf.parse(strDate);
return strtodate;
2018-12-20 17:32:09 +08:00
}
2018-12-23 20:46:19 +08:00
2017-12-19 14:55:52 +08:00
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
2018-12-23 20:46:19 +08:00
// System.out.println(formatDate(parseDate("2010/3/6")));
// System.out.println(getDate("yyyy年MM月dd日 E"));
// long time = new Date().getTime()-parseDate("2012-11-19").getTime();
// System.out.println(time/(24*60*60*1000));
/* Date date = new Date();
2018-12-23 20:46:19 +08:00
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, date.getMonth() - 2);
2017-12-19 14:55:52 +08:00
cal.set(Calendar.DAY_OF_MONTH, 1);
2018-12-23 20:46:19 +08:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// timeMap.put("startTime", sdf.format(cal2.getTime()));
// timeMap.put("endTime", sdf.format(cal.getTime()));
logger.info("月报默认开始时间条件:" + sdf.format(cal.getTime()));*/
String beginDate = DateUtils.getSpecifiedDayBefore("2018-12-23 10:10:10");
System.out.println(beginDate);
String bhour = DateUtils.getSpecifiedHourBefore("2018-12-23 10:10:10");
System.out.println(bhour);
String bhour1 = DateUtils.getSpecifiedHourBefore("2018-12-23 00:10:10");
System.out.println(bhour1);
2018-12-23 20:46:19 +08:00
}
/**
* 获得指定日期的前一小时
*
* @param specifiedDay
* @return
* @throws Exception
*/
public static String getSpecifiedHourBefore(String specifiedDay) {
// SimpleDateFormat simpleDateFormat = new
// SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int hour = c.get(Calendar.HOUR_OF_DAY);
c.set(Calendar.HOUR_OF_DAY, hour - 1);
String dayBefore = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
return dayBefore;
}
/**
* 获得指定日期的后一小时
*
* @param specifiedDay
* @return
*/
public static String getSpecifiedHourAfter(String specifiedDay) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int hour = c.get(Calendar.HOUR_OF_DAY);
c.set(Calendar.HOUR_OF_DAY, hour + 1);
2018-12-23 20:46:19 +08:00
String dayAfter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
return dayAfter;
}
2018-12-23 20:46:19 +08:00
/**
* 获得指定日期的前一天
*
* @param specifiedDay
* @return
* @throws Exception
*/
public static String getSpecifiedDayBefore(String specifiedDay) {
// SimpleDateFormat simpleDateFormat = new
// SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(specifiedDay);
2018-12-23 20:46:19 +08:00
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day - 1);
String dayBefore = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
2018-12-23 20:46:19 +08:00
return dayBefore;
}
/**
* 获得指定日期的后一天
*
* @param specifiedDay
* @return
*/
public static String getSpecifiedDayAfter(String specifiedDay) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(specifiedDay);
2018-12-23 20:46:19 +08:00
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day + 1);
String dayAfter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
2018-12-23 20:46:19 +08:00
return dayAfter;
2017-12-19 14:55:52 +08:00
}
/**
* 获取某个时间的最近五分钟,strDate和numDate传入一个即可
*
* @param date 格式yyyy-MM-dd HH:mm:ss
* @param numDate 时间戳
* @return
*/
public static Calendar getRecentFiveMinute(String strDate, Long numDate) {
Calendar instance = Calendar.getInstance();
if (strDate != null) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
instance.setTime(sdf.parse(strDate));
} catch (ParseException e) {
e.printStackTrace();
}
} else if (numDate != null) {
instance.setTimeInMillis(numDate);
}
int oldMinute = instance.get(Calendar.MINUTE);
char[] charArray = String.valueOf(oldMinute).toCharArray();
if (charArray[charArray.length - 1] - '0' >= 5) {
charArray[charArray.length - 1] = '5';
} else {
charArray[charArray.length - 1] = '0';
}
instance.set(Calendar.SECOND, 0);
instance.set(Calendar.MINUTE, Integer.valueOf(String.valueOf(charArray)));
return instance;
}
/**
* 获取两个时间的五分钟间隔,小时间隔,天间隔,月间隔
*
* @param beginDate 开始时间(包含开始时间)
* @param endDate 结束时间(不包含结束时间)
* @param type 0:五分钟间隔,1:小时间隔,2:天间隔,3:月间隔,4:年间隔
* @return 返回时间戳集合
*/
public static List<Long> getTimeInterval(String beginDate, String endDate, int type) {
Calendar beginCal = getRecentFiveMinute(beginDate,null);// 将时间转为最近的五分钟
Calendar endCal = getRecentFiveMinute(endDate,null);
List<Long> list = new ArrayList<>();
boolean bool = true;
switch (type) {
case 1:// 获取小时间隔
beginCal.set(Calendar.MINUTE, 0);
beginCal.set(Calendar.HOUR_OF_DAY, beginCal.get(Calendar.HOUR_OF_DAY));
endCal.set(Calendar.MINUTE, 0);
endCal.set(Calendar.HOUR_OF_DAY, endCal.get(Calendar.HOUR_OF_DAY));
break;
case 2:// 获取天间隔
beginCal.set(Calendar.MINUTE, 0);
beginCal.set(Calendar.HOUR_OF_DAY, 0);
beginCal.set(Calendar.DAY_OF_MONTH, beginCal.get(Calendar.DAY_OF_MONTH));
endCal.set(Calendar.MINUTE, 0);
endCal.set(Calendar.HOUR_OF_DAY, 0);
endCal.set(Calendar.DAY_OF_MONTH, endCal.get(Calendar.DAY_OF_MONTH));
break;
case 3:// 获取月间隔
beginCal.set(Calendar.MINUTE, 0);
beginCal.set(Calendar.HOUR_OF_DAY, 0);
beginCal.set(Calendar.DAY_OF_MONTH, 1);
beginCal.set(Calendar.MONTH, beginCal.get(Calendar.MONTH));
endCal.set(Calendar.MINUTE, 0);
endCal.set(Calendar.HOUR_OF_DAY, 0);
endCal.set(Calendar.DAY_OF_MONTH, 1);
endCal.set(Calendar.MONTH, endCal.get(Calendar.MONTH));
break;
case 4:// 获取年间隔
beginCal.set(Calendar.MINUTE, 0);
beginCal.set(Calendar.HOUR_OF_DAY, 0);
beginCal.set(Calendar.DAY_OF_MONTH, 1);
beginCal.set(Calendar.MONTH, 0);
beginCal.set(Calendar.YEAR, beginCal.get(Calendar.YEAR));
endCal.set(Calendar.MINUTE, 0);
endCal.set(Calendar.HOUR_OF_DAY, 0);
endCal.set(Calendar.DAY_OF_MONTH, 1);
endCal.set(Calendar.MONTH, 0);
endCal.set(Calendar.YEAR, endCal.get(Calendar.YEAR));
break;
}
list.add(beginCal.getTimeInMillis());
long endTime = endCal.getTimeInMillis();
while (bool) {
switch (type) {
case 1:// 获取小时间隔
beginCal.add(Calendar.HOUR_OF_DAY, 1);
break;
case 2:// 获取天间隔
beginCal.add(Calendar.DAY_OF_MONTH, 1);
break;
case 3:// 获取月间隔
beginCal.add(Calendar.MONTH, 1);
break;
case 4:// 获取年间隔
beginCal.add(Calendar.YEAR, 1);
break;
default:// 默认获取五分钟间隔
beginCal.add(Calendar.MINUTE, 5);
}
long timeInMillis = beginCal.getTimeInMillis();
if (timeInMillis < endTime) {
list.add(timeInMillis);
} else {
bool = false;
}
}
return list;
}
/**
* 获取一个时间对应的小时,,,年等粒度
*
* @param time 需要转化的时间
* @param type 0:五分钟粒度,1:小时粒度,2:天粒度,3:月粒度,4:年粒度
* @return
*/
public static Long getTimeByType(Long time, int type) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
switch (type) {
case 0:// 获取小时间隔
calendar=getRecentFiveMinute(null,time);
break;
case 1:// 获取小时间隔
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
break;
case 2:// 获取天间隔
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
break;
case 3:// 获取月间隔
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
break;
case 4:// 获取年间隔
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
break;
}
return calendar.getTimeInMillis();
}
2017-12-19 14:55:52 +08:00
}