IntelligentRecognition/ah-jjsp-service/.svn/pristine/19/19e02b95d13b952b822eae63307...

669 lines
19 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.securityControl.task.util;
import com.securityControl.common.core.utils.aes.StringHelper;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
/**
* 时间操作类
*
* @author cshu
*/
@SuppressWarnings("deprecation")
public class DateTimeHelper {
/**
* 获取当前时间的年份
*
* @return
*/
public static String getNowYear() {
return format(new Date(), "yyyy");
}
public static String getYMDToYMDHMS(String time) {
String ymdhms = time+ " 00:00:01";
return ymdhms;
}
public static String getYMDToYMDHMS2(String time) {
String ymdhms = time+ " 23:59:59";
return ymdhms;
}
public static String getNextDay(){
Calendar calendar = Calendar.getInstance();
// 对日期进行加1天的操作
calendar.add(Calendar.DAY_OF_MONTH, 1);
// 获取明天的年、月、日
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1; // 注意月份从0开始计算
int day = calendar.get(Calendar.DAY_OF_MONTH);
return year+"-"+month+"-"+day;
}
//普通时间转为UTC
public static String localToUTC(String localTimeStr) {
try {
Date localDate = getLocalSDF().parse(localTimeStr);
return getUTCSDF().format(localDate);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
//UTC转为普通时间
public static String utcToLocal(String utcTimeStr) {
try {
Date date = getUTCSDF().parse(utcTimeStr);
return getLocalSDF().format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
private static SimpleDateFormat getLocalSDF() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
private static SimpleDateFormat getUTCSDF() {
SimpleDateFormat utcSDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
utcSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
return utcSDF;
}
/**
* 获取当前月份
*
* @return
*/
public static String getNowMonths() {
return format(new Date(), "MM");
}
public static String getYear(Date d) {
int year = d.getYear() + 1900;
return year + "";
}
public static String getMonth(Date d) {
int month = d.getMonth() + 1;
return StringHelper.fillPrefixZero(month, 2);
}
public static String getDay() {
return format(new Date(),"dd");
}
public static void main(String[] args) {
getTimesUPDown("2023-10-25 11:58:52");
}
/**
* 時間計算器
* @param time
* @return
*/
public static String getTimesUPDown(String time){
try {
if(StringHelper.isEmpty(time)){
return time;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(time);
// 创建Calendar对象表示当前时间
Calendar calendar = Calendar.getInstance();
// 将Calendar对象设置为当前时间
calendar.setTime(date);
// 将时间减去5分钟
calendar.add(Calendar.MINUTE, -5);
calendar.add(Calendar.SECOND, -13);
// 获取减去5分钟后的时间
Date newTime = calendar.getTime();
String startTime = sdf.format(newTime);
return startTime;
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
/**
* 格式化 时间
*
* @param format
* @return
*/
public static String format(Date d, String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(d);
}
public static String format(String str, String format, String disFormat) {
Date d = parse(str, format);
return format(d, disFormat);
}
/***
* @comments 计算两个时间的时间差
* @param strTime1
* @param strTime2
*/
public static String getTimeDifference(String strTime1, String strTime2) {
//格式日期格式,在此我用的是"2018-01-24 19:49:50"这种格式
//可以更改为自己使用的格式例如yyyy/MM/dd HH:mm:ss 。。。
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date now = df.parse(strTime1);
Date date = df.parse(strTime2);
long l = now.getTime() - date.getTime(); //获取时间差
long day = l / (24 * 60 * 60 * 1000);
long hour = (l / (60 * 60 * 1000) - day * 24);
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
return "" + day + "天" + hour + "小时" + min + "分" + s + "秒";
} catch (Exception e) {
e.printStackTrace();
}
return "计算错误";
}
/**
* 格式化 时间
*
* @param format
* @return
*/
public static String format(Date d, DateFormat format) {
return format.format(d);
}
/**
* 格式化 时间
*
* @param format
* @return Date
* @throws ParseException
*/
public static Date parse(String dateStr, String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
try {
return df.parse(dateStr);
} catch (ParseException e) {
return new Date();
}
}
public static Date parse(String dateStr, String format, Date d) {
try {
SimpleDateFormat df = new SimpleDateFormat(format);
return df.parse(dateStr);
} catch (Exception e) {
return d;
}
}
/**
* 解析时间
*
* @return Date
* @throws ParseException
*/
public static Date parse(String dateStr, DateFormat format)
throws ParseException {
return format.parse(dateStr);
}
/**
* 获取当前时间的上个年月
*
* @return
*/
public static String getPrevMonth() {
return minusMonth(new Date(), 1);
}
public static String getTodayStartTime(){
String date=format(new Date(),"yyyy-MM-dd")+" 00:00:00";
return dateToStamp(date);
}
public static String getTodayEndTime(){
String date=format(new Date(),"yyyy-MM-dd")+" 23:59:59";
return dateToStamp(date);
}
/**
* 获取当前时间的年月
*
* @return
*/
public static String getNowMonth() {
return format(new Date(), "yyyy-MM");
}
public static String getNowDay() {
return format(new Date(),"yyyy-MM-dd");
}
/**
* 当前时间的上一年的年月日
*
* @return
*/
public static String getPrevDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.YEAR, -1);
Date y = c.getTime();
String year = format.format(y);
return year;
}
/**
* 获取当前时间的年月日
*
* @return
*/
public static String getNowDate() {
return format(new Date(), "yyyy-MM-dd");
}
/**
* 获取当前为本年的第几周
*
* @return
*/
public static String getNowWeek() {
Calendar ca = Calendar.getInstance();
ca.setTime(new Date());
int week = ca.get(Calendar.WEEK_OF_YEAR);
return format(new Date(), "yyyy") + "-" + week;
}
public static String getNowDayHMS() {
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
return time;
}
/**
* @return
*/
public static String minusMonth(Date date, int c) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
try {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -c);
return format(cal.getTime(), df);
} catch (Exception e) {
return format(date, df);
}
}
/**
* @param month
* @return
*/
public static String minusMonth(String month, int c) {
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
Date date = parse(month, df);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -c);
return format(cal.getTime(), df);
} catch (Exception e) {
return month;
}
}
/**
* @return
*/
public static Date minus(Date d, int type, int c) {
try {
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.add(type, -c);
return cal.getTime();
} catch (Exception e) {
return new Date();
}
}
/**
* 得到前一个月
*
* @param month
* @return
*/
public static String prevMonth(String month) {
return minusMonth(month, 1);
}
/**
* 得到前一个月
*
* @param month
* @return
*/
public static String prevMonth(String month, int c) {
return minusMonth(month, c);
}
public static float duration(String startTime, String stopTime) {
Date sd = DateTimeHelper.parse(startTime, "yyyy-MM-dd HH:mm");
long sl = sd.getTime();
Date ed = DateTimeHelper.parse(stopTime, "yyyy-MM-dd HH:mm");
long el = ed.getTime();
float l = el - sl;
if (l <= 0) {
l = 0;
}
l = l / (1000 * 60);
return l;
}
public static String getStartYearMonthBySeason(String year, int season) {
switch (season) {
case 1:
return getYearMonth(year, "01");
case 2:
return getYearMonth(year, "04");
case 3:
return getYearMonth(year, "07");
case 4:
return getYearMonth(year, "10");
default:
return getYearMonth(year, "01");
}
}
private static String getYearMonth(String year, String month) {
return year + "-" + month;
}
public static String getEndYearMonthBySeason(String year, int season) {
switch (season) {
case 1:
return getYearMonth(year, "03");
case 2:
return getYearMonth(year, "06");
case 3:
return getYearMonth(year, "09");
case 4:
return getYearMonth(year, "12");
default:
return getYearMonth(year, "03");
}
}
/**
* 获取当年初始时间
*
* @return
*/
public static String getYearStart() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.DATE, 1);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
return format.format(calendar.getTime());
}
/**
* 获取当前日期周一
*
* @return
*/
public static String getMonday() {
Calendar cal = Calendar.getInstance(Locale.CHINA);
// 如果当前时间是星期天,则向上移动一天,再取本周的星期一,老外用周日到周六为一周,向前移动一天,则是中国人的本周
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
cal.add(Calendar.DATE, -1);
}
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
return sdf1.format(cal.getTime());
}
/**
* 获取当前时间的年月
*
* @return
*/
public static String getYmDate() {
return format(new Date(), "yyyy-MM");
}
/**
* 将时间转换为时间戳
*/
public static String dateToStamp(String time) {
String stap;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = simpleDateFormat.parse(time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long ts = date.getTime() / 1000;//获取时间的时间戳
stap = String.valueOf(ts);
return stap;
}
/**
* 将时间戳转换为时间
*/
public static String stampToDate(String stap) {
String time;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(stap);
Date date = new Date(lt * 1000);
time = simpleDateFormat.format(date);
return time;
}
public static List<String> findDates(String stime, String etime)
throws ParseException {
List<String> allDate = new ArrayList();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dBegin = sdf.parse(stime);
Date dEnd = sdf.parse(etime);
allDate.add(sdf.format(dBegin));
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
while (dEnd.after(calBegin.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
allDate.add(sdf.format(calBegin.getTime()));
}
return allDate;
}
public static String getDate(String date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dates = DateTimeHelper.parse(date, sdf);
return DateTimeHelper.format(dates, "yyyy-MM-dd");
}
public static int dateCompare(Date date1, Date date2) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String dateFirst = dateFormat.format(date1);
String dateLast = dateFormat.format(date2);
int dateFirstIntVal = Integer.parseInt(dateFirst);
int dateLastIntVal = Integer.parseInt(dateLast);
if (dateFirstIntVal > dateLastIntVal) {
return 1;
} else if (dateFirstIntVal < dateLastIntVal) {
return -1;
}else if (dateFirstIntVal == dateLastIntVal){
return 2;
}
return 0;
}
/**
* 传入两个时间参数,返回需要的值
* @param date1
* @param date2
* @return
*/
public static int dateCompare(String date1, String date2){
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date parse1 = sdf.parse(date1);
Date parse2 = sdf.parse(date2);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String dateFirst = dateFormat.format(parse1);
String dateLast = dateFormat.format(parse2);
int dateFirstIntVal = Integer.parseInt(dateFirst);
int dateLastIntVal = Integer.parseInt(dateLast);
if (dateFirstIntVal > dateLastIntVal) {
return 1;
} else if (dateFirstIntVal < dateLastIntVal) {
return -1;
} else {
return 0;
}
}catch (ParseException e){
//假设转换错误,按照第一个时间来
return 1;
}
}
/**
* 传入两个时间参数,返回需要的值
* @param date1
* @param date2
* @param i>0 true 返回大的 false返回小的
* @return
*/
public static String getNeedDate(String date1, String date2,int i){
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date parse1 = sdf.parse(date1);
Date parse2 = sdf.parse(date2);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String dateFirst = dateFormat.format(parse1);
String dateLast = dateFormat.format(parse2);
int dateFirstIntVal = Integer.parseInt(dateFirst);
int dateLastIntVal = Integer.parseInt(dateLast);
if (dateFirstIntVal > dateLastIntVal && i > 0) {
return date1;
} else if (dateFirstIntVal < dateLastIntVal && i>0) {
return date2;
} else if (dateFirstIntVal < dateLastIntVal && i<0) {
return date1;
} else if (dateFirstIntVal > dateLastIntVal && i < 0) {
return date2;
} else {
return date1;
}
}catch (ParseException e){
//假设转换错误,按照第一个时间来
return date1;
}
}
/**
* 计算两个时间点之间的天数
*/
public static long getBetweenDay(LocalDate start, LocalDate end) {
return end.toEpochDay() - start.toEpochDay();
}
/**
* 计算两个时间点之间的天数
*/
public static long getBetweenDay(String start, String end) {
LocalDate date1 = LocalDate.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDate date2 = LocalDate.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
return date2.toEpochDay() - date1.toEpochDay();
}
public static String getNowTime() {
return format(new Date(), "yyyy-MM-dd HH:mm:ss");
}
/**
* 获取开始时间时间戳
* @param time
* @return
*/
public static String getStartTime(String time){
String date=time+" 00:00:00";
return dateToStamp(date);
}
/**
* 获取解释时间时间戳
* @param time
* @return
*/
public static String getEndTime(String time){
String date=time+" 23:59:59";
return dateToStamp(date);
}
/**
* 获取当前时间毫秒
* @return
*/
public static String getNowTimestamp() {
return String.valueOf(System.currentTimeMillis());
}
/**
* 根据指定日期获取上月日期
* @param
* @return
*/
public static String getLastDate(String time) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(time);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -1);
return sdf.format(cal.getTime());
}
}