Update DateUtil.java

1. 获取指定月的第一天
2. 获取指定月的最后一天
This commit is contained in:
Carol 2020-08-03 10:22:17 +08:00 committed by GitHub
parent f1fd1ed395
commit 5fe5057068
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,7 +9,7 @@ import java.util.*;
*/
public class DateUtil {
public static class DATEFORMATE {
public static class DATEFORMATE{
//基本单位
public static final String BASETIME_yyyy = "yyyy";
//基本单位
@ -42,9 +42,9 @@ public class DateUtil {
public static final String FULLTIMEBY_ms = "mm:ss";
//::.毫秒
public static final String FULLTIMEBY_HmsS = "HH:mm:ss.SSS";
//年月 无分割
public static final String NOSEGMENTATION_yM = "yyyyMM";
//年月日 无分割
public static final String NOSEGMENTATION_yM = "yyyyMM";
public static final String NOSEGMENTATION_yMd = "yyyyMMdd";
//时分 无分割
public static final String NOSEGMENTATION_Hm = "HHmm";
@ -78,12 +78,11 @@ public class DateUtil {
/**
* 日期字符串转日期格式
*
* @param date 日期字符串
* @param date 日期字符串
* @param timeFormat 转换后的日期格式
* @return
*/
public static Date dateStringToDate(String date, String timeFormat) {
public static Date dateStringToDate(String date, String timeFormat){
SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
try {
return sdf.parse(date);
@ -93,7 +92,7 @@ public class DateUtil {
return null;
}
public static String timeStamp2fulltime(Date date) {
public static String timeStamp2fulltime(Date date){
SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMATE.FULLTIMEBY_yMdHms);
return sdf.format(date);
}
@ -101,14 +100,14 @@ public class DateUtil {
/**
* 获取指定日期的零点时间戳
*
* @param time 时间
* @param time 时间
* @param timeFormat 时间对应格式
* @return 零点时间戳(0 : 0 : 0)
* @return 零点时间戳(0:0:0)
*/
public static long getTodayEarlyMorning(String time, String timeFormat) {
long current = translateDatetoTimestamp(time, timeFormat);//当前时间毫秒数
long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();//前一天的零点零分零秒的毫秒数
long startTime = zero + 24 * 60 * 60 * 1000;
public static long getTodayEarlyMorning(String time, String timeFormat){
long current=translateDatetoTimestamp(time, timeFormat);//当前时间毫秒数
long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//前一天的零点零分零秒的毫秒数
long startTime = zero + 24*60*60*1000;
return startTime;
}
@ -116,125 +115,84 @@ public class DateUtil {
/**
* 获取指定日期的晚上24点的时间戳
*
* @param time 时间
* @param time 时间
* @param timeFormat 时间对应格式
* @return 当天24点的时间戳(23 : 59 : 59)
* @return 当天24点的时间戳(23:59:59)
*/
public static long getTodayLaterMorning(String time, String timeFormat) {
long current = translateDatetoTimestamp(time, timeFormat);//当前时间毫秒数
long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();//前一天的零点零分零秒的毫秒数
long endTime = zero + 24 * 60 * 60 * 1000 * 2 - 1;
public static long getTodayLaterMorning(String time, String timeFormat){
long current=translateDatetoTimestamp(time, timeFormat);//当前时间毫秒数
long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//前一天的零点零分零秒的毫秒数
long endTime = zero + 24*60*60*1000*2 - 1;
return endTime;
}
/**
* 获取当前日期的过去指定天数长度的零点时时间戳包含当天
*
* @param maxDays 3 (2018/10/11 0:0:0)
* @return 2018/10/09 0:0:0 的时间戳
*/
public static long getLastDaysTimestamp(int maxDays) {
public static long getLastDaysTimestamp(int maxDays){
//当前时间毫秒数
long current = System.currentTimeMillis();
long current=System.currentTimeMillis();
//今天零点零分零秒的毫秒数
long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
long lastThirty = zero - 24 * 60 * 60 * 1000 * (maxDays);
long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();
long lastThirty = zero - 24*60*60*1000*(maxDays);
return lastThirty;
}
/**
* 获取过去指定时间的时间戳
*
* @param pastTime 指定的时间数量 5
* @param type 时间的类型 d日 H时 m分 s秒 S毫秒 不写默认单位秒
* @param pastTime 指定的时间数量 5
* @param type 时间的类型 d日 H时 m分 s秒 S毫秒 不写默认单位秒
* @param timeFormat 转换后的时间戳格式化的标准 不写默认格式 -- ::
* @return
*/
public static String getThePastTime(int pastTime, String type, String timeFormat) {
if (type == null) {
public static String getThePastTime(int pastTime, String type, String timeFormat){
if (type == null){
type = "s";
}
if (timeFormat == null) {
if (timeFormat == null){
timeFormat = DATEFORMATE.FULLTIMEBY_yMdHmsS;
}
long date = System.currentTimeMillis();
Long now_time = new Long(1);
if ("S".equals(type)) {
if ("S".equals(type)){
now_time = date - pastTime;
} else if ("s".equals(type)) {
now_time = date - pastTime * 1000;
} else if ("m".equals(type)) {
now_time = date - pastTime * 1000 * 60;
} else if ("H".equals(type)) {
now_time = date - pastTime * 1000 * 60 * 60;
} else if ("d".equals(type)) {
now_time = date - pastTime * 1000 * 60 * 60 * 24;
}else if ("s".equals(type)){
now_time = date - pastTime*1000;
}else if ("m".equals(type)){
now_time = date - pastTime*1000*60;
}else if ("H".equals(type)){
now_time = date - pastTime*1000*60*60;
}else if ("d".equals(type)){
now_time = date - pastTime*1000*60*60*24;
}
String finalTime = translateTimeToDate(now_time, timeFormat);
return finalTime;
}
public static String getThePastTime(int pastTime) {
public static String getThePastTime(int pastTime){
return getThePastTime(pastTime, null, null);
}
public static String getThePastTime(int pastTime, String type) {
public static String getThePastTime(int pastTime, String type){
return getThePastTime(pastTime, type, null);
}
/**
* 获取当前系统时间的时间戳
*
* @return 时间戳
*/
public static Long getCurrentTime() {
public static Long getCurrentTime(){
return System.currentTimeMillis();
}
/**
* 获取当前时间规整以5分钟为单位的时间
*
* @param currentTime 时间搓
* @param min 多少分钟的倍数如当前时间16:38 以为5为倍数规整后的时间为1635
* @return 时间 201906211635
*/
public static String getCurrentTimePutInOrder(long currentTime, long min) {
try {
long needTime = 0;
//北京时间和零时区差八个小时所以代码中如果大于八个小时取余就有问题
if (min >= 8 * 60) {
return new SimpleDateFormat(DATEFORMATE.NOSEGMENTATION_yMdHm).format(getMinDate(currentTime));
} else {
needTime = currentTime - currentTime % (min * 60L * 1000L);
}
return new SimpleDateFormat(DATEFORMATE.NOSEGMENTATION_yMdHm).format(new Date(needTime));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取当天的日期
*
* @param time 时间搓
* @return
* @throws ParseException
*/
public static Date getMinDate(Long time) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMATE.FULLTIMEBY_yMd);
Date newDate = sdf.parse(sdf.format(time));
return newDate;
}
/**
* 获取指定格式的当前时间
*
* @return 时间
*/
public static String getNowTimeByFormat(String timeFormat) {
public static String getNowTimeByFormat(String timeFormat){
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeFormat);
String today = simpleDateFormat.format(date);
@ -243,25 +201,35 @@ public class DateUtil {
/**
* 转换时间戳位时间格式
*
* @param timestamp 时间戳(long)
* @param timestamp 时间戳(long)
* @param timeFormat 转换的时间格式
* @return 时间
*/
public static String translateTimeToDate(Long timestamp, String timeFormat) {
public static String translateTimeToDate(Long timestamp, String timeFormat){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeFormat);
String da = simpleDateFormat.format(timestamp);
return da;
}
/**
* 转换时间格式
* @param date 时间(date)
* @param timeFormat 转换的时间格式
* @return 时间
*/
public static String translateDateToString(Date date, String timeFormat){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeFormat);
String da = simpleDateFormat.format(date);
return da;
}
/**
* 字符串转成时间格式
*
* @param time 字符串
* @param time 字符串
* @param timeFormat 转换格式
* @return 时间格式
*/
public static Date translateStringToDate(String time, String timeFormat) {
public static Date translateStringToDate(String time, String timeFormat){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeFormat);
Date da = null;
try {
@ -274,12 +242,11 @@ public class DateUtil {
/**
* 13位时间戳转date格式
*
* @param timestamp 时间戳
* @param timestamp 时间戳
* @param timeFormat 时间格式
* @return
*/
public static Date translateTimestampToDate(Long timestamp, String timeFormat) {
public static Date translateTimestampToDate(Long timestamp, String timeFormat){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeFormat);
String time = translateTimeToDate(timestamp, timeFormat);
Date da = null;
@ -293,12 +260,11 @@ public class DateUtil {
/**
* 时间转时间戳
*
* @param time 需要转换的时间
* @param time 需要转换的时间
* @param timeFormat 时间对应的格式
* @return 13位时间戳
*/
public static Long translateDatetoTimestamp(String time, String timeFormat) {
public static Long translateDatetoTimestamp(String time, String timeFormat){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeFormat);
Date date = null;
try {
@ -306,23 +272,18 @@ public class DateUtil {
} catch (ParseException e) {
e.printStackTrace();
}
if (date == null) {
return null;
}
long res = date.getTime();
return res;
return date.getTime();
}
/**
* 序列化字符串为时间格式
*
* @param time 需要转换的字符串 (20181011 10:11:12.013)
* @param timeFormat 字符串对应的时间格式 (yyyyMMdd HH:mm:ss.SSS)
* @param time 需要转换的字符串 (20181011 10:11:12.013)
* @param timeFormat 字符串对应的时间格式 (yyyyMMdd HH:mm:ss.SSS)
* @param timeTranselate 转换后需要的字符串格式 (yyyy年M月dd日 HH时mm分ss秒SSS毫秒)
* @return 2018年10月11日 10时11分12秒13毫秒
*/
public static String serializationDate(String time, String timeFormat, String timeTranselate) {
public static String serializationDate(String time, String timeFormat, String timeTranselate){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeFormat);
try {
Date date = simpleDateFormat.parse(time);
@ -337,18 +298,17 @@ public class DateUtil {
/**
* 获取指定天数前的日期集合包含当天
*
* @param num 天数
* @param time 时间
* @param timeFormat 时间格式
* @param num 天数
* @param time 时间
* @param timeFormat 时间格式
* @param timeTranslate 输出格式
* @return 日期集合
*/
public static List<String> getDesignationDay(int num, String time, String timeFormat, String timeTranslate) {
List<String> days = new ArrayList<String>();
public static List getDesignationDay(int num, String time, String timeFormat, String timeTranslate){
List days = new ArrayList();
Long timestamp_time = translateDatetoTimestamp(time, timeFormat);
for (int i = 0; i < num; i++) {
Long new_time = timestamp_time - i * 24 * 60 * 60 * 1000;
Long new_time = timestamp_time - i*24*60*60*1000;
String today = translateTimeToDate(new_time, timeTranslate);
days.add(today);
}
@ -359,40 +319,34 @@ public class DateUtil {
/**
* 时间切割机小时切割
*
* @param cutTime 切割小时数 2小时 = 2
* @param cutTime 切割小时数 2小时 = 2
* @param startTime 开始时间戳 10位
* @param endTime 结束时间戳 10位
* @return Map<序号, 时间段> 切割后时间段列表
* @param endTime 结束时间戳 10位
* @return 切割后时间段列表
*/
public static Map<Integer, String> splitTimestamp(int cutTime, long startTime, long endTime) {
public static List<String> splitTimestamp(int cutTime, long startTime, long endTime) {
List<String> timeList = new ArrayList<>();
Map timeMap = new LinkedHashMap();
startTime = startTime * 1000;
endTime = endTime * 1000;
if (cutTime <= 0) {
if (cutTime <= 0){
timeList.add(String.valueOf(String.valueOf(startTime).concat(",").concat(String.valueOf(endTime))));
} else if (cutTime > 0 && endTime > startTime) {
long cut = ((endTime - startTime) / 3600000 / cutTime) + ((endTime - startTime) / 3600000 % cutTime);
}else if (cutTime > 0 && endTime > startTime){
long cut = ((endTime - startTime) / 3600000 /cutTime) + ((endTime - startTime) / 3600000 % cutTime);
long timeInterval = (endTime - startTime) / cut;
int i = 0;
while (true) {
Object put;
if (startTime < endTime) {
if ((startTime + timeInterval) > endTime) {
while(true){
if (startTime < endTime){
if ((startTime + timeInterval) > endTime){
timeList.add(String.valueOf(String.valueOf(startTime).concat(",").concat(String.valueOf(endTime))));
put = timeMap.put(i, String.valueOf(String.valueOf(startTime).concat(",").concat(String.valueOf(endTime))));
} else {
timeList.add(String.valueOf(String.valueOf(startTime).concat(",").concat(String.valueOf(startTime + timeInterval))));
put = timeMap.put(i, String.valueOf(String.valueOf(startTime).concat(",").concat(String.valueOf(startTime + timeInterval))));
}else {
timeList.add(String.valueOf( String.valueOf(startTime).concat(",").concat(String.valueOf(startTime + timeInterval))));
}
i++;
} else if (startTime >= endTime) {
}else if (startTime >= endTime){
break;
}
startTime = startTime + timeInterval;
}
}
return timeMap;
return timeList;
}
/**
@ -408,8 +362,7 @@ public class DateUtil {
//获取当前时间与周期时间的差
long nowTime = System.currentTimeMillis();
int timeDiscrepancy = Integer.parseInt(String.valueOf(nowTime%(executorTime*1000)/1000));
int delayTime = executorTime-timeDiscrepancy;
return delayTime;
return executorTime-timeDiscrepancy;
}else {
return 0;
}
@ -455,8 +408,67 @@ public class DateUtil {
return delayTime;
}
}
/**
/**
* 判断传入的字符串是时间格式还是时间戳格式
* 时间戳格式根据设置的format转换成对应的时间格式
* @param date 校验字符串
* @param format 时间格式
* @return 时间
*/
public static String judgeTimeAndTimestamp(String date, String format){
try {
String time = "";
if (date.length() == 14){
//20190613171965
time = date.substring(0, 4) + "-" +
date.substring(4, 6) + "-" +
date.substring(6, 8) + " " +
date.substring(8, 10) + ":" +
date.substring(10, 12) + ":" +
date.substring(12, 14);
}else if (date.length() == 13){
//1560390240760
time = DateUtil.translateTimeToDate(Long.valueOf(date), format);
}
return time;
} catch (NumberFormatException e) {
e.printStackTrace();
}
return null;
}
/**
* 时间格式转换
* @param time 2020-03-24T11:24:31.000Z
* @return 2020-03-24 19:24:31
*/
public static String switchTime(String time) throws ParseException {
SimpleDateFormat format2 = new SimpleDateFormat(DATEFORMATE.FULLTIMEBY_yMdHms);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//格式化的表达式
time = time.replace("Z", " UTC");//是空格+UTC
Date data = format.parse(time);
String format1 = format2.format(data);
return format1;
}
/**
* 转化时间格式
* @param time 2020-01-15T15:16:23+08:00
* @return 2020-01-15 15:16:23
* @throws ParseException
*/
public static String switchLineTime(String time) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = df.parse(time);
SimpleDateFormat df1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
Date date1 = df1.parse(date.toString());
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = df2.format(date1);
return format;
}
/**
* 根据日期格式自动转换时间
* 最大精确到秒不支持毫秒最小精确到年不支持世纪
* @param date_format 日期格式例如 YYYY-MM-dd
@ -537,6 +549,8 @@ public class DateUtil {
String value = String.valueOf(Integer.valueOf(year) + parseInt);
Long aLong = DateUtil.translateDatetoTimestamp(value, DATEFORMATE.BASETIME_yyyy);
translateTimeToDate = DateUtil.translateTimeToDate(aLong, date_format);
}else {
translateTimeToDate = DateUtil.translateTimeToDate(current_time, date_format);
}
return translateTimeToDate;
}
@ -554,4 +568,31 @@ public class DateUtil {
return format2.format(data);
}
/**
* 获取指定月份的第一天的当前时间
* @param month 指定月1 当前月加1
* @param format 转换后的格式
* @return 转换后格式的时间
*/
public static String getFirstDayByMonth(int month, String format){
//获取当前月第一天
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
return DateUtil.translateDateToString(c.getTime(), format);
}
/**
* 获取指定月的最后一天
* @param month 指定月1 当前月加1
* @param format 转换后时间格式
* @return 转换后的时间
*/
public static String getLastDayByMonth(int month, String format){
//获取当前月最后一天
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
return DateUtil.translateDateToString(c.getTime(), format);
}
}