Java日期工具
package com.eqics.base.utils;
import com.eqics.base.vo.DateRange;
import com.eqics.common.core.util.StringUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @description 日期时间专门处理工具类
*/
public class DateUtil {
private static String YYYY = "yyyy";
private static String YYYY_MM_DD = "yyyy-MM-dd";
private static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
private static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
private static String YYYYMMDD = "yyyyMMdd";
private static String HH = "HH";
private static String YYYYMMDDHHMM = "yyyyMMddHHmm";
private static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
private static String YYYYMMDDHH = "yyyyMMddHH";
private static String MM_DD = "MMdd";
/**
* @return 当前年
* @description 获取当前年
*/
public static String getSystemYear() {
return getSystemTime().substring(0, 4);
}
/**
* @return 当前日期
* @description 获取当前日期
*/
public static String getSystemDay() {
return getSystemTime().substring(0, 8);
}
/**
* 获取当前月
*
* @return
*/
public static String getMonth() {
return getSystemTime().substring(4, 6);
}
/**
* 获取当前月日
*
* @return
*/
public static String getMonthDay() {
return getSystemTime().substring(4, 8);
}
/**
* 获取当前月日
*
* @return
*/
public static String getDay() {
return getSystemTime().substring(6, 8);
}
/**
* @return
* @description 获取当前时间
*/
public static String getSystemTime() {
return new SimpleDateFormat(YYYYMMDDHHMMSS).format(new Date());
}
/**
* @return
* @description 获取当前时间小时
*/
public static String getSystemTimeHour() {
return new SimpleDateFormat(YYYYMMDDHH).format(new Date());
}
/**
* 获取当前时间,指定前面多少小时的时间
*
* @param ihour
* @return
*/
public static String getBeforeHourTime(int ihour) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
return new SimpleDateFormat(YYYYMMDDHH).format(calendar.getTime());
}
/**
* 获取当前小时
*
* @return
*/
public static int getSystemHour() {
return Integer.parseInt(getSystemTimeHour().substring(8, 10));
}
/**
* @param thisdate
* @param step
* @return
* @description 返回给定日期之后的第几天
*/
public static String getConventDate(String thisdate, int step) {
java.util.Date now = getDateByDay(thisdate);
SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
String xtsj = sdf.format(dateAdd(now, step, 0));
return xtsj;
}
/**
* 获取当年第一天
*
* @return
*/
public static String getYearFirst() {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
int currentYear = calendar.get(Calendar.YEAR);
calendar.clear();
calendar.set(Calendar.YEAR, currentYear);
Date currYearFirst = calendar.getTime();
String formatDate = df.format(currYearFirst);
return formatDate;
}
/**
* 获取今年已过多少天
*
* @return
*/
public static long getHappenDayCountThisYear() {
Calendar cal = Calendar.getInstance();
Date date = new Date();
cal.set(Integer.parseInt(getSystemYear()), 0, 1);//设置今年的1月1日
long timeOne = cal.getTimeInMillis();
cal.setTime(date);
long timeTwo = cal.getTimeInMillis();
long daysapart = (timeTwo - timeOne) / (1000 * 60 * 60 * 24);
return daysapart;
}
/**
* 根据给定的年获取某一年
*
* @param year 给定的年
* @param past 步长
* @return
*/
public static String getYearDynamic(String year, int past) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(YYYY);
Date start = sdf.parse(year);
return sdf.format(dateAdd(start, past, 2));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String args[]) throws Exception {
System.out.println(getSpecifiedAfterDateHour("2022070724",1));
}
/**
* 返回给定的日期在指定时间段后的日期,指定con为0时为天,1-月,2-年,3-小时 Description
*
* @param date 日期
* @param x 给定数目
* @param con 给定操作
* @return
*/
public static Date dateAdd(Date date, int x, int con) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
if (con == 0) {
int d = cal.get(Calendar.DAY_OF_MONTH);
d = d + x;
cal.set(Calendar.DAY_OF_MONTH, d);
}
if (con == 1) {
int m = cal.get(Calendar.MONTH);
m = m + x;
cal.set(Calendar.MONTH, m);
}
if (con == 2) {
int y = cal.get(Calendar.YEAR);
y = y + x;
cal.set(Calendar.YEAR, y);
}
if (con == 3) {
int d = cal.get(Calendar.HOUR_OF_DAY);
d = d + x;
cal.set(Calendar.HOUR_OF_DAY, d);
}
return cal.getTime();
}
/**
* @param day 待处理日期
* @return
* @description 日期数据类型转换:根据给定的字符串生成日期, 字符串的格式为yyyymmdd
*/
public static Date getDateByDay(String day) {
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(day.substring(0, 4)), Integer.parseInt(day.substring(4, 6)) - 1, Integer.parseInt(day.substring(6, 8)));
return cal.getTime();
}
/**
* @param dayHour 待处理日期
* @return
* @description 时间数据类型转换:根据给定的字符串生成日期, 字符串的格式为yyyymmddhh
*/
public static Date getDateByDayHour(String dayHour) {
return new SimpleDateFormat(YYYYMMDDHH).parse(dayHour, new ParsePosition(0));
}
/**
* @param startTime String 开始时间 yyyyMMdd
* @param endTime String 结束时间 yyyyMMdd
* @return
* @description 获取两个日期之间的所有日期(字符串格式, 按天计算)
*/
public static List<String> getAllBetweenDay(String startTime, String endTime) {
List<String> result = new ArrayList<String>();
try {
if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
return result;
}
//定义转换格式
SimpleDateFormat df = new SimpleDateFormat(YYYYMMDD);
Date start = df.parse(startTime);
Date end = df.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
tempStart.add(Calendar.DAY_OF_YEAR, 1);
SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
result.add(sdf.format(start));
while (tempStart.before(tempEnd)) {
result.add(sdf.format(tempStart.getTime()));
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
if (!startTime.equals(endTime)) {
result.add(sdf.format(tempStart.getTime()));
}
} catch (Exception e) {
e.printStackTrace();
return result;
}
return result;
}
/**
* @Param: day
* @Author fun
* @Description: 获取两个日期间隔多少天
* @Date 2020/10/13
*/
public static long getBetweenDayCount(String startTime, String endTime) {
LocalDate startDate = LocalDate.parse(startTime, DateTimeFormatter.ofPattern(YYYYMMDD));
LocalDate endDate = LocalDate.parse(endTime, DateTimeFormatter.ofPattern(YYYYMMDD));
long between = startDate.until(endDate, ChronoUnit.DAYS) + 1;
return between;
}
/**
* @param day
* @Author: whui
* @Description: day to year-month-day (yyyy-MM-dd) 该方法与getConventDate基本一致
* @Date: 2020/8/31
* @return: java.lang.String
*/
public static String dayToDate(String start, int day) {
// 时间表示格式可以改变,yyyyMMdd需要写例如bai20160523这种形式的时间
SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
// 将字符串的du日期转为Date类型,ParsePosition(0)表示从第一个字符开始解析
Date date = sdf.parse(start, new ParsePosition(0));
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// add方法中的第二个参数n中,正数表示该日期后n天,负数表示该日期的前n天
// 20203
calendar.add(Calendar.DATE, day - 1);
Date date1 = calendar.getTime();
return sdf.format(date1);
}
/**
* @param day 格式yyyymmdd
* @return
* @description 获取昨天(根据所传日期字符串参数)
*/
public static String getYesterDay(String day) {
Date time = new SimpleDateFormat(YYYYMMDD).parse(day, new ParsePosition(0));
Calendar c = Calendar.getInstance();
c.setTime(time);
c.add(Calendar.DATE, -1);//-1.昨天时间 0.当前时间 1.明天时间 *以此类推
Date dayTime = c.getTime();
SimpleDateFormat df = new SimpleDateFormat(YYYYMMDD);
return df.format(dayTime);
}
/**
* @param beginTime
* @return
* @description 获取目标小时之前的25小时(含),格式 yyyymmddhh
*/
public static String getBefore25Hour(String beginTime) {
SimpleDateFormat df = new SimpleDateFormat(YYYYMMDDHH);
Calendar calendar = Calendar.getInstance();
try {
Date today = df.parse(beginTime);
calendar.setTime(today);
} catch (ParseException e) {
e.printStackTrace();
}
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 24);
String endTime = df.format(calendar.getTime());
if (String.valueOf(endTime).substring(8, 10).equals("00")) {
endTime = getYesterDay(String.valueOf(endTime).substring(0, 8)) + "24";
}
return endTime;
}
/**
* @param beginTime
* @return
* @description 获取目标小时之前的24小时(含),格式 yyyymmddhh
*/
public static String getBefore24Hour(String beginTime) {
SimpleDateFormat df = new SimpleDateFormat(YYYYMMDDHH);
Calendar calendar = Calendar.getInstance();
try {
Date today = df.parse(beginTime);
calendar.setTime(today);
} catch (ParseException e) {
e.printStackTrace();
}
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 23);
String endTime = df.format(calendar.getTime());
if (String.valueOf(endTime).substring(8, 10).equals("00")) {
endTime = getYesterDay(String.valueOf(endTime).substring(0, 8)) + "24";
}
return endTime;
}
/**
* @param beginTime
* @return
* @description 获取目标小时之前的48小时(含),格式 yyyymmddhh
*/
public static String getBefore48Hour(String beginTime) {
SimpleDateFormat df = new SimpleDateFormat(YYYYMMDDHH);
Calendar calendar = Calendar.getInstance();
try {
Date today = df.parse(beginTime);
calendar.setTime(today);
} catch (ParseException e) {
e.printStackTrace();
}
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 47);
String endTime = df.format(calendar.getTime());
if ("00".equals(endTime.substring(8, 10))) {
endTime = getYesterDay(endTime.substring(0, 8)) + "24";
}
return endTime;
}
/**
* 获取当前年的前一年
*
* @return
*/
public static String getYearBefore() {
SimpleDateFormat formats = new SimpleDateFormat(YYYY);
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -1);
Date date = c.getTime();
return formats.format(date);
}
/**
* 动态获取当前年的前x年
*
* @param val
* @return
*/
public static String getYearBeforeDynamic(int val) {
SimpleDateFormat formats = new SimpleDateFormat(YYYY);
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -val);
Date date = c.getTime();
return formats.format(date);
}
/**
* 获取当前年的后一年
*
* @return
*/
public static String getYearAfter() {
SimpleDateFormat formats = new SimpleDateFormat(YYYY);
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, +1);
Date date = c.getTime();
return formats.format(date);
}
/**
* 获取当天的前一天
*
* @return
*/
public static String getDayBefore() {
SimpleDateFormat formats = new SimpleDateFormat(YYYYMMDD);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
Date date = c.getTime();
return formats.format(date);
}
/**
* 获取当天的前一小时
*
* @return
*/
public static String getHourBefore() {
SimpleDateFormat formats = new SimpleDateFormat(HH);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
Date date = c.getTime();
return formats.format(date);
}
/**
* 获取上一季度 开始和结束时间
*
* @return
*/
public static DateRange getLastQuarter() {
Calendar startCalendar = Calendar.getInstance();
startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3 - 1) * 3);
startCalendar.set(Calendar.DAY_OF_MONTH, 1);
setMinTime(startCalendar);
Calendar endCalendar = Calendar.getInstance();
endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
setMaxTime(endCalendar);
return new DateRange(startCalendar.getTime(), endCalendar.getTime());
}
/**
* 最小时间
*
* @param calendar
*/
private static void setMinTime(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
}
/**
* 最大时间
*
* @param calendar
*/
private static void setMaxTime(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
}
/**
* 获取当月(开始时间)
*
* @return
*/
public static String getCurrentMonthStartTime() {
// 获取当前年份、月份、日期
Calendar cale = null;
// 获取当月第一天
SimpleDateFormat format = new SimpleDateFormat(YYYYMMDD);
String firstday;
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 0);
cale.set(Calendar.DAY_OF_MONTH, 1);
firstday = format.format(cale.getTime());
return firstday;
}
/**
* 获取当月(结束时间)
*
* @return
*/
public static String getCurrentMonthEndTime() {
// 获取当前年份、月份、日期
Calendar cale = null;
// 获取当月最后一天
SimpleDateFormat format = new SimpleDateFormat(YYYYMMDD);
String lastday;
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 0);
lastday = format.format(cale.getTime());
return lastday;
}
/**
* 获取当季
*
* @return
*/
public static DateRange getThisQuarter() {
Calendar startCalendar = Calendar.getInstance();
startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3);
startCalendar.set(Calendar.DAY_OF_MONTH, 1);
setMinTime(startCalendar);
Calendar endCalendar = Calendar.getInstance();
endCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3 + 2);
endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
setMaxTime(endCalendar);
return new DateRange(startCalendar.getTime(), endCalendar.getTime());
}
/**
* 获取未来七天或七天以上的日期
*
* @param past(1 2 3 4 5 6 7)
* @return
*/
public static String getAfterDate(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat(YYYYMMDD);
String result = format.format(today);
return result;
}
public static String getAfterDateHour(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
String result = format.format(today);
return result;
}
/**
* 获取指定日期,n天后日期
*
* @param date
* @param past
* @return
*/
public static String getAfterDate(Date date, int past) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + past);
SimpleDateFormat format = new SimpleDateFormat(YYYYMMDD);
String result = format.format(calendar.getTime());
return result;
}
/**
* 获取过去的某个日期
*
* @param num
* @return
*/
public static String getBeforeDate(Integer num) {
SimpleDateFormat format = new SimpleDateFormat(YYYYMMDD);
Calendar c = Calendar.getInstance();
//过去num天
c.setTime(new Date());
c.add(Calendar.DATE, -num);
Date d = c.getTime();
String day = format.format(d);
return day;
}
/**
* 获取指定日期的前N天
*
* @param sdate
* @param num
* @return
*/
public static String getSpecifiedBeforeDate(String sdate, Integer num) {
DateFormat fmt = new SimpleDateFormat(YYYYMMDD);
String day = "";
try {
Date s = fmt.parse(sdate);
SimpleDateFormat format = new SimpleDateFormat(YYYYMMDD);
Calendar c = Calendar.getInstance();
//过去num天
c.setTime(s);
c.add(Calendar.DATE, -num);
Date d = c.getTime();
day = format.format(d);
} catch (Exception e) {
e.printStackTrace();
}
return day;
}
/**
* 获取指定日期的后30天
*
* @param sdate
* @param past
* @return
*/
public static String getSpecifiedAfterDate(String sdate, int past) {
DateFormat fmt = new SimpleDateFormat(YYYYMMDD);
String day = "";
try {
Date s = fmt.parse(sdate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(s);
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat(YYYYMMDD);
day = format.format(today);
} catch (Exception e) {
e.printStackTrace();
}
return day;
}
/**
* 获取指定日期的后某几个小时
*
* @param sdate 小时
* @param past 过去某几个小时
* @return
*/
public static String getSpecifiedAfterDateHour(String sdate, int past) {
DateFormat fmt = new SimpleDateFormat(YYYYMMDDHH);
String dh = "";
try {
Date s = fmt.parse(sdate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(s);
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat(YYYYMMDDHH);
dh = format.format(today);
} catch (Exception e) {
e.printStackTrace();
}
return dh;
}
/**
* 判断是否是闰年
*
* @param year
* @return
*/
public static boolean isLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { //平闰年判断算法
return true;
} else {
return false;
}
}
/**
* 根据开始时间和结束时间返回时间段内的时间集合
*
* @param beginDate
* @param endDate
* @return List
*/
public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {
List<Date> lDate = new ArrayList<Date>();
lDate.add(beginDate);// 把开始时间加入集合
Calendar cal = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
cal.setTime(beginDate);
boolean bContinue = true;
while (bContinue) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
cal.add(Calendar.DAY_OF_MONTH, 1);
// 测试此日期是否在指定日期之后
if (endDate.after(cal.getTime())) {
lDate.add(cal.getTime());
} else {
break;
}
}
lDate.add(endDate);// 把结束时间加入集合
return lDate;
}
/**
* 获取开始和结束时间List
*
* @param startDay
* @param endDay
* @return
* @throws ParseException
*/
public static List<Date> getBetweenDateList(String startDay, String endDay) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
Date dBegin = sdf.parse(startDay);
Date dEnd = sdf.parse(endDay);
List<Date> listDate = getDatesBetweenTwoDate(dBegin, dEnd);
return listDate;
}
/**
* 获取指定月份的最后一天
*
* @param yearMonth
* @return
*/
public static String getLastDayOfMonth(String yearMonth) {
int year = Integer.parseInt(yearMonth.substring(0, 4)); //年
int month = Integer.parseInt(yearMonth.substring(4, 6)); //月
System.out.println(year + "----" + month);
Calendar cal = Calendar.getInstance();
// 设置年份
cal.set(Calendar.YEAR, year);
// 设置月份
// cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.MONTH, month); //设置当前月的上一个月
// 获取某月最大天数
//int lastDay = cal.getActualMaximum(Calendar.DATE);
int lastDay = cal.getMinimum(Calendar.DATE); //获取月份中的最小值,即第一天
// 设置日历中月份的最大天数
//cal.set(Calendar.DAY_OF_MONTH, lastDay);
cal.set(Calendar.DAY_OF_MONTH, lastDay - 1); //上月的第一天减去1就是当月的最后一天
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
return sdf.format(cal.getTime());
}
/**
* 日期格式转换(YYYY-MM-DD) 开始日期
*
* @param day
* @return
* @throws Exception
*/
public static String dateFormatConvertStartDay(String day) {
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = new Date();
try {
date1 = format1.parse(day);
} catch (ParseException e) {
e.printStackTrace();
}
return format1.format(date1);
}
public static boolean isDate(String mes) {
String format = "([0-9]{4})(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])";
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(mes);
if (matcher.matches()) {
pattern = Pattern.compile("(\\d{4})(\\d{2})(\\d{2})");
matcher = pattern.matcher(mes);
if (matcher.matches()) {
int y = Integer.valueOf(matcher.group(1));
int m = Integer.valueOf(matcher.group(2));
int d = Integer.valueOf(matcher.group(3));
if (d > 28) {
Calendar c = Calendar.getInstance();
c.set(y, m - 1, 1);
int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
return (lastDay >= d);
}
}
return true;
}
return false;
}
/**
* 获取指定月(开始时间)
*
* @return
*/
public static String getFixStartDayOfMonth(String yearMonth) {
int year = Integer.parseInt(yearMonth.substring(0, 4)); //年
int month = Integer.parseInt(yearMonth.substring(4, 6)); //月
Calendar cal = Calendar.getInstance();
// 设置年份
cal.set(Calendar.YEAR, year);
// 设置月份
cal.set(Calendar.MONTH, month - 1); //设置当前月的上一个月
// 获取某月最大天数
int lastDay = cal.getActualMinimum(Calendar.DATE); //获取月份中的最小值,即第一天
// 设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, 1);
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
return sdf.format(cal.getTime());
}
/**
* 获取指定月(结束时间)
*
* @param yearMonth
* @return
*/
public static String getFixLastDayOfMonth(String yearMonth) {
int year = Integer.parseInt(yearMonth.substring(0, 4)); //年
int month = Integer.parseInt(yearMonth.substring(4, 6)); //月
Calendar cal = Calendar.getInstance();
// 设置年份
cal.set(Calendar.YEAR, year);
// 设置月份
cal.set(Calendar.MONTH, month); //设置当前月的上一个月
// 获取某月最大天数
int lastDay = cal.getMinimum(Calendar.DATE); //获取月份中的最小值,即第一天
// 设置日历中月份的最大天数
//cal.set(Calendar.DAY_OF_MONTH, lastDay);
cal.set(Calendar.DAY_OF_MONTH, lastDay - 1); //上月的第一天减去1就是当月的最后一天
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
return sdf.format(cal.getTime());
}
/**
* 字符串格式转换(yyyyMMdd转yyyy-MM-dd)
*
* @param targetStr
* @return
*/
public static String dateConvertion(String targetStr) {
String str = targetStr; //
try {
// yyyyMMdd转yyyy-MM-dd
Date format1 = null;
format1 = new SimpleDateFormat("yyyyMMdd").parse(str);
String longDate = new SimpleDateFormat("yyyy-MM-dd").format(format1);
System.out.println("yyyyMMdd转yyyy-MM-dd:" + longDate);
return str;
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}
/**
* 字符串格式转换(yyyy-MM-dd转yyyyMMdd)
*
* @param targetStr
* @return
*/
public static String dateConvertion1(String targetStr) {
String str = targetStr;
try {
// yyyy-MM-dd转yyyyMMdd
Date format1 = null;
format1 = new SimpleDateFormat("yyyy-MM-dd").parse(str);
String longDate = new SimpleDateFormat("yyyyMMdd").format(format1);
System.out.println("yyyy-MM-dd转yyyyMMdd:" + longDate);
return longDate;
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}
/**
* 字符串格式转换(yyyyMMdd转yyyy.MM.dd)
*
* @param targetStr
* @return
*/
public static String dateConvertion2(String targetStr) {
String str = targetStr;
try {
Date format1 = null;
format1 = new SimpleDateFormat("yyyyMMdd").parse(str);
String longDate = new SimpleDateFormat("yyyy.MM.dd").format(format1);
return longDate;
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}
/**
* 获取相差天数
*
* @param start
* @param end
* @return
* @throws ParseException
*/
public static int daysBetween(String start, String end) throws ParseException {
DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
try {
Date star = dft.parse(start);//开始时间
Date endDay = dft.parse(end);//结束时间
Date nextDay = star;
int i = 0;
while (nextDay.before(endDay)) {//当明天不在结束时间之前是终止循环
Calendar cld = Calendar.getInstance();
cld.setTime(star);
cld.add(Calendar.DATE, 1);
star = cld.getTime();
//获得下一天日期字符串
nextDay = star;
i++;
}
// System.out.println("相差天数为:" + i);
return i;
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
/**
* 获取相差天数
*
* @param start
* @param end
* @return
* @throws ParseException
*/
public static int daysBetweenPlus(String start, String end) throws ParseException {
DateFormat dft = new SimpleDateFormat("yyyyMMdd");
try {
Date star = dft.parse(start);//开始时间
Date endDay = dft.parse(end);//结束时间
Date nextDay = star;
int i = 0;
while (nextDay.before(endDay)) {//当明天不在结束时间之前是终止循环
Calendar cld = Calendar.getInstance();
cld.setTime(star);
cld.add(Calendar.DATE, 1);
star = cld.getTime();
//获得下一天日期字符串
nextDay = star;
i++;
}
// System.out.println("相差天数为:" + i);
return i;
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
public static String getWeekOfDate(Date date) {
String[] weekDays = {"日", "一", "二", "三", "四", "五", "六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0) {
w = 0;
}
return weekDays[w];
}
/**
* 转星期几
*
* @param datetime
* @return
*/
public static String getWeekOfDate2(String datetime) {
SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance(); // 获得一个日历
Date datet = null;
try {
datet = f.parse(datetime);
cal.setTime(datet);
} catch (ParseException e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0) {
w = 0;
}
return weekDays[w];
}
public static String getWeekOfDate3(String datetime) {
SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
Calendar cal = Calendar.getInstance(); // 获得一个日历
Date datet = null;
try {
datet = f.parse(datetime);
cal.setTime(datet);
} catch (ParseException e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0) {
w = 0;
}
return weekDays[w];
}
/**
* 获取指定日期后日期
*
* @param day 格式:YYYYMMDD
* @param num
*/
public static String getDay2(String day, int num) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = dateAdd(new SimpleDateFormat(YYYYMMDD).parse(day), num, 0);
} catch (ParseException e) {
e.printStackTrace();
}
return new SimpleDateFormat(YYYYMMDD).format(date);
}
/**
* 获取指定日期后日期
*
* @param day 格式:yy-MM-dd
* @param num
*/
public static String getDay(String day, int num) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(day);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day1 = c.get(Calendar.DATE);
c.set(Calendar.DATE, day1 + num);
String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayAfter;
}
/**
* 获取区间时间
*
* @param startTime
* @param endTime
* @return
*/
public static List<String> getRangeDays(String startTime, String endTime) {
// 返回的日期集合
List<String> days = new ArrayList<String>();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
while (tempStart.before(tempEnd)) {
days.add(dateFormat.format(tempStart.getTime()));
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
/**
* 获取区间时间
*
* @param startTime
* @param endTime
* @return
*/
public static List<String> getRangeDaysPlus(String startTime, String endTime) {
// 返回的日期集合
List<String> days = new ArrayList<String>();
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
try {
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
while (tempStart.before(tempEnd)) {
days.add(dateFormat.format(tempStart.getTime()));
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
/**
* 时间格式转换(yyyy-MM-dd HH:mm:ss转为yyyyMMddHHmmss)
*
* @param inTime
* @return
* @throws ParseException
*/
public static String transferFormat(String inTime) {
String outTime = null;
try {
SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat s2 = new SimpleDateFormat("yyyyMMddHHmmss");
Date tempDate = null;
tempDate = s1.parse(inTime);
outTime = s2.format(s2.parse(s1.format(tempDate)));
return outTime;
} catch (ParseException e) {
e.printStackTrace();
}
return outTime;
}
/**
* 获取指定日期是多少周
*
* @param inTime
* @return
*/
public static int getDayIsHowManyWeek(String inTime) {
String result = "";
int week = 0;
try {
result = inTime;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(result);
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
System.out.println();
week = calendar.get(Calendar.WEEK_OF_YEAR);
return week;
} catch (Exception e) {
e.printStackTrace();
}
return week;
}
/**
* 判断是周几
*
* @param datetime
* @return
*/
public static String dateToWeek(String datetime) {
SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
Calendar cal = Calendar.getInstance(); // 获得一个日历
Date datet = null;
try {
datet = f.parse(datetime);
cal.setTime(datet);
} catch (ParseException e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0) {
w = 0;
}
return weekDays[w];
}
/**
* 获取某月的最后一天
*
* @throws
* @param:@param year
* @param:@param month
* @return:String
*/
public static String getLastDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR, year);
//设置月份
cal.set(Calendar.MONTH, month - 1);
//获取某月最大天数
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
/**
* 获取某年某月的第一天
*
* @param:@param year
* @param:@param month
* @return:String
*/
public static String getFisrtDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR, year);
//设置月份
cal.set(Calendar.MONTH, month - 1);
//获取某月最小天数
int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最小天数
cal.set(Calendar.DAY_OF_MONTH, firstDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
String firstDayOfMonth = sdf.format(cal.getTime());
return firstDayOfMonth;
}
/**
* 根据年月判断该月份有多少天
*
* @param year
* @param month
* @return
*/
public static int getYearMonthDays(String year, String month) {
int y = Integer.parseInt(year);
int m = Integer.parseInt(month);
Calendar c = Calendar.getInstance();
c.set(y, m, 0); //输入类型为int类型
int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
return dayOfMonth;
}
/**
* 处理1到12月
*
* @param targetMonth
* @return
*/
public static String handleMonth(String targetMonth) {
String result = targetMonth;
if (Integer.parseInt(result) < 10) {
result = "0" + result;
}
return result;
}
/**
* 判断参数的格式是否为“yyyyMMdd”格式的合法日期字符串
*
* @param str
* @return
*/
public static boolean checkDateFormat1(String str) {
try {
if (str != null && !str.equals("")) {
if (str.length() == 8) {
// 闰年标志
boolean isLeapYear = false;
String year = str.substring(0, 4);
String month = str.substring(4, 6);
String day = str.substring(6, 8);
int vYear = Integer.parseInt(year);
// 判断年份是否合法
if (vYear < 1900 || vYear > 2200) {
return false;
}
// 判断是否为闰年
if (vYear % 4 == 0 && vYear % 100 != 0 || vYear % 400 == 0) {
isLeapYear = true;
}
// 判断月份
// 1.判断月份
if (month.startsWith("0")) {
String units4Month = month.substring(1, 2);
int vUnits4Month = Integer.parseInt(units4Month);
if (vUnits4Month == 0) {
return false;
}
if (vUnits4Month == 2) {
// 获取2月的天数
int vDays4February = Integer.parseInt(day);
if (isLeapYear) {
if (vDays4February > 29) {
return false;
}
} else {
if (vDays4February > 28) {
return false;
}
}
}
} else {
// 2.判断非0打头的月份是否合法
int vMonth = Integer.parseInt(month);
if (vMonth != 10 && vMonth != 11 && vMonth != 12) {
return false;
}
}
// 判断日期
// 1.判断日期
if (day.startsWith("0")) {
String units4Day = day.substring(1, 2);
int vUnits4Day = Integer.parseInt(units4Day);
if (vUnits4Day == 0) {
return false;
}
} else {
// 2.判断非0打头的日期是否合法
int vDay = Integer.parseInt(day);
if (vDay < 10 || vDay > 31) {
return false;
}
}
return true;
} else {
return false;
}
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 获取本周一
*
* @param date
* @return
*/
public static Date getThisWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
return cal.getTime();
}
/**
* 获取本周周一(YYYYMMDD)
*
* @param date
* @return
*/
public static String getThisWeekMondayPlus(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
return sdf.format(cal.getTime());
}
/**
* 获取下周一
*
* @param date
* @return
*/
public static Date getNextWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, 7);
return cal.getTime();
}
/**
* 获得入参日期下周日的日期
*
* @param date 入参日期
* @return 入参日期的下周日
*/
public static Date getNextSunday(Date date) {
//获得入参的日期
Calendar cd = Calendar.getInstance();
cd.setTime(date);
// 获得入参日期是一周的第几天
int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
// 获得入参日期相对于下周日的偏移量(在国外,星期一是一周的第二天,所以下周日相对于本周来说,是第15天)
// 若入参日期是周日,它的下周日偏移量是7
int nextMondayOffset = dayOfWeek == 1 ? 7 : 15 - dayOfWeek;
// 增加到入参日期的下周日
cd.add(Calendar.DAY_OF_MONTH, nextMondayOffset);
return cd.getTime();
}
public static void sleep(int millisecond) {
try {
Thread.sleep(millisecond);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取今日定位的前后某月的第一天、最后一天
*
* @param step 步长
* @return
*/
public static String[] getMonthFristAndLastDay(Integer step) {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
//获取前月的第一天
Calendar cal_1 = Calendar.getInstance();//获取当前日期
cal_1.add(Calendar.MONTH, step);
cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
String firstDay = format.format(cal_1.getTime());
//获取前月的最后一天
Calendar cale = Calendar.getInstance();
cale.set(Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天
String lastDay = format.format(cale.getTime());
return new String[]{firstDay, lastDay};
}
public static Integer getThisYearHappenedDayNoThisMonth() {
Calendar cal = Calendar.getInstance();
return cal.get(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_MONTH);
}
/**
* 返回上一个月
*
* @param month
* @return
*/
public static String getPreMonth(String month) {
Integer m = Integer.parseInt(month);
if (m == 1) {
return "12";
}
Integer pm = m - 1;
if (pm < 10) {
return "0" + pm;
} else {
return pm + "";
}
}
/**
* 返回某月天数
*
* @param year
* @param month
* @return
*/
public static String dayOfMonth(Integer year, Integer month) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
return "31";
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return "30";
} else if (month == 2) {
if (isLeapYear(year)) {
return "29";
} else {
return "28";
}
} else {
return null;
}
}
/**
* 获取月份所属季度
*
* @param month 月份
* @return the quarter by month 所属季度
*/
public static int getQuarterByMonth(int month) {
if (month >= 1 && month <= 12) {
return ((month + 2) / 3);
} else {
return -1;
}
}
/**
* 获取最近5年
*
* @return
*/
public static String[] getRecentFiveYear() {
List<String> years = new ArrayList<>();
years.add(DateUtil.getYearBeforeDynamic(5));
years.add(DateUtil.getYearBeforeDynamic(4));
years.add(DateUtil.getYearBeforeDynamic(3));
years.add(DateUtil.getYearBeforeDynamic(2));
years.add(DateUtil.getYearBeforeDynamic(1));
return years.stream().toArray(String[]::new);
}
/**
* 获取昨天的24完整时间:yyyymmddhh
*
* @param dt
* @return
*/
public static String getYesterDay24Hour(String dt) {
return getDay2(dt.substring(0, 8), -1) + "24";
}
/**
* 返回某年所有日期,升序排列
*
* @param year
* @return
*/
public static String[] getYearDay(Integer year) {
//请注意月份是从0-11,天数是1, 2013-1-1 至 2013-12-31
Calendar start = Calendar.getInstance();
start.set(year, 0, 1); //2013-1-1 开始
Calendar end = Calendar.getInstance();
end.set(year + 1, 0, 0); // 2014--0-0结束,2014-1-1不算
int count = 365;
//获取是否是闰年
if (isLeapYear(year)) {
count = 366;
}
String[] day = new String[count];
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
int index = 0;
while (start.compareTo(end) <= 0) {
day[index] = format.format(start.getTime());
//循环,每次天数加1
start.set(Calendar.DATE, start.get(Calendar.DATE) + 1);
index++;
}
return day;
}
/**
* 获取2个时间(日小时)内所有的小时(日小时),小时定义为1-24
*
* @param dh1
* @param dh2
* @return
*/
public static String[] getAllTimeBetweenDayHour(String dh1, String dh2) {
if (!NumberUtil.isNumber(dh1) || !NumberUtil.isNumber(dh2)) {
return null;
}
if (dh1.length() != 10 || dh2.length() != 10) {
return null;
}
if (dh1.equals(dh2)) {
return new String[]{dh1};
} else if (dh1.compareTo(dh2) > 0) {
return null;
} else {
String sDay = dh1.substring(0, 8);
String eDay = dh2.substring(0, 8);
List<String> allDay = getRangeDaysPlus(sDay, eDay);
String[] tempArr = new String[allDay.size() * 24];
int count = 0;
for (int d = 0; d < allDay.size(); d++) {
for (int h = 1; h < 25; h++) {
if (h < 10) {
tempArr[count] = allDay.get(d) + "0" + h;
} else {
tempArr[count] = allDay.get(d) + h;
}
count++;
}
}
Integer sHour = Integer.parseInt(dh1.substring(8, 10));
Integer eHour = Integer.parseInt(dh2.substring(8, 10));
String[] res = new String[allDay.size() * 24 - (sHour - 1) - (24 - eHour)];
count = 0;
for (int i = 0; i < tempArr.length; i++) {
if (i >= (sHour - 1) && i < ((allDay.size() - 1) * 24 + eHour)) {
res[count] = tempArr[i];
count++;
}
}
return res;
}
}
public static String getYYYY_MM_DD(String targetDate) {
Date date = new Date(targetDate);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dt = simpleDateFormat.format(date);
return dt;
}
public static String getYYYYMMDD(String targetDate) {
Date date = new Date(targetDate);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
String dt = simpleDateFormat.format(date);
return dt;
}
/**
* 获取下一个日期小时
*
* @param targetDH
* @return
*/
public static String getNextDayHour(String targetDH) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYYMMDDHH);
try {
Date date = simpleDateFormat.parse(targetDH);
Date date2 = dateAdd(date, 1, 3);
return simpleDateFormat.format(date2);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 日期格式转化:yyyymmdd->yyyy-mm-dd
*
* @param day
* @return
*/
public static String dayFormat1(String day) {
if (day == null) {
return null;
} else {
if (day.length() != 8) {
return null;
} else {
return day.substring(0, 4) + "-" + day.substring(4, 6) + "-" + day.substring(6, 8);
}
}
}
public static Date tranFormat1(String day) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM);
try {
return simpleDateFormat.parse(day);
} catch (Exception e) {
return null;
}
}
/**
* 将日期格式为yyyymmdd?转换为yyyy-mm-dd格式
*
* @param day
* @return
*/
public static String tranFormat2(String day) {
if (day != null && day.length() >= 8) {
return day.substring(0, 4) + "-" + day.substring(4, 6) + "-" + day.substring(6, 8);
}
return null;
}
/**
* 返回日期格式为yyyymmdd
*
* @param date
* @return
*/
public static String formatDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(date);
}
/**
* 解析String
*
* @param str
* @return
*/
public static Date parse(String str) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
try {
return sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String lastYearDay(String str) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
try {
Date parse = sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(parse);
calendar.add(Calendar.YEAR, -1);
return sdf.format(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 时间戳转换为指定格式日期
*
* @param time
* @return
*/
public static String timeStamp2Date(String time) {
Long timeLong = Long.parseLong(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
Date date;
try {
date = sdf.parse(sdf.format(timeLong));
return sdf.format(date);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 获取当前时间戳
*
* @throws Exception
*/
public static long dateConvertTime() throws Exception {
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d = cn.hutool.core.date.DateUtil.now();
return formatter.parse(d).getTime();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
---------是故无冥冥之志者----------
---------无昭昭之明----------
---------无惛惛之事者----------
---------无赫赫之功----------
本文来自博客园,作者:|是吴啊|,转载请注明原文链接:https://www.cnblogs.com/wuguofeng/p/16623411.html