package com.template.common.utils; import com.template.model.enumModel.eWeekStatu; import com.template.model.pojo.UnitTimeHelpModel; import com.template.model.vo.ClassSettingDateVo; import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateUtils; import java.math.BigDecimal; import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.*; /** * 时间转化工具 date转为时间戳 时间戳转date 互相与String的转换 * 所有出现的String time 格式都必须为(yyyy-MM-dd HH:mm:ss),否则出错 * */ public class TimeExchange { /** * String(yyyy-MM-dd HH:mm:ss) 转 Date * * @param time * @return * @throws ParseException */ // String date = "2010/05/04 12:34:23"; public static Date StringToDate(String time, String formatDate){ Date date = new Date(); // 注意format的格式要与日期String的格式相匹配 DateFormat dateFormat = new SimpleDateFormat(formatDate); try { date = dateFormat.parse(time); } catch (Exception e) { e.printStackTrace(); } return date; } /** * Date转为String(yyyy-MM-dd HH:mm:ss) * * @param time * @return */ public static String DateToString(Date time) { String dateStr = ""; Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:sss"); try { dateStr = dateFormat.format(time); System.out.println(dateStr); } catch (Exception e) { e.printStackTrace(); } return dateStr; } public static String chineseDateTime(Date time){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分"); String dateStringParse = null; try { dateStringParse = sdf.format(time); } catch (Exception e) { e.printStackTrace(); } return dateStringParse; } /** * String(yyyy-MM-dd HH:mm:ss)转10位时间戳 * * @param time * @return */ public static Integer StringToTimestamp(String time) { int times = 0; try { times = (int) ((Timestamp.valueOf(time).getTime()) / 1000); } catch (Exception e) { e.printStackTrace(); } if (times == 0) { System.out.println("String转10位时间戳失败"); } return times; } /** * 10位int型的时间戳转换为String(yyyy-MM-dd HH:mm:ss) * * @param time * @return */ public static String timestampToString(Integer time) { //int转long时,先进行转型再进行计算,否则会是计算结束后在转型 long temp = (long) time * 1000; Timestamp ts = new Timestamp(temp); String tsStr = ""; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { //方法一 tsStr = dateFormat.format(ts); System.out.println(tsStr); } catch (Exception e) { e.printStackTrace(); } return tsStr; } /** * 10位时间戳转Date * * @param time * @return */ public static Date TimestampToDate(Integer time) { long temp = (long) time * 1000; Timestamp ts = new Timestamp(temp); Date date = new Date(); try { date = ts; //System.out.println(date); } catch (Exception e) { e.printStackTrace(); } return date; } /** * 获取当前时间戳 * @return */ public static String DateNowTimeStamo(){ Date date = new Date();//获取当前的日期 SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");//设置日期格式 String str = df.format(date);//获取String类型的时间 return str; } /** * Date类型转换为10位时间戳 * * @param time * @return */ public static Integer DateToTimestamp(Date time) { Timestamp ts = new Timestamp(time.getTime()); return (int) ((ts.getTime()) / 1000); } // 当前时间减1小时 public static String TimeDesH(Date time, int hour) { Calendar nowTime2 = Calendar.getInstance(); nowTime2.setTime(time); nowTime2.add(Calendar.HOUR, -hour); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return simpleDateFormat.format(nowTime2.getTime()); } // 当前时间加5分钟 public static String TimeRangeI(String time) throws ParseException { // 当前时间+5分钟 Date endTime = DateUtils.addMinutes(StringToDate(time, "yyyy-MM-dd HH:mm:ss"), 300); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return simpleDateFormat.format(endTime); } // 当前时间加2分钟 public static String TimeRangeI10(String time, int m) { Calendar nowTime2 = Calendar.getInstance(); nowTime2.setTime(StringToDate(time, "yyyy-MM-dd HH:mm:ss")); nowTime2.add(Calendar.SECOND, m);//10分钟前的时间 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return simpleDateFormat.format(nowTime2.getTime()); } // 当前时间减5分钟 public static String TimeRangeD(String time) throws ParseException { Calendar nowTime2 = Calendar.getInstance(); nowTime2.setTime(StringToDate(time, "yyyy-MM-dd HH:mm:ss")); nowTime2.add(Calendar.MINUTE, -300);//5分钟前的时间 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return simpleDateFormat.format(nowTime2.getTime()); } // 获取当前日期 public static String getDate() { SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd"); return sp.format(new Date()); } // 获取当前日期的年月 public static String getDateMonth() { SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-"); return sp.format(new Date()); } // 获取前天 public static String getQianDay() throws ParseException { Calendar nowTime2 = Calendar.getInstance(); nowTime2.setTime(StringToDate(getTime(), "yyyy-MM-dd HH:mm:ss")); nowTime2.add(Calendar.DATE, -5);//5分钟前的时间 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return simpleDateFormat.format(nowTime2.getTime()); } // 获取当前时间 public static String getTime() { SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sp.format(new Date()); } public static String getOnlyMM() { SimpleDateFormat sp = new SimpleDateFormat("HH:mm"); return sp.format(new Date()); } public static String getOnlyDesMM() throws ParseException { Calendar nowTime2 = Calendar.getInstance(); nowTime2.setTime(StringToDate(getTime(), "yyyy-MM-dd HH:mm:ss")); nowTime2.add(Calendar.MINUTE, -5);//5分钟前的时间 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"); return simpleDateFormat.format(nowTime2.getTime()); } public static String getYear() { SimpleDateFormat sp = new SimpleDateFormat("yyyy"); return sp.format(new Date()); } public static String getMonth() { SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM"); return sp.format(new Date()); } public static String getNowMonth() { SimpleDateFormat sp = new SimpleDateFormat("MM"); return sp.format(new Date()); } // 获取当前时间 public static String getOnlyTime() { SimpleDateFormat sp = new SimpleDateFormat("HH:mm:ss"); return sp.format(new Date()); } /** * 计算两个日期的时间差 * * @param time1 * @param time2 * @return */ public static double getTimeDifference(String time1, String time2) { SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long t1 = 0L; long t2 = 0L; try { t1 = timeformat.parse(time1).getTime(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { t2 = timeformat.parse(time2).getTime(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } //因为t1-t2得到的是毫秒级,所以要初3600000得出小时.算天数或秒同理 double hours = (double) ((t2 - t1) / 3600000); double minutes = (double) (((t2 - t1) / 1000 - hours * 3600) / 60 / 60); return hours + minutes; } public static double getOnlyTimeDifference(String time1, String time2) { SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm:ss"); long t1 = 0L; long t2 = 0L; try { t1 = timeformat.parse(time1).getTime(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { t2 = timeformat.parse(time2).getTime(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } //因为t1-t2得到的是毫秒级,所以要初3600000得出小时.算天数或秒同理 double hours = (double) ((t2 - t1) / 3600000); double minutes = (double) (((t2 - t1) / 1000 - hours * 3600) / 60 / 60); return hours + minutes; } public static double getDiff(String str1, String str2) { return str2.compareTo(str1); } /** * 时间加减分钟数 * * @param time 时间 * @param FormatStr 时间格式 * @param amount 要加减的时间(单位为分钟) * @return * @throws ParseException */ public static String TimeRangeMinute(String time, int amount, String FormatStr) throws ParseException { Date endTime = DateUtils.addMinutes(ShortStringToDate(time, FormatStr), amount); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FormatStr); return simpleDateFormat.format(endTime); } /** * 时间加减小时数 * * @param time 时间 * @param FormatStr 时间格式 * @param amount 要加减的时间(单位为小时) * @return * @throws ParseException */ public static String TimeRangeHour(Date time, int amount, String FormatStr) { Date endTime = DateUtils.addHours(time, amount); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FormatStr); return simpleDateFormat.format(endTime); } /** * String 转 Date * * @param time 时间 * @param formatStr 自定义时间格式 * @return * @throws ParseException */ public static Date ShortStringToDate(String time, String formatStr) throws ParseException { Date date = new Date(); // 注意format的格式要与日期String的格式相匹配 DateFormat dateFormat = new SimpleDateFormat(formatStr); try { date = dateFormat.parse(time); System.out.println(date.toString()); } catch (Exception e) { e.printStackTrace(); } return date; } /** * Date转为String * * @param time 时间 * @param FormatStr 自定义时间格式 * @return */ public static String DateToString(Date time, String FormatStr) { String dateStr = ""; DateFormat dateFormat = new SimpleDateFormat(FormatStr); try { dateStr = dateFormat.format(time); } catch (Exception e) { e.printStackTrace(); } return dateStr; } /** * 日期路径 即年/月/日 如2018/08/08 */ public static final String datePath() { Date now = new Date(); return DateFormatUtils.format(now, "yyyy/MM/dd"); } /** * 比较时间1是否小于时间2 * 如果时间1小于时间2,接口返回true * 如果时间1大于时间2,接口返回false * * @param dateOne 时间1 * @param dateTwo 时间2 * @param Forma 时间格式 * @return * @throws ParseException */ public static boolean CompareDate(String dateOne, String dateTwo, String Forma) throws ParseException { SimpleDateFormat df = new SimpleDateFormat(Forma); Date sd1 = df.parse(dateOne); Date sd2 = df.parse(dateTwo); return sd1.before(sd2); } public static LocalDateTime StringToLocalTime(String time, String formatStr){ //1.具有转换功能的对象 DateTimeFormatter df = DateTimeFormatter.ofPattern(formatStr); //3.LocalDate发动,将字符串转换成  df格式的LocalDateTime对象,的功能 LocalDateTime LocalTime = LocalDateTime.parse(time,df); return LocalTime; } /** * 获取一周的开始时间和结束时间 * 获取本周星期一作为一周的第一天的起始时间和结束时间 * * @return 返回的数据中第一个是开始时间 第二个是结束时间 */ public static String[] getCurrentWeekTimeFrame() { Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(TimeZone.getTimeZone("GMT+8")); //start of the week if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { calendar.add(Calendar.DAY_OF_YEAR, -1); } calendar.add(Calendar.DAY_OF_WEEK, -(calendar.get(Calendar.DAY_OF_WEEK) - 2)); //给0的时候查不出数据 // calendar.set(Calendar.HOUR_OF_DAY, 0); // calendar.set(Calendar.MINUTE, 0); // calendar.set(Calendar.SECOND, 0); // calendar.set(Calendar.MILLISECOND, 0); String startTime = DateToString(calendar.getTime(), "yyyy-MM-dd"); //end of the week calendar.add(Calendar.DAY_OF_WEEK, 6); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); String endTime = DateToString(calendar.getTime(), "yyyy-MM-dd"); return new String[]{startTime, endTime}; } /** * 获取指定月份有多少天 * * @param month * @return */ public static int getMonthDays(String date, int month) { int year = Integer.valueOf(date.substring(0, 4)); int[] arr = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int day = arr[month - 1];//天数对应=数组-1 if (month == 2 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { day = 29; } return day; } /** * 获取指定月份的第一天和最后一天 * * @param DateStr 指定月份 * @return 返回的数据中第一个是开始时间 第二个是结束时间 */ public static String[] getCurrentMonthTimeFrame(String DateStr) { Calendar c = Calendar.getInstance();//获取Calendar实例 c.set(Calendar.YEAR, Integer.parseInt(DateStr.substring(0, 4))); int sss = Integer.parseInt(DateStr.substring(5, 7)); c.set(Calendar.MONTH, Integer.parseInt(DateStr.substring(5, 7).replace("-", "")) - 1); c.set(Calendar.DAY_OF_MONTH, 1); String startDate = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()); int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH); c.set(Calendar.DAY_OF_MONTH, lastDay); String endDate = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()) + " 23:59:59"; return new String[]{startDate, endDate}; } //region 获取某一天的开始时间 public static String getStartOfDayStr(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DATE); calendar.setTimeInMillis(0); calendar.set(year, month, day, 0, 0, 0); return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()); } //endregion //region 获取某一天的结束时间 public static String getEndOfDayStr(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DATE); calendar.setTimeInMillis(0); calendar.set(year, month, day, 23, 59, 59); return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()); } //endregion //region 获取指定月份的最后一天 public static Date getToMonthEndDate(String DateStr) { Calendar c = Calendar.getInstance(); c.clear(); c.set(Calendar.YEAR, Integer.parseInt(DateStr.substring(0, 4))); c.set(Calendar.MONTH, Integer.parseInt(DateStr.substring(5, 7).replace("-", "")) - 1); c.roll(Calendar.DAY_OF_MONTH, -1); c.set(Calendar.HOUR_OF_DAY, 23); c.set(Calendar.MINUTE, 59); c.set(Calendar.SECOND, 59); c.set(Calendar.MILLISECOND, 999); return c.getTime(); } //endregion //region 获取指定月份的第一天 public static Date getToMonthBeginDate(String DateStr) { Calendar c = Calendar.getInstance(); c.clear(); c.set(Calendar.YEAR, Integer.parseInt(DateStr.substring(0, 4))); c.set(Calendar.MONTH, Integer.parseInt(DateStr.substring(5, 7).replace("-", "")) - 1); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 000); return c.getTime(); } //endregion //region 获取某一天的开始时间 public static Date getStartOfDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DATE); calendar.set(year, month, day, 0, 0, 0); calendar.set(Calendar.MILLISECOND, 000); return calendar.getTime(); } //endregion //region 获取某一天的结束时间 public static Date getEndOfDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DATE); calendar.setTimeInMillis(0); calendar.set(year, month, day, 23, 59, 59); return calendar.getTime(); } //endregion public static List MonthArrayTime(Date StartDate, Date EndDate) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化为年月 Calendar StartTime = Calendar.getInstance(); StartTime.setTime(StartDate); StartTime.set(StartTime.get(Calendar.YEAR), StartTime.get(Calendar.MONTH), StartTime.get(Calendar.DATE)); Calendar EndTime = Calendar.getInstance(); EndTime.setTime(EndDate); EndTime.set(EndTime.get(Calendar.YEAR), EndTime.get(Calendar.MONTH), EndTime.get(Calendar.DATE)); int total = (EndTime.get(Calendar.YEAR) - StartTime.get(Calendar.YEAR)) * 12 + (EndTime.get(Calendar.MONTH) - StartTime.get(Calendar.MONTH)); List result = new ArrayList() { }; for (int i = 0; i <= total; i++) { StartTime.add(Calendar.MONTH, i == 0 ? 0 : 1); Date MonthLastDay = getToMonthEndDate(sdf.format(StartTime.getTime())); UnitTimeHelpModel data = new UnitTimeHelpModel(); data.setStartDate(i == 0 ? StartTime.getTime() : getStartOfDay(getToMonthBeginDate(sdf.format(StartTime.getTime())))); data.setEndDate(EndTime.getTime().before(MonthLastDay) ? getEndOfDay(EndTime.getTime()) : MonthLastDay); result.add(data); } return result; } public static List getDatesBetween(LocalDate startDate, LocalDate endDate) { List datas = new ArrayList<>(); long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate); for (int i = 0; i <= numOfDaysBetween; i++) { ClassSettingDateVo data = new ClassSettingDateVo(); LocalDate date = startDate.plusDays(i); // 获取当前日期对应的星期几 DayOfWeek dow = date.getDayOfWeek(); int dayOfWeek = dow.getValue(); // 获取星期几的数字表示,1表示周一,7表示周日 data.setWeekStr(eWeekStatu.stringOf(dayOfWeek)); data.setWeek(dayOfWeek); data.setDateStr(date.toString()); datas.add(data); } return datas; } /** * 获取时间date1与date2相差的秒数 * * @param date1 起始时间 * @param date2 结束时间 * @return 返回相差的秒数 */ public static int getOffsetSeconds(Date date1, Date date2) { int seconds = (int) ((date2.getTime() - date1.getTime()) / 1000); return seconds; } /** * 获取时间date1与date2相差的分钟数 * * @param date1 起始时间 * @param date2 结束时间 * @return 返回相差的分钟数 */ public static int getOffsetMinutes(Date date1, Date date2) { return getOffsetSeconds(date1, date2) / 60; } /** * 获取时间date1与date2相差的小时数 * * @param date1 起始时间 * @param date2 结束时间 * @return 返回相差的小时数 */ public static int getOffsetHours(Date date1, Date date2) { return getOffsetMinutes(date1, date2) / 60; } /** * 获取时间date1与date2相差的天数数 * * @param date1 起始时间 * @param date2 结束时间 * @return 返回相差的天数 */ public static int getOffsetDays(Date date1, Date date2) { return getOffsetHours(date1, date2) / 24+1; } public static boolean overlapping(String start1,String end1,String start2,String end2) throws ParseException { boolean one = CompareDate(start1,end2,"HH:mm"); boolean two = CompareDate(start2,end1,"HH:mm"); return (one && two); } public static String getWeek(String sdate) throws ParseException { // 再转换为时间 Date date = StringToDate(sdate,"yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.setTime(date); // int hour=c.get(Calendar.DAY_OF_WEEK); // hour中存的就是星期几了,其范围 1~7 // 1=星期日 7=星期六,其他类推 return new SimpleDateFormat("EEEE").format(c.getTime()); } public static void main(String[] args) throws ParseException { BigDecimal ss = new BigDecimal(0.3).divide(new BigDecimal(3),2);//.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(""); } }