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

@ -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,7 +78,6 @@ public class DateUtil {
/**
* 日期字符串转日期格式
*
* @param date 日期字符串
* @param timeFormat 转换后的日期格式
* @return
@ -130,7 +129,6 @@ public class DateUtil {
/**
* 获取当前日期的过去指定天数长度的零点时时间戳包含当天
*
* @param maxDays 3 (2018/10/11 0:0:0)
* @return 2018/10/09 0:0:0 的时间戳
*/
@ -145,7 +143,6 @@ public class DateUtil {
/**
* 获取过去指定时间的时间戳
*
* @param pastTime 指定的时间数量 5
* @param type 时间的类型 d日 H时 m分 s秒 S毫秒 不写默认单位秒
* @param timeFormat 转换后的时间戳格式化的标准 不写默认格式 -- ::
@ -185,53 +182,14 @@ public class DateUtil {
/**
* 获取当前系统时间的时间戳
*
* @return 时间戳
*/
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){
@ -243,7 +201,6 @@ public class DateUtil {
/**
* 转换时间戳位时间格式
*
* @param timestamp 时间戳(long)
* @param timeFormat 转换的时间格式
* @return 时间
@ -254,9 +211,20 @@ public class DateUtil {
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 timeFormat 转换格式
* @return 时间格式
@ -274,7 +242,6 @@ public class DateUtil {
/**
* 13位时间戳转date格式
*
* @param timestamp 时间戳
* @param timeFormat 时间格式
* @return
@ -293,7 +260,6 @@ public class DateUtil {
/**
* 时间转时间戳
*
* @param time 需要转换的时间
* @param timeFormat 时间对应的格式
* @return 13位时间戳
@ -306,17 +272,12 @@ 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 timeTranselate 转换后需要的字符串格式 (yyyy年M月dd日 HH时mm分ss秒SSS毫秒)
@ -337,15 +298,14 @@ public class DateUtil {
/**
* 获取指定天数前的日期集合包含当天
*
* @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;
@ -362,11 +322,10 @@ public class DateUtil {
* @param cutTime 切割小时数 2小时 = 2
* @param startTime 开始时间戳 10位
* @param endTime 结束时间戳 10位
* @return Map<序号, 时间段> 切割后时间段列表
* @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){
@ -374,25 +333,20 @@ public class DateUtil {
}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){
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))));
}
i++;
}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;
}
@ -456,6 +409,65 @@ public class DateUtil {
}
}
/**
* 判断传入的字符串是时间格式还是时间戳格式
* 时间戳格式根据设置的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;
}
/**
* 根据日期格式自动转换时间
* 最大精确到秒不支持毫秒最小精确到年不支持世纪
@ -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);
}
}