package me.zhengjie.modules.quartz.utils;
|
|
import io.swagger.models.auth.In;
|
import me.zhengjie.utils.StringUtils;
|
|
import java.sql.Timestamp;
|
import java.text.DateFormat;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
public class DateTimeUtil {
|
|
public static final String DIVIDE_LINE = "-";
|
public static final String DIVIDE_COLON = ":";
|
|
public static final long SECOND = 1000;
|
public static final long MINUTE = 60 * SECOND;
|
public static final long HOUR = 60 * MINUTE;
|
public static final long DAY = 24 * HOUR;
|
public static final long WEEK = 7 * DAY;
|
public static final String YEARFORMAT = "yyyy";
|
public static final String MONTHFORMAT = "yyyy-MM";
|
public static final String SHORTFORMAT = "yyyy-MM-dd";
|
public static final String SHORTFORMATNOSPIT = "yyyyMMdd";
|
public static final String LONGFORMATNOSPIT = "yyyyMMddHHmmss";
|
public static final String LONGFORMAT = "yyyy-MM-dd HH:mm:ss";
|
public static final String SUPERLONGFORMAT = "yyyy-MM-dd HH:mm:ss sss";
|
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
public static final int FIELD_YEAR = Calendar.YEAR;
|
public static final int FIELD_MONTH = Calendar.MONTH;
|
public static final int FIELD_DATE = Calendar.DATE;
|
|
/**
|
* 时间戳格式化 yyyy-MM-dd HH:mm:ss
|
*/
|
public static String getTimestampString(Date date) {
|
return sdf.format(date).toString();
|
}
|
|
/**
|
* 系统当前时间戳
|
*/
|
public static Timestamp getTimestamp(){
|
SimpleDateFormat myFormat = new SimpleDateFormat(LONGFORMAT);
|
long stamp = System.currentTimeMillis();
|
String timestamp = myFormat.format(stamp);
|
return Timestamp.valueOf(timestamp);
|
}
|
|
|
/**
|
* 日期转时间戳
|
*/
|
public static Timestamp getTimestamp(Date date) {
|
return Timestamp.valueOf(getTimestampString(date));
|
}
|
|
/**
|
* 字符串转时间戳
|
*/
|
public static Timestamp getTimestamp(String timestamp) {
|
if(StringUtils.isEmpty(timestamp)){
|
return null;
|
}
|
return Timestamp.valueOf(timestamp);
|
}
|
|
/**
|
* 时间格式化 yyyy-MM-dd
|
*/
|
public static String getDate() {
|
SimpleDateFormat myFormat = new SimpleDateFormat(SHORTFORMAT);
|
return myFormat.format(System.currentTimeMillis());
|
}
|
|
/**
|
* 时间格式化 yyyy-MM-dd
|
*/
|
public static String getDate(Date date) {
|
return getDate(date, SHORTFORMAT);
|
}
|
|
/**
|
* 时间格式化 yyyy-MM-dd
|
*/
|
public static String getDate(Date date, String format) {
|
String result = null;
|
try {
|
SimpleDateFormat myFormat = new SimpleDateFormat(format);
|
result = myFormat.format(date);
|
} catch (Exception e) {
|
return null;
|
}
|
return result;
|
}
|
|
/**
|
* 时间格式化 yyyy-MM-dd HH:mm:ss
|
* */
|
public static String getDateTime() {
|
return getDateTime(new Date());
|
}
|
|
/**
|
* 时间格式化 yyyy-MM-dd HH:mm:ss
|
* */
|
public static String getDateTime(Date date) {
|
SimpleDateFormat d = new SimpleDateFormat(LONGFORMAT);
|
return d.format(date);
|
}
|
|
/**
|
* 时间格式化 yyyy-MM-dd
|
* */
|
public static String getShortDate(Date date){
|
if(date==null)
|
date = new Date();
|
return getDate(date, SHORTFORMAT);
|
}
|
|
/**
|
* 时间格式化 yyyy-MM-dd HH:mm:ss
|
* */
|
public static String getLongDate(Date date){
|
if(date==null)
|
date = new Date();
|
return getDateTime(date);
|
}
|
|
/**
|
* 时间格式化 yyyy-MM-dd HH:mm:ss
|
* */
|
public static String getTimestampStr(){
|
SimpleDateFormat myFormat = new SimpleDateFormat(LONGFORMAT);
|
return myFormat.format(System.currentTimeMillis());
|
}
|
|
/**
|
* 时间格式化 yyyyMMddHHmmss
|
* */
|
public static String getLongTime(){
|
SimpleDateFormat myFormat = new SimpleDateFormat(LONGFORMATNOSPIT);
|
return myFormat.format(System.currentTimeMillis());
|
}
|
|
/**
|
* 时间格式化 yyyyMMddHHmmss
|
* */
|
public static String getLongTime(long ts){
|
SimpleDateFormat myFormat = new SimpleDateFormat(LONGFORMATNOSPIT);
|
return myFormat.format(ts);
|
}
|
|
/**
|
* 时间戳TS
|
*/
|
public static String getTS(){
|
long ts = System.currentTimeMillis();
|
return Long.toString(ts);
|
}
|
|
/**
|
* N年后
|
* */
|
public static Date addYear(Date date, int amount) {
|
return add(date, Calendar.YEAR, amount);
|
}
|
|
/**
|
* N个月后
|
* */
|
public static Date addMonth(Date date, int amount) {
|
return add(date, Calendar.MONTH, amount);
|
}
|
|
/**
|
* 得到某一天的该星期的第一日 00:00:00
|
*
|
* @param date
|
* @param firstDayOfWeek
|
* 一个星期的第一天为星期几
|
*
|
* @return
|
*/
|
public static Date getFirstDayOfWeek(Date date, int firstDayOfWeek) {
|
Calendar cal = Calendar.getInstance();
|
if (date != null)
|
cal.setTime(date);
|
cal.setFirstDayOfWeek(firstDayOfWeek);//设置一星期的第一天是哪一天
|
cal.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);//指示一个星期中的某天
|
cal.set(Calendar.HOUR_OF_DAY, 0);//指示一天中的小时。HOUR_OF_DAY 用于 24 小时制时钟。例如,在 10:04:15.250 PM 这一时刻,HOUR_OF_DAY 为 22。
|
cal.set(Calendar.MINUTE, 0);//指示一小时中的分钟。例如,在 10:04:15.250 PM 这一时刻,MINUTE 为 4。
|
cal.set(Calendar.SECOND, 0);
|
cal.set(Calendar.MILLISECOND, 0);
|
return cal.getTime();
|
}
|
|
/**
|
* N天后
|
* */
|
public static Date addDay(Date date, int amount) {
|
return add(date, Calendar.DATE, amount);
|
}
|
|
/**
|
* N(年、月、天)后
|
* */
|
public static Date add(Date date, int field, int amount) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
calendar.add(field, amount);
|
return calendar.getTime();
|
}
|
|
/**
|
* N(年、月、天)后
|
* */
|
public static Date add(String datestr, int field, int amount) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(formatDate(datestr));
|
calendar.add(field, amount);
|
return calendar.getTime();
|
}
|
|
/**
|
* N(年、月、天)后(字符串形式)
|
* */
|
public static String add(String datestr, int field, int amount, String format) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(formatDate(datestr,format));
|
calendar.add(field, amount);
|
return getDate(calendar.getTime(), format);
|
}
|
|
/***
|
* 获取当前时间,不含日期
|
* @return
|
*/
|
public static String getCurrentTime(){
|
SimpleDateFormat sdf = new SimpleDateFormat("kk:mm:ss");
|
String time = sdf.format(Calendar.getInstance().getTime());
|
return time;
|
}
|
|
/**
|
* 将字符串转为为日期
|
*/
|
public static Date formatDate(String datestr) {
|
if(19==datestr.length()){
|
return formatDate(datestr, LONGFORMAT);
|
}else{
|
return formatDate(datestr, SHORTFORMAT);
|
}
|
}
|
|
/**
|
* 将字符串转为为日期
|
*/
|
public static Date formatDate(String datestr, String format) {
|
try {
|
if(StringUtils.isEmpty(datestr)){
|
return null;
|
}
|
SimpleDateFormat myFormat = new SimpleDateFormat(format);
|
Date date = myFormat.parse(datestr);
|
return date;
|
} catch (Exception e) {
|
return null;
|
}
|
}
|
|
/***
|
* 月份的开始结束日期
|
* @param year
|
* @param month
|
* @param keys
|
* @return
|
*/
|
public static Map<String, String> getMonthStartDateAndEndDate(int year, int month,
|
String... keys) {
|
SimpleDateFormat d = new SimpleDateFormat(LONGFORMAT);
|
Map<String, String> date = new HashMap<String, String>();
|
Calendar calendar = Calendar.getInstance();
|
calendar.set(Calendar.YEAR, year);
|
calendar.set(Calendar.MONTH, month - 1);
|
calendar.set(Calendar.DAY_OF_MONTH, 1);
|
date.put(keys[0], d.format(calendar.getTime()));
|
calendar.add(Calendar.MONTH, 1);
|
calendar.add(Calendar.DAY_OF_MONTH, -1);
|
date.put(keys[1], d.format(calendar.getTime()));
|
return date;
|
}
|
|
/**
|
* 时间格式化
|
*/
|
public static String getTimeString(String time) {
|
String[] ti = time.split(":");
|
if (ti[1].length() == 1) {
|
time = ti[0] + "0" + ti[1];
|
} else {
|
time = ti[0] + ti[1];
|
}
|
return time;
|
}
|
|
/**
|
* <br>描 述: 获取计费模式的年月 2017-06-26 至 2017-07-25 为 2017年07月 以此类推
|
* <br>作 者: shizhenwei
|
* <br>历 史: (版本) 作者 时间 注释
|
* @return
|
*/
|
public static String getSicdtYearMonth(String datestr){
|
String[] fileds = datestr.split(DIVIDE_LINE);
|
if(Integer.valueOf(fileds[2])>25){
|
Date date = add(datestr, FIELD_MONTH, 1);
|
return getDate(date, MONTHFORMAT);
|
}
|
return datestr.substring(0, 7);
|
}
|
|
public static String getSicdtYearMonth(){
|
return getSicdtYearMonth(getDate());
|
}
|
|
@SuppressWarnings("deprecation")
|
public static String timeString() {
|
Date date = new Date();
|
return getTimeString(date.getHours() + ":" + date.getMinutes());
|
}
|
|
|
/**
|
* 月-天数
|
* */
|
public static int getDayNum(int year, int month) {
|
if (month == 2) {
|
return year % 400 != 0 && (year % 4 != 0 || year % 100 == 0) ? 28
|
: 29;
|
}
|
String SmallMonth = ",4,6,9,11,";
|
return SmallMonth.indexOf(String.valueOf(String
|
.valueOf((new StringBuffer(",")).append(String.valueOf(month))
|
.append(",")))) < 0 ? 31 : 30;
|
}
|
/***
|
* 两个日期之间的天数
|
* @param date1
|
* @param date2
|
* @return
|
*/
|
public static int DateDiff(Date date1, Date date2) {
|
int i = (int) ((date1.getTime() - date2.getTime()) / 3600 / 24 / 1000);
|
return i;
|
}
|
/**
|
* 日期拆分,取年或月或日 Y-年 M-月 D-日
|
* @param strDate
|
* @param style
|
* @return
|
*/
|
public static int getYearMonthDate(String strDate, String style) {
|
int year;
|
int month;
|
int day;
|
int firstDash;
|
int secondDash;
|
if (strDate == null) {
|
return 0;
|
}
|
firstDash = strDate.indexOf('-');
|
secondDash = strDate.indexOf('-', firstDash + 1);
|
if ((firstDash > 0) & (secondDash > 0)
|
& (secondDash < strDate.length() - 1)) {
|
year = Integer.parseInt(strDate.substring(0, firstDash));
|
month = Integer.parseInt(strDate.substring(firstDash + 1,
|
secondDash));
|
day = Integer.parseInt(strDate.substring(secondDash + 1));
|
} else {
|
return 0;
|
}
|
if (style.equalsIgnoreCase("Y")) {
|
return year;
|
} else if (style.equalsIgnoreCase("M")) {
|
return month;
|
} else if (style.equalsIgnoreCase("D")) {
|
return day;
|
} else {
|
return 0;
|
}
|
}
|
|
/***
|
* 是否是年份的第一天
|
* @param date
|
* @return
|
*/
|
public static boolean isYearFirstDay(java.sql.Date date) {
|
boolean i = false;
|
if ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 1)
|
&& (DateTimeUtil.getYearMonthDate(date.toString(), "D") == 1)) {
|
i = true;
|
}
|
return i;
|
}
|
/**
|
* 是否是半年的第一天
|
* @param date
|
* @return
|
*/
|
public static boolean isHalfYearFirstDay(java.sql.Date date) {
|
boolean i = false;
|
if (((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 1) && (DateTimeUtil
|
.getYearMonthDate(date.toString(), "D") == 1))
|
|| ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 7) && (DateTimeUtil
|
.getYearMonthDate(date.toString(), "D") == 1))) {
|
i = true;
|
}
|
return i;
|
}
|
/**
|
* 取半年的第一天
|
* @param date
|
* @return
|
*/
|
public static String getHalfYearFirstDay(java.sql.Date date) {
|
String month = "01";
|
if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 7) {
|
month = "07";
|
}
|
String day = Integer.toString(DateTimeUtil.getYearMonthDate(
|
date.toString(), "Y")) + "-" + month + "-01";
|
return day;
|
}
|
/**
|
* 是否是半年最后一天
|
* */
|
public static boolean isHalfYearLastDay(java.sql.Date date) {
|
boolean i = false;
|
if (((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 12) && (DateTimeUtil
|
.getYearMonthDate(date.toString(), "D") == 31))
|
|| ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 6) && (DateTimeUtil
|
.getYearMonthDate(date.toString(), "D") == 30))) {
|
i = true;
|
}
|
return i;
|
}
|
/***
|
* 取半年最后一天
|
* @param date
|
* @return
|
*/
|
public static String getHalfYearLastDay(java.sql.Date date) {
|
String month = "-06-30";
|
if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 7) {
|
month = "-12-31";
|
}
|
String day = Integer.toString(getYearMonthDate(date.toString(), "Y"))
|
+ "-" + month;
|
return day;
|
}
|
|
/***
|
*是否是年度最后一天
|
* @param date
|
* @return
|
*/
|
public static boolean isYearLastDay(java.sql.Date date) {
|
boolean i = false;
|
if ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 12)
|
&& (DateTimeUtil.getYearMonthDate(date.toString(), "D") == 31)) {
|
i = true;
|
}
|
return i;
|
}
|
|
/**
|
* 是否季度第一天
|
* */
|
public static boolean isQuarterFirstDay(java.sql.Date date) {
|
boolean i = false;
|
if (((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 1)
|
|| (DateTimeUtil.getYearMonthDate(date.toString(), "M") == 4)
|
|| (DateTimeUtil.getYearMonthDate(date.toString(), "M") == 7) || (DateTimeUtil
|
.getYearMonthDate(date.toString(), "M") == 10))
|
&& (DateTimeUtil.getYearMonthDate(date.toString(), "D") == 1)) {
|
i = true;
|
}
|
return i;
|
}
|
/***
|
* 季度第一天
|
* @param date
|
* @return
|
*/
|
public static String getQuarterFirstDay(java.sql.Date date) {
|
String month = "01";
|
if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 10) {
|
month = "10";
|
} else if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 7) {
|
month = "07";
|
} else if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 4) {
|
month = "04";
|
} else if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 1) {
|
month = "01";
|
}
|
|
String day = Integer.toString(DateTimeUtil.getYearMonthDate(
|
date.toString(), "Y")) + "-" + month + "-01";
|
return day;
|
}
|
|
|
/**
|
* 是否季度最后一天
|
* @param date
|
* @return
|
*/
|
public static boolean isQuarterLastDay(java.sql.Date date) {
|
boolean i = false;
|
if ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 3)
|
&& (DateTimeUtil.getYearMonthDate(date.toString(), "D") == 31)) {
|
i = true;
|
}
|
if ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 6)
|
&& (DateTimeUtil.getYearMonthDate(date.toString(), "D") == 30)) {
|
i = true;
|
}
|
if ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 9)
|
&& (DateTimeUtil.getYearMonthDate(date.toString(), "D") == 30)) {
|
i = true;
|
}
|
if ((DateTimeUtil.getYearMonthDate(date.toString(), "M") == 12)
|
&& (DateTimeUtil.getYearMonthDate(date.toString(), "D") == 31)) {
|
i = true;
|
}
|
return i;
|
}
|
/**
|
* 季度最后一天
|
* @param date
|
* @return
|
*/
|
public static String getQuarterLastDay(java.sql.Date date) {
|
String month = "-03-31";
|
if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 10) {
|
month = "-12-31";
|
} else if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 7) {
|
month = "-09-30";
|
} else if (DateTimeUtil.getYearMonthDate(date.toString(), "M") >= 4) {
|
month = "-06-30";
|
}
|
|
String day = Integer.toString(DateTimeUtil.getYearMonthDate(
|
date.toString(), "Y")) + "-" + month;
|
return day;
|
}
|
|
/***
|
* 是否月份最后一天
|
* @param date
|
* @return
|
*/
|
public static boolean isMonthLastDay(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH);
|
}
|
|
/**
|
* 是否月份第一天
|
* @param date
|
* @return
|
*/
|
public static boolean isMonthFisrtDay(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
return calendar.getActualMinimum(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH);
|
}
|
|
/**
|
* 月份第一天日期
|
* @param date
|
* @return
|
*/
|
public static Date getMonthFisrtDay(Date date){
|
Calendar calendar = Calendar.getInstance();
|
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
|
return calendar.getTime();
|
}
|
|
/**
|
* 星期的第几天
|
* @param strDate yy-mm-dd
|
* @return
|
*/
|
public static int getDateInWeek(String strDate) {
|
DateFormat df = DateFormat.getDateInstance();
|
try {
|
df.parse(strDate);
|
Calendar c = df.getCalendar();
|
int day = c.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
|
return day;
|
} catch (ParseException e) {
|
return -1;
|
}
|
}
|
|
|
public static java.sql.Date getSqlDate(Date date) {
|
java.sql.Date result = null;
|
try {
|
SimpleDateFormat myFormat = new SimpleDateFormat(LONGFORMAT);
|
String mystrdate = myFormat.format(date);
|
result = java.sql.Date.valueOf(mystrdate);
|
} catch (Exception e) {
|
return null;
|
}
|
return result;
|
}
|
|
/**
|
* 获取当前月的月初到今天的日期
|
*/
|
// public static List<String> getDayofMouth() {
|
// List<String> data = new ArrayList<>();
|
//
|
// }
|
|
/**
|
* 获取当月所有日期
|
* @return
|
*/
|
public static List<String> getDayByMonth(String yearStr, String monthStr){
|
List<String> data = new ArrayList<>();
|
try {
|
Calendar c = Calendar.getInstance();
|
// 获取当前的年份
|
c.set(Calendar.YEAR, Integer.valueOf(yearStr));
|
// 获取当前的月份(需要加1才是现在的月份)
|
c.set(Calendar.YEAR, Integer.valueOf(monthStr) - 1);
|
// 获取本月的总天数
|
int dayCount = c.getActualMaximum(Calendar.DAY_OF_MONTH);
|
// 定义时间格式
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
// 开始日期为当前年月拼接1号
|
Date startDate = sdf.parse(yearStr + "-" + monthStr + "-01");
|
// 结束日期为当前年月拼接该月最大天数
|
Date endDate = sdf.parse(yearStr + "-" + monthStr + "-" + dayCount);
|
// 设置calendar的开始日期
|
c.setTime(startDate);
|
// 当前时间小于等于设定的结束时间
|
while(c.getTime().compareTo(endDate) <= 0){
|
String time = sdf.format(c.getTime());
|
data.add(time);
|
// 当前日期加1
|
c.add(Calendar.DATE, 1);
|
}
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
|
return data;
|
}
|
|
|
/**
|
* 上个月
|
* @param dt
|
* @return
|
*/
|
@SuppressWarnings("deprecation")
|
public static String getUpMDate(Date dt) {
|
dt.setDate(1);
|
dt.setDate(dt.getDate() - 1);
|
return dt.toLocaleString();
|
}
|
|
|
public static String getShortDate(String dt) {
|
if (dt != null) {
|
SimpleDateFormat myFormat = new SimpleDateFormat(SHORTFORMAT);
|
try {
|
Date date = myFormat.parse(dt);
|
return getDate(date, SHORTFORMATNOSPIT);
|
} catch (ParseException e) {
|
return dt;
|
}
|
} else
|
return dt;
|
}
|
|
|
public static String getLongDate(String dt) {
|
if (dt != null) {
|
SimpleDateFormat myFormat = new SimpleDateFormat(SHORTFORMAT);
|
try {
|
Date date = myFormat.parse(dt);
|
return getDate(date, SHORTFORMAT);
|
} catch (ParseException e) {
|
return dt;
|
}
|
} else
|
return dt;
|
}
|
|
|
public static boolean isSameYearMonth(String date) {
|
try {
|
String currdate = getDate();
|
currdate = getShortDate(currdate).substring(0, 6);
|
String lastdate = getShortDate(date).substring(0, 6);
|
if (lastdate.equals(currdate)) {
|
return true;
|
} else {
|
return false;
|
}
|
} catch (NumberFormatException e) {
|
return false;
|
}
|
}
|
}
|