| | |
| | | private static SimpleDateFormat Format_16 = new SimpleDateFormat("yyyy/MM/dd HH:mm"); |
| | | private static SimpleDateFormat Format_17 = new SimpleDateFormat("HH:mm"); |
| | | |
| | | /**时间格式转化iso8601 |
| | | * @param date 时间 |
| | | * @return 返回的时间格式字符串 |
| | | */ |
| | | public static String dateFormatISO8601(Date date) { |
| | | if(!SimpleTool.checkNotNull(date)){ |
| | | return ""; |
| | | } |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");//设置日期格式 |
| | | return df.format(date); |
| | | } |
| | | |
| | | /**时间格式转化 |
| | | * @param date 时间 |
| | | * @param format 时间格式 |
| | |
| | | } |
| | | SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式 |
| | | return df.format(date); |
| | | } |
| | | |
| | | /**时间戳转时间 |
| | | * @param timestamp 时间戳 |
| | | * @param format 时间格式 |
| | | * @return 返回的时间格式字符串 |
| | | */ |
| | | public static Date timestampToDate(long timestamp, String format) { |
| | | SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String sd = sdf.format(new Date(timestamp)); // 时间戳转换成时间 |
| | | return DateUtil.parseString(sd,"yyyy-MM-dd HH:mm:ss"); |
| | | } |
| | | |
| | | /** |
| | |
| | | return Format_14.format(date); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 当前时间之前的时间与当前时间相差多少秒 |
| | | * @param startDate 当前时间之前的时间 |
| | | * @return |
| | | */ |
| | | public static int calLastedTime(Date startDate) { |
| | | long nowDate = new Date().getTime(); |
| | | long startDateTime = startDate.getTime(); |
| | | int diffSeconds = (int) ((nowDate - startDateTime) / 1000); |
| | | return diffSeconds; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 转换成yyyyMMddHHmmssSSS格式的日期字符串 |
| | | * |
| | |
| | | } |
| | | } |
| | | |
| | | /**时间上秒叠加 |
| | | * @Author: ChenJiaHe |
| | | * @param dateTime 时间 |
| | | * @param second 秒 |
| | | * @return |
| | | */ |
| | | public static Date addSecond(Date dateTime,int second){ |
| | | Calendar c = Calendar.getInstance(); |
| | | c.setTime(dateTime); |
| | | c.add(Calendar.SECOND, second); |
| | | return c.getTime(); |
| | | } |
| | | |
| | | /**时间上分钟叠加 |
| | | * @Author: ChenJiaHe |
| | |
| | | Calendar c = Calendar.getInstance(); |
| | | c.setTime(dateTime); |
| | | c.add(Calendar.DATE, dayNum); |
| | | return c.getTime(); |
| | | } |
| | | |
| | | /**时间上月数叠加 |
| | | * @Author: ChenJiaHe |
| | | * @param dateTime 时间 |
| | | * @param dayNum 天数 |
| | | * @return |
| | | */ |
| | | public static Date addMonth(Date dateTime,int dayNum){ |
| | | Calendar c = Calendar.getInstance(); |
| | | c.setTime(dateTime); |
| | | c.add(Calendar.MONTH, dayNum); |
| | | return c.getTime(); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取两个时间相差分钟数 |
| | | * @param startTime 开始时间 |
| | | * @param endTime 结束时间 |
| | | * @return 分钟 |
| | | */ |
| | | public static long differMinute(Date startTime, Date endTime) { |
| | | long NTime = startTime.getTime(); |
| | | long OTime = endTime.getTime(); |
| | | return (NTime - OTime) / 1000 / 60; |
| | | } |
| | | |
| | | /** |
| | | * endTime比startTime多的天数 |
| | | * @param startTime 最小时间 |
| | | * @param endTime 最大时间 |
| | |
| | | } |
| | | boolean status = false; |
| | | if(formatDate(startTime,"yyyyMMdd").equals(formatDate(endTime,"yyyyMMdd"))){ |
| | | status = false; |
| | | status = true; |
| | | } |
| | | return status; |
| | | } |
| | |
| | | } |
| | | |
| | | return 0; |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 获取当前月的开始时间 |
| | | * @param time 时间 |
| | | * @return 返回时间 格式yyyy-MM-dd 00:00:00 |
| | | */ |
| | | public static Date getMonthStart(Date time) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(time); |
| | | calendar.set(Calendar.DAY_OF_MONTH, 1); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| | | calendar.set(Calendar.MINUTE, 0); |
| | | calendar.set(Calendar.SECOND, 0); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前月的开始时间 |
| | | * |
| | | * @param num 0拿取当月,正代表后,负代表前,值为几个(月) |
| | | * @return 返回时间 格式yyyy-MM-dd 00:00:00 |
| | | */ |
| | | public static String getMonthStart(Integer num) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.add(Calendar.MONTH, num); |
| | | calendar.set(Calendar.DAY_OF_MONTH, 1); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| | | calendar.set(Calendar.MINUTE, 0); |
| | | calendar.set(Calendar.SECOND, 0); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); |
| | | return sdf.format(calendar.getTime()); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前月的结束时间 |
| | | * @param date 点前时间 |
| | | * @return 返回时间 格式yyyy-MM-dd 23:59:59 |
| | | */ |
| | | public static Date getMonthEnd(Date date) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 23); |
| | | calendar.set(Calendar.MINUTE, 59); |
| | | calendar.set(Calendar.SECOND, 59); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前月的结束时间 |
| | | * @param num 0拿取当月,正代表后,负代表前,值为几个(月) |
| | | * @return 返回时间 格式yyyy-MM-dd 23:59:59 |
| | | */ |
| | | public static String getMonthEnd(Integer num) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.add(Calendar.MONTH, num); |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 23); |
| | | calendar.set(Calendar.MINUTE, 59); |
| | | calendar.set(Calendar.SECOND, 59); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); |
| | | return sdf.format(calendar.getTime()); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年的开始时间 |
| | | * @param time 时间 |
| | | * @return 返回时间 格式yyyy-MM-dd 00:00:00 |
| | | */ |
| | | public static Date getYearStart(Date time) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(time); |
| | | calendar.set(Calendar.MONTH, 1); |
| | | calendar.set(Calendar.DAY_OF_MONTH, 1); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| | | calendar.set(Calendar.MINUTE, 0); |
| | | calendar.set(Calendar.SECOND, 0); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年的结束时间 |
| | | * @param time 时间 |
| | | * @return 返回时间 格式yyyy-MM-dd 00:00:00 |
| | | */ |
| | | public static Date getYearEnd(Date time) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(time); |
| | | calendar.set(Calendar.MONTH, 12); |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE)); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 23); |
| | | calendar.set(Calendar.MINUTE, 59); |
| | | calendar.set(Calendar.SECOND, 59); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | |
| | | /**这天的开始时间 |
| | | * 日期2000-01-01变2000-01-01 00:00:00 |
| | | */ |
| | | public static String dayToStart(Date date) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.set(Calendar.HOUR, 0); |
| | | calendar.set(Calendar.MINUTE, 0); |
| | | calendar.set(Calendar.SECOND, 0); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); |
| | | return sdf.format(calendar.getTime()); |
| | | } |
| | | |
| | | /**这天的最后时间 |
| | | * 日期2000-01-01变2000-01-01 23:59:59 |
| | | */ |
| | | public static String dayToEnd(Date date) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.set(Calendar.HOUR, 23); |
| | | calendar.set(Calendar.MINUTE, 59); |
| | | calendar.set(Calendar.SECOND, 59); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); |
| | | return sdf.format(calendar.getTime()); |
| | | } |
| | | |
| | | /**这天的最后时间 |
| | | * 日期2000-01-01变2000-01-01 23:59:59 |
| | | */ |
| | | public static Date dayToEndDate(Date date) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.set(Calendar.HOUR, 23); |
| | | calendar.set(Calendar.MINUTE, 59); |
| | | calendar.set(Calendar.SECOND, 59); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /**获取月份的天数 |
| | | * @param date 时间 |
| | | * @return 月份的天数 |
| | | */ |
| | | public static int getMonthDays(Date date) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.setTime(date); |
| | | cal.set(Calendar.DATE, 1); |
| | | cal.roll(Calendar.DATE, -1); |
| | | return cal.getActualMaximum(Calendar.DATE); |
| | | } |
| | | /**获取月份的天数 |
| | | * @param year 年份 |
| | | * @param month 月份 |
| | | * @return 月份的天数 |
| | | */ |
| | | public static int getMonthDays(int year, int month) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.set(Calendar.YEAR, year); |
| | | cal.set(Calendar.MONTH, (month - 1)); |
| | | cal.set(Calendar.DATE, 1); |
| | | cal.roll(Calendar.DATE, -1); |
| | | return cal.getActualMaximum(Calendar.DATE); |
| | | } |
| | | |
| | | /**获取月份的天数 |
| | | * @param yearMonth 年月 |
| | | * @param format 时间格式 |
| | | * @return |
| | | */ |
| | | public static int getMonthDays(String yearMonth,String format) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat(format); |
| | | Calendar calendar = Calendar.getInstance(); |
| | | try { |
| | | calendar.setTime(sdf.parse(yearMonth)); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | calendar.getActualMaximum(Calendar.DAY_OF_MONTH); |
| | | return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); |
| | | } |
| | | |
| | | /**当天多少点前 |
| | | * @param date 时间 |
| | | * @param num 多少点前,不含当点前,24小时制 |
| | | * @return 月份的天数 |
| | | */ |
| | | public static boolean getFrontMinute(Date date,int num) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.setTime(date); |
| | | cal.set(Calendar.HOUR, num-1); |
| | | cal.set(Calendar.MINUTE, 59); |
| | | cal.set(Calendar.SECOND, 59); |
| | | |
| | | return date.compareTo(cal.getTime()) < 1; |
| | | } |
| | | } |