import org.apache.commons.lang3.StringUtils; import java.io.IOException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtils { /** * 获取当前时间-时间戳 * * @return */ public static int getNowTime() { return Integer.parseInt((System.currentTimeMillis() / 1000) + ""); } public static int getTime(long time) { return Integer.parseInt(String.valueOf((time / 1000))); } public static String dateStr(Date date) { SimpleDateFormat format = new SimpleDateFormat("MM月dd日 hh:mm"); String str = format.format(date); return str; } public static String dateStr(Date date, String f) { SimpleDateFormat format = new SimpleDateFormat(f); String str = format.format(date); return str; } public static String dateStr2(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String str = format.format(date); return str; } public static String newdateStr2(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); String str = format.format(date); return str; } public static String newdateStr6(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日"); String str = format.format(date); return str; } public static String newdateStr3(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyMMdd"); String str = format.format(date); return str; } public static String dateStr3(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); String str = format.format(date); return str; } public static String dateStr4(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str = format.format(date); return str; } public static String dateStr5(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); String str = format.format(date); return str; } public static String dateStr6(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH"); String str = format.format(date); return str; } public static String getYearStr(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy"); String str = format.format(date); return str; } public static Date getDate(String time, String formatStr) { SimpleDateFormat format = new SimpleDateFormat(formatStr); try { return format.parse(time); } catch (ParseException e) { } return null; } /** * 将秒转换成时间 * * @param times * @return */ public static Date getDate(String times) { long time = Long.parseLong(times); return new Date(time * 1000); } public static String dateStr(String times) { return dateStr(getDate(times)); } public static String dateStr2(String times) { return dateStr2(getDate(times)); } public static String dateStr3(String times) { return dateStr3(getDate(times)); } public static String dateStr4(String times) { return dateStr4(getDate(times)); } public static long getTime(Date date) { return date.getTime() / 1000; } public static int getDay(Date d) { Calendar cal = Calendar.getInstance(); cal.setTime(d); return cal.get(Calendar.DAY_OF_MONTH); } /** * s - 表示 "yyyy-mm-dd" 形式的日期的 String 对象 * * @param * @return */ public static Date valueOf(String s) { final int YEAR_LENGTH = 4; final int MONTH_LENGTH = 2; final int DAY_LENGTH = 2; final int MAX_MONTH = 12; final int MAX_DAY = 31; int firstDash; int secondDash; Date d = null; if (s == null) { throw new IllegalArgumentException(); } firstDash = s.indexOf('-'); secondDash = s.indexOf('-', firstDash + 1); if ((firstDash > 0) && (secondDash > 0) && (secondDash < s.length() - 1)) { String yyyy = s.substring(0, firstDash); String mm = s.substring(firstDash + 1, secondDash); String dd = s.substring(secondDash + 1); if (yyyy.length() == YEAR_LENGTH && mm.length() == MONTH_LENGTH && dd.length() == DAY_LENGTH) { int year = Integer.parseInt(yyyy); int month = Integer.parseInt(mm); int day = Integer.parseInt(dd); if (month >= 1 && month <= MAX_MONTH) { int maxDays = MAX_DAY; switch (month) { // February determine if a leap year or not case 2: if ((year % 4 == 0 && !(year % 100 == 0)) || (year % 400 == 0)) { maxDays = MAX_DAY - 2; // leap year so 29 days in February } else { maxDays = MAX_DAY - 3; // not a leap year so 28 days in February } break; // April, June, Sept, Nov 30 day months case 4: case 6: case 9: case 11: maxDays = MAX_DAY - 1; break; } if (day >= 1 && day <= maxDays) { Calendar cal = Calendar.getInstance(); cal.set(year, month - 1, day, 0, 0, 0); cal.set(Calendar.MILLISECOND, 0); d = cal.getTime(); } } } } if (d == null) { throw new IllegalArgumentException(); } return d; } /** * @param * @param * @return 返回Map 获取相隔多少年 get("YEAR")及为俩个时间年只差,月 天,类推 * Key : * YEAR MONTH DAY * 如果开始时间 晚于 结束时间 return null; * @author lijie */ public static Date rollDay(Date d, int day) { Calendar cal = Calendar.getInstance(); cal.setTime(d); cal.add(Calendar.DAY_OF_MONTH, day); return cal.getTime(); } public static Date rollMon(Date d, int mon) { Calendar cal = Calendar.getInstance(); cal.setTime(d); cal.add(Calendar.MONTH, mon); return cal.getTime(); } public static Date rollYear(Date d, int year) { Calendar cal = Calendar.getInstance(); cal.setTime(d); cal.add(Calendar.YEAR, year); return cal.getTime(); } public static Date rollDate(Date d, int year, int mon, int day) { Calendar cal = Calendar.getInstance(); cal.setTime(d); cal.add(Calendar.YEAR, year); cal.add(Calendar.MONTH, mon); cal.add(Calendar.DAY_OF_MONTH, day); return cal.getTime(); } public static Date rollSecond(Date d, int second) { Calendar cal = Calendar.getInstance(); cal.setTime(d); cal.add(Calendar.SECOND, second); return cal.getTime(); } public static Date rollHour(Date d, int hour) { Calendar cal = Calendar.getInstance(); cal.setTime(d); cal.add(Calendar.HOUR_OF_DAY, hour); return cal.getTime(); } /** * 获取当前时间的秒数字符串 * * @return */ public static String getNowTimeStr() { String str = Long.toString(System.currentTimeMillis() / 1000); return str; } public static String getTimeStr(Date time) { long date = time.getTime(); String str = Long.toString(date / 1000); return str; } public static Date getIntegralTime() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); } public static Date getLastIntegralTime() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); } public static Date getLastSecIntegralTime(Date d) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(d.getTime()); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); } public static Date getSecIntegralTime(Date d) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(d.getTime()); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); } public static long getTime(String format) { long t = 0; if (StringUtils.isBlank(format)) return t; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date; try { date = sdf.parse(format); t = date.getTime() / 1000; } catch (ParseException e) { e.printStackTrace(); } return t; } public static Date getDate2(String str) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = format.parse(str); } catch (ParseException e) { e.printStackTrace(); } return date; } public static Date getDate3(String str) { SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); Date date = null; try { date = format.parse(str); } catch (ParseException e) { e.printStackTrace(); } return date; } public static Date getDateYYYYMMdd(String str) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = format.parse(str); } catch (ParseException e) { e.printStackTrace(); } return date; } /** * 获取下一天 默认 格式 yyyy-MM-dd 00:00:00 * * @param date * @return */ public static Date getNextDayYYYYMMdd(Date date) { date = rollDay(date, 1); return getDateYYYYMMdd(dateStr2(date)); } public static String getYYYYMMddHHmmss(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (date == null) { return null; } try { return sdf.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } public static String getYYYYMMddHHmm(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); if (date == null) { return null; } try { return sdf.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } public static String getHHmm(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); if (date == null) { return null; } try { return sdf.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } public static String getHH(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("HH"); if (date == null) { return null; } try { return sdf.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } public static String getYYYYMMdd(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); if (date == null) { return null; } try { return sdf.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } public static String getMMdd(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("MM-dd"); if (date == null) { return null; } try { return sdf.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } public static String getMMdd2(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("M/d"); if (date == null) { return null; } try { return sdf.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } public static String getDayOfWeekly(Date d) { String result; Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(d.getTime()); boolean isFirstSunday = (cal.getFirstDayOfWeek() == Calendar.SUNDAY); int n = cal.get(Calendar.DAY_OF_WEEK); n = isFirstSunday ? (n - 1) : n; switch (n) { case 0: result = "星期日"; break; case 1: result = "星期一"; break; case 2: result = "星期二"; break; case 3: result = "星期三"; break; case 4: result = "星期四"; break; case 5: result = "星期五"; break; case 6: result = "星期六"; break; case 7: result = "星期日"; break; default: result = ""; } return result; } public static String getMMddHHmmss(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss"); if (date == null) { return null; } try { return sdf.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } public static String getMMddHHmmss_0(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); if (date == null) { return null; } try { return sdf.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } public static String getMMddHHmmssms(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss,SSS"); if (date == null) { return null; } try { return sdf.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } public static boolean valid(String str) { DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date date = (Date) formatter.parse(str); return str.equals(formatter.format(date)); } catch (Exception e) { return false; } } /** * 计算时间过后几天的时间戳 * * @param date 具体时间 * @param day 几天之后 * @return 时间 */ public static String getStrDateAfter(Date date, int day) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(date.getTime()); cal.add(Calendar.DATE, day); SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); String str = format.format(cal.getTime()); return str; } public static long getDaySub(String beginDateStr, String endDateStr) { long day = 0; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date beginDate; Date endDate; try { beginDate = format.parse(beginDateStr); endDate = format.parse(endDateStr); day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000); } catch (ParseException e) { e.printStackTrace(); } return day; } /** * 计算两个日期之间相差的天数 * * @param smdate 较小的时间 * @param bdate 较大的时间 * @return 相差天数 * @throws ParseException */ public static int daysBetween(Date bdate, Date smdate) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); smdate = sdf.parse(sdf.format(smdate)); bdate = sdf.parse(sdf.format(bdate)); Calendar cal = Calendar.getInstance(); cal.setTime(smdate); long time1 = cal.getTimeInMillis(); cal.setTime(bdate); long time2 = cal.getTimeInMillis(); long between_days = (time2 - time1) / (1000 * 3600 * 24); return Integer.parseInt(String.valueOf(between_days)); } /** * 计算两个日期之间相差的秒数 * * @param smdate 较小的时间 * @param bdate 较大的时间 * @return 相差天数 * @throws ParseException */ public static int secondsBetween(Date bdate, Date smdate) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); smdate = sdf.parse(sdf.format(smdate)); bdate = sdf.parse(sdf.format(bdate)); Calendar cal = Calendar.getInstance(); cal.setTime(smdate); long time1 = cal.getTimeInMillis(); cal.setTime(bdate); long time2 = cal.getTimeInMillis(); long between_days = (time2 - time1) / 1000; return Integer.parseInt(String.valueOf(between_days)); } /** * 获取本周的第一天 * @return String **/ public static String getWeekStart() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.WEEK_OF_MONTH, 0); cal.set(Calendar.DAY_OF_WEEK, 2); Date time = cal.getTime(); return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time); } /** * 获取本周的最后一天 * @return String **/ public static String getWeekEnd() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK)); cal.add(Calendar.DAY_OF_WEEK, 1); Date time = cal.getTime(); return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time); } /** * 获取月份第一天 * @return */ public static String getFirstDayOfMonth() { Calendar thisMonthFirstDateCal = Calendar.getInstance(); thisMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, 1); return new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(thisMonthFirstDateCal.getTime()); } /** * 获取本月的最后一天 * @return String **/ public static String getMonthEnd() { Calendar thisMonthEndDateCal = Calendar.getInstance(); thisMonthEndDateCal.set(Calendar.DAY_OF_MONTH, thisMonthEndDateCal.getActualMaximum(Calendar.DAY_OF_MONTH)); return new SimpleDateFormat("yyyy-MM-dd 23:59:59").format(thisMonthEndDateCal.getTime()); } public static int getWorkDays(Date startTime, Date endTime) throws IOException { int workDays = 0; // 日期格式化 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { // 起始日期 Date begin = simpleDateFormat.parse(simpleDateFormat.format(startTime)); // 结束日期 Date end = simpleDateFormat.parse(simpleDateFormat.format(endTime)); Calendar calendar = Calendar.getInstance(); calendar.setTime(begin); while (begin.getTime() <= end.getTime()) { //获取当前日期是周几 int week = calendar.get(Calendar.DAY_OF_WEEK) - 1; calendar.add(Calendar.DAY_OF_MONTH, 1); if (week != 0 && week != 6) {//0为周日,6为周六 workDays += 1; } begin = calendar.getTime(); } } catch (Exception e) { e.printStackTrace(); } SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd"); int startTime1 = Integer.parseInt(simpleDateFormat1.format(startTime)); int endTime1 = Integer.parseInt(simpleDateFormat1.format(endTime)); // InputStream inputStream = VmController.class.getResourceAsStream("/data.properties"); // Properties properties = new Properties(); // properties.load(inputStream); String holData = "20230102,20230123,20230124,20230125,20230126,20230127,20230405,20230501,20230502,20230503,20230622,20230623,20230929,20231002,20231003,20231004,20231005,20231006" + ",20240101,20240212,20240213,20240214,20240215,20240216,20240404,20240405,20240501,20240502,20240503,20240610,20240916,20240917" + ",20241001,20241002,20241003,20241004,20241007"; String[] split = holData.split(","); for (String s : split) { int i = Integer.parseInt(s); if (startTime1 <= i && i <= endTime1){ workDays--; } } return workDays; } }
java-日期工具类
于 2024-12-05 15:50:51 首次发布