123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606 |
- package com.ruixuan.common.utils;
- import org.apache.commons.lang3.time.DateFormatUtils;
- import java.lang.management.ManagementFactory;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.time.LocalTime;
- import java.time.ZoneId;
- import java.time.ZonedDateTime;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * 时间工具类
- *
- * @author ruoyi
- */
- public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
- public static String YYYY = "yyyy";
- public static String YYYY_MM = "yyyy-MM";
- public static String YYYY_MM_DD = "yyyy-MM-dd";
- public static String YYYYMMDD = "yyyyMMdd";
- public static String YYMMDD = "yyMMdd";
- public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
- public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
- private static String[] parsePatterns = {
- "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
- "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
- "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
- /**
- * 获取当前Date型日期
- *
- * @return Date() 当前日期
- */
- public static Date getNowDate() {
- return new Date();
- }
- public static String getNowDate(String format) {
- SimpleDateFormat sdf = new SimpleDateFormat(format);
- Date date = new Date();
- return sdf.format(date);
- }
- public static String getNowDateStr() {
- SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd");
- return simp.format(new Date());
- }
- /**
- * 获取当前日期, 默认格式为yyyy-MM-dd
- *
- * @return String
- */
- public static String getDate() {
- return dateTimeNow(YYYY_MM_DD);
- }
- public static final String getTime() {
- return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
- }
- public static final String dateTimeNow() {
- return dateTimeNow(YYYYMMDDHHMMSS);
- }
- public static final String dateTimeNow(final String format) {
- return parseDateToStr(format, new Date());
- }
- public static final String dateTime(final Date date) {
- return parseDateToStr(YYYY_MM_DD, date);
- }
- public static final String parseDateToStr(final String format, final Date date) {
- return new SimpleDateFormat(format).format(date);
- }
- public static final Date dateTime(final String format, final String ts) {
- try {
- return new SimpleDateFormat(format).parse(ts);
- } catch (ParseException e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * 日期路径 即年/月/日 如2018/08/08
- */
- public static final String datePath() {
- Date now = new Date();
- return DateFormatUtils.format(now, "yyyy/MM/dd");
- }
- /**
- * 日期路径 即年/月/日 如20180808
- */
- public static final String dateTime() {
- Date now = new Date();
- return DateFormatUtils.format(now, "yyyyMMdd");
- }
- /**
- * 日期型字符串转化为日期 格式
- */
- public static Date parseDate(Object str) {
- if (str == null) {
- return null;
- }
- try {
- return parseDate(str.toString(), parsePatterns);
- } catch (ParseException e) {
- return null;
- }
- }
- /**
- * 获取服务器启动时间
- */
- public static Date getServerStartDate() {
- long time = ManagementFactory.getRuntimeMXBean().getStartTime();
- return new Date(time);
- }
- /**
- * 计算相差天数
- */
- public static int differentDaysByMillisecond(Date date1, Date date2) {
- return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
- }
- /**
- * 计算两个时间差
- */
- public static String getDatePoor(Date endDate, Date nowDate) {
- long nd = 1000 * 24 * 60 * 60;
- long nh = 1000 * 60 * 60;
- long nm = 1000 * 60;
- // long ns = 1000;
- // 获得两个时间的毫秒时间差异
- long diff = endDate.getTime() - nowDate.getTime();
- // 计算差多少天
- long day = diff / nd;
- // 计算差多少小时
- long hour = diff % nd / nh;
- // 计算差多少分钟
- long min = diff % nd % nh / nm;
- // 计算差多少秒//输出结果
- // long sec = diff % nd % nh % nm / ns;
- return day + "天" + hour + "小时" + min + "分钟";
- }
- /**
- * 增加 LocalDateTime ==> Date
- */
- public static Date toDate(LocalDateTime temporalAccessor) {
- ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
- return Date.from(zdt.toInstant());
- }
- /**
- * 增加 LocalDate ==> Date
- */
- public static Date toDate(LocalDate temporalAccessor) {
- LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
- ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
- return Date.from(zdt.toInstant());
- }
- /**
- * 时间字符串转long类型
- * 2022-05-01 ===>>>> 20220501
- *
- * @param date
- * @return
- */
- public static Long strDateToInt(String date) {
- return Long.valueOf(date.replaceAll("-", ""));
- }
- /**
- * 时间字符串转long类型
- * 2022-05-01 00:00:00 ===>>>> 20220501
- *
- * @param date
- * @return
- */
- public static Long strDateTimsToInt(String date) {
- String[] datas = date.split(" ");
- return Long.valueOf(datas[0].replaceAll("-", ""));
- }
- /**
- * @param
- * @description: 获取当前周的开始和截至时间
- * @return: java.util.Map
- * @author: zianY
- * @time: 2022/6/1
- */
- public static Map getWeekBeginAndEnd() {
- Map<String, String> timeMap = new HashMap<>();
- String begin = parseDateToStr(YYYY_MM_DD, getBeginDayOfWeek());
- String end = parseDateToStr(YYYY_MM_DD, getEendDayOfWeek());
- timeMap.put("begin", begin + " 00:00:00");
- timeMap.put("end", end + " 23:59:59");
- return timeMap;
- }
- /**
- * @param
- * @description: 获取本周开始时间
- * @return: java.util.Date
- * @author: zianY
- * @time: 2022/6/1
- */
- public static Date getBeginDayOfWeek() {
- Date date = new Date();
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
- if (dayOfWeek == 1) {
- dayOfWeek += 7;
- }
- cal.add(Calendar.DATE, 2 - dayOfWeek);
- return cal.getTime();
- }
- /**
- * @param
- * @description: 获取本周结束时间
- * @return: java.util.Date
- * @author: zianY
- * @time: 2022/6/1
- */
- public static Date getEendDayOfWeek() {
- Calendar cal = Calendar.getInstance();
- cal.setTime(getBeginDayOfWeek());
- cal.add(Calendar.DAY_OF_WEEK, 6);
- return cal.getTime();
- }
- /**
- * 指定模式的时间格式
- */
- private static SimpleDateFormat getSDFormat(String pattern) {
- return new SimpleDateFormat(pattern);
- }
- /**
- * 指定日期按指定格式显示
- */
- public static String formatDate(Date date, String pattern) {
- return getSDFormat(pattern).format(date);
- }
- /**
- * 日期转换为时间戳
- */
- public static long timeToStamp(String timers) {
- Date d = new Date();
- long timeStemp = 0;
- try {
- SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- // 日期转换为时间戳
- d = sf.parse(timers);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- timeStemp = d.getTime();
- return timeStemp;
- }
- /**
- * 时间戳转换为字符串
- *
- * @param time
- * @return
- */
- public static String timestamptoStr(Long timestamp) {
- Date date = new Date();
- date.setTime(timestamp);
- return DateFormatUtils.format(date, YYYYMMDD);
- }
- /**
- * 时间戳转换为小时(0-23)
- */
- public static Integer timestamptoHour(Long timestamp) {
- Date date = new Date();
- date.setTime(timestamp);
- String hh = DateFormatUtils.format(date, "HH");
- return Integer.valueOf(hh);
- }
- /**
- * 时间戳转换为字符串
- *
- * @param time
- * @return
- */
- public static String timestamptoMinutesAndSecondsStr(Long timestamp) {
- Date date = new Date();
- date.setTime(timestamp);
- return DateFormatUtils.format(date, YYYY_MM_DD_HH_MM_SS);
- }
- /**
- * 返回 日期-天数 的string 日期
- *
- * @param time 时间
- * @param daysBetween 天数
- * @return
- */
- public static String getSubtractTime(Date time, Integer daysBetween) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Calendar rightNow = Calendar.getInstance();
- rightNow.setTime(time);
- if (daysBetween.intValue() > 0) {
- rightNow.add(Calendar.DAY_OF_YEAR, -daysBetween.intValue());
- } else {
- rightNow.add(Calendar.DAY_OF_YEAR, -1);//日期 -1
- }
- Date rightNowTime = rightNow.getTime();
- String nowTime = sdf.format(rightNowTime);
- return nowTime;
- }
- /**
- * 返回 日期-天数 的string 日期
- *
- * @param time 时间
- * @param daysBetween 天数
- * @return
- */
- public static String getSubtractTime2(Date time, Integer daysBetween) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Calendar rightNow = Calendar.getInstance();
- rightNow.setTime(time);
- if (daysBetween.intValue() > 0) {
- rightNow.add(Calendar.DAY_OF_YEAR, -daysBetween.intValue() - 1);
- } else {
- rightNow.add(Calendar.DAY_OF_YEAR, -1);//日期 -1
- }
- Date rightNowTime = rightNow.getTime();
- String nowTime = sdf.format(rightNowTime);
- return nowTime;
- }
- /**
- * 根据时间获取季度
- * 1即Q1(1,2,3月),以此类推
- *
- * @param date
- * @return
- */
- public static int getQuarter(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- //获取当前月份
- int month = cal.get(Calendar.MONTH) + 1;
- if (month <= 3) {
- return 1;
- } else if (month <= 6) {
- return 2;
- } else if (month <= 9) {
- return 3;
- } else if (month <= 12) {
- return 4;
- }
- return 0;
- }
- public static Long getStartLongTime(String orderStartDate) {
- String date = orderStartDate + " 00:00:00";
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Long orderCreateTimeStart = null;
- try {
- orderCreateTimeStart = sdf.parse(date).getTime();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return orderCreateTimeStart;
- }
- public static Integer getIntegerTime(String date) {
- String replace = date.replace("-", "");
- return Integer.valueOf(replace);
- }
- public static Long getEndLongTime(String orderEndDate) {
- String date = orderEndDate + " 23:59:59";
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Long orderCreateTimeEnd = null;
- try {
- orderCreateTimeEnd = sdf.parse(date).getTime();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return orderCreateTimeEnd;
- }
- public static Integer getNowHour() {
- Calendar cal = Calendar.getInstance();
- Date date = new Date();
- cal.setTime(date);
- return cal.get(Calendar.HOUR_OF_DAY);
- }
- public static String getAnotherDay(String format, String date, Integer num) {
- SimpleDateFormat sdf = new SimpleDateFormat(format);
- Date getDate = null;
- try {
- getDate = sdf.parse(date);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(getDate);
- calendar.add(Calendar.DAY_OF_MONTH, num);
- Date resultDate = calendar.getTime();
- return sdf.format(resultDate);
- }
- public static Long getNowEndHourTemp(String dateStr, Integer hour) {
- String date = dateStr + " " + hour + ":59:59";
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Long orderCreateTimeEnd = null;
- try {
- orderCreateTimeEnd = sdf.parse(date).getTime();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return orderCreateTimeEnd;
- }
- /**
- * 获取当前月份第一天
- *
- * @return 返回格式:2022-01-19
- */
- public static String getFirstDayByMonth() {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Calendar c = Calendar.getInstance();
- c.add(Calendar.MONTH, 0);
- c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
- return format.format(c.getTime());
- }
- public static String getLastMonthFirstDay() {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Calendar c = Calendar.getInstance();
- c.add(Calendar.MONTH, -1);
- c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
- return format.format(c.getTime());
- }
- public static String getLastMonthFirstDayByDate(String date) throws ParseException {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Calendar c = Calendar.getInstance();
- c.setTime(format.parse(date));
- c.add(Calendar.MONTH, -1);
- c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
- return format.format(c.getTime());
- }
- public static String getLastMonthNowDay(String date) throws ParseException {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Calendar c = Calendar.getInstance();
- c.setTime(format.parse(date));
- c.add(Calendar.MONTH, -1);
- // c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
- return format.format(c.getTime());
- }
- public static String getMonthStartDay(String date) throws ParseException {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Calendar c = Calendar.getInstance();
- c.setTime(format.parse(date));
- // c.add(Calendar.MONTH, -1);
- c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
- return format.format(c.getTime());
- }
- /**
- * 获取 两个日期之间的所有 日期
- *
- * @param startDate yyyy-MM-dd
- * @param endDate yyyy-MM-dd
- * @return
- */
- public static List<String> getAllDatesOfTwoTimes(String startDate, String endDate) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- List<String> dateList = new ArrayList<String>();
- try {
- Date dateOne = sdf.parse(startDate);
- Date dateTwo = sdf.parse(endDate);
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(dateOne);
- dateList.add(startDate);
- while (calendar.getTime().before(dateTwo)) {
- calendar.add(Calendar.DAY_OF_MONTH, 1);
- dateList.add(sdf.format(calendar.getTime()));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return dateList;
- }
- public static String tempToDate(long millis) {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- Date d1 = new Date(millis);
- String d2 = simpleDateFormat.format(d1);
- return d2;
- }
- public static Integer getHour(String dateStr) {
- try {
- DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Calendar cal = Calendar.getInstance();
- Date date = sdf.parse(dateStr);
- cal.setTime(date);
- return cal.get(Calendar.HOUR_OF_DAY);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 根据本阶段日期 获取上阶段 开始 和 截至 日期
- *
- * @param startTime 本阶段开始时间
- * @param endTime 本阶段截至时间
- * @return
- * @throws ParseException
- */
- public static Map<String, String> getStartEndTime(String startTime, String endTime) {
- Map<String, String> timeMap = new HashMap();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Date start = null;
- Date end = null;
- try {
- start = sdf.parse(startTime);
- end = sdf.parse(endTime);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- //相差天数
- Long daysBetween = (end.getTime() - start.getTime()) / (60 * 60 * 24 * 1000);
- String lastTimeStart = getSubtractTime2(start, daysBetween.intValue());
- String lastTimeEnd = getSubtractTime2(end, daysBetween.intValue());
- timeMap.put("lastTimeStart", lastTimeStart);
- timeMap.put("lastTimeEnd", lastTimeEnd);
- timeMap.put("daysBetween", daysBetween.toString());
- return timeMap;
- }
- public static void main(String[] args) {
- Map<String, String> map = getStartEndTime("2024-09-12", "2024-09-18");
- //下个周期的开始时间
- String lastTimeStart = map.get("lastTimeStart");
- //下个周期的结束时间
- String lastTimeEnd = map.get("lastTimeEnd");
- System.out.println(map);
- System.out.println(lastTimeStart);
- System.out.println(lastTimeEnd);
- }
- }
|