| | |
| | | * 日期格式(yyyy-MM-dd HH) |
| | | * */ |
| | | public static final String yyyy_MM_dd_HH_EN = "yyyy-MM-dd HH"; |
| | | /* |
| | | * 日期格式(MM-dd) |
| | | * */ |
| | | public static final String MM_dd_EN = "MM-dd"; |
| | | |
| | | /* |
| | | * Date类toString格式 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 转化dateTimeStr为Date类型 |
| | | * 转化dateTimeStr为Date类型 去除时间 |
| | | * |
| | | * @param dateTimeStr |
| | | * @return |
| | |
| | | } |
| | | |
| | | /** |
| | | * 比较两个"yyyy-MM-dd HH:mm:ss"格式的日期,之间相差多少毫秒,time2-time1 |
| | | * 比较两个"yyyy-MM-dd"格式的日期,之间相差多少毫秒,time2-time1 |
| | | * |
| | | * @param time1 |
| | | * @param time2 |
| | |
| | | Date d2 = getDate(time2); |
| | | return d2.getTime() - d1.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 比较两个"yyyy-MM-dd HH:mm:ss"格式的日期,之间相差多少毫秒,time2-time1 |
| | | * |
| | | * @param time1 |
| | | * @param time2 |
| | | * @return |
| | | */ |
| | | public static long compareDateStrDay(String time1, String time2) { |
| | | Date d1 = getDate(time1,DateUtils.yyyy_MM_dd_HH_mm_ss_EN); |
| | | Date d2 = getDate(time2,DateUtils.yyyy_MM_dd_HH_mm_ss_EN); |
| | | return d2.getTime() - d1.getTime(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 比较任意格式时间相差毫秒数 |
| | |
| | | } |
| | | } |
| | | |
| | | public static boolean isTimeBeforE(Date nows, Date date) { |
| | | long hous = nows.getTime() - date.getTime(); |
| | | if (hous >= 0) { |
| | | return true; |
| | | } else { |
| | | return false; |
| | | } |
| | | } |
| | | /** |
| | | * 将小时数换算成返回以毫秒为单位的时间 |
| | | * |
| | |
| | | return dateToDateString(now.getTime(), formatStr); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取指定日期day天后的一个(formatStr)的字符串 |
| | | * |
| | | * @param date |
| | | * @param date, |
| | | * @param hours |
| | | * @return |
| | | */ |
| | | public static Date getDateAddHour(Date date,int hours) { |
| | | Calendar now = Calendar.getInstance(TimeZone.getDefault()); |
| | | now.setTime(date); |
| | | now.add(Calendar.HOUR_OF_DAY, hours); |
| | | return now.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * @Description: 获取指定日期day天后的日期 |
| | | * @Param: [date, day] |
| | |
| | | } |
| | | return new Date(); |
| | | } |
| | | |
| | | |
| | | |
| | | public static String getAfterNDays(Date date, int n, String formateStr) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat(formateStr); |
| | |
| | | return year == currentYear; |
| | | } |
| | | |
| | | /* |
| | | * 判断日期是否为本月 |
| | | * */ |
| | | public static boolean isCurrentMonth(Date date) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.setTime(new Date()); |
| | | int currentYear = cal.get(Calendar.MONTH); |
| | | cal.setTime(date); |
| | | int year = cal.get(Calendar.MONTH); |
| | | return year == currentYear; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取当前月的最后一天 |
| | | * |
| | |
| | | */ |
| | | public static Date getLastDayOfCurrMonth() { |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.add(Calendar.MONTH, 1); |
| | | cal.set(Calendar.DAY_OF_MONTH, 0); |
| | | return getDate(dateToDateString(cal.getTime(), yyyy_MM_dd_EN)); |
| | | } |
| | | |
| | | public static Date getLastDayOfCurrMonth(Date time) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.setTime(time); |
| | | cal.add(Calendar.MONTH, 1); |
| | | cal.set(Calendar.DAY_OF_MONTH, 0); |
| | | return getDate(dateToDateString(cal.getTime(), yyyy_MM_dd_EN)); |
| | |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | //加减年份 |
| | | public static Date addYears(Date date, int years) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.add(Calendar.YEAR, years); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * <p> |
| | | * Title: getDays |
| | |
| | | String lastYear = getDateAddYear(DateUtils.dateToDateString(getDate(), DateUtils.yyyy), -1); |
| | | return DateUtils.getDate(lastYear, DateUtils.yyyy); |
| | | } |
| | | //去年指定日期 |
| | | public static Date getFirstDayOfLastYear(Date date) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.add(Calendar.YEAR, -1); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | //获取一天中每个小时的前后一小时集合 |
| | | public static Map<Date, List<Integer>> getBeforeAndAfterHourDate(Date date) { |