分享一些日期、时间格式处理工具方法
2016-08-23 14:11
811 查看
分享一些本人项目中用得比较多的日期、时间格式等处理工具方法,希望对初学者有用,用到的都是java 基础的 API
/** * 获取系统时间 * * @param dateFormatStr * 日期格式字符串 如: yyyy-MM-dd * @return 系统时间字符串 */ public static String getSysDate(String dateFormatStr) { String systemDate = ""; if (dateFormatStr != null) { Calendar date = Calendar.getInstance(); SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatStr, Locale.CHINA); systemDate = dateFormat.format(date.getTime()); // 系统时间 } return systemDate; } /** * 获取系统时间,用默认的时间格式 * * @return 系统时间 */ public static String getSysDate() { return getSysDate(DEFAULT_DATE_FORMAT); } /** * 计算两个日期之间相差的天数 * * @param bDate * 开始时间 * @param eDate * 结束时间 * @return 相差天数 * @throws ParseException */ public static int getDaysBetween(Date bDate, Date eDate) { try { if (bDate == null || eDate == null) { return 0; } SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT); bDate = sdf.parse(sdf.format(bDate)); eDate = sdf.parse(sdf.format(eDate)); Calendar cal = Calendar.getInstance(); cal.setTime(bDate); long time1 = cal.getTimeInMillis(); cal.setTime(eDate); long time2 = cal.getTimeInMillis(); long between_days = (time2 - time1) / (1000 * 3600 * 24); return Integer.parseInt(String.valueOf(between_days)); } catch (ParseException e) { logger.error("getDaysBetween bDate:{} eDate: {} error:{} ", bDate, eDate, e); } return 0; } /** * 计算时间间隔 * * @param bDate * @param eDate * @return * @throws ParseException * @author Tan.xx * @date 2015年7月28日 上午10:10:48 * */ public static int getDaysBetween(String bDate, String eDate) { try { if (StringUtils.isBlank(bDate) || StringUtils.isBlank(eDate)) { return 0; } SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(bDate)); long time1 = cal.getTimeInMillis(); cal.setTime(sdf.parse(eDate)); long time2 = cal.getTimeInMillis(); long between_days = (time2 - time1) / (1000 * 3600 * 24); return Integer.parseInt(String.valueOf(between_days)); } catch (ParseException e) { logger.error("getDaysBetween bDate:{} eDate: {} error:{} ", bDate, eDate, e); } return 0; } /** * 获取距离系统时间间隔 * * @param bDate * @return * @author Tan.xx * @date 2015年7月28日 上午10:32:00 * */ public static int getDaysWithSysDate(Date bDate) { return getDaysBetween(new Date(), bDate); } /** * 日期大小的比较 格式 yyyy-MM-dd * * @param inDate * 开始日期 * @param outDate * 止始日期 * @return inDate>outDate return true || inDate<outDate return false || * inDate == outDate return true * @throws */ public static boolean toCompareDate(String inDate, String outDate) { return toCompareDate(inDate, outDate, DEFAULT_DATE_FORMAT, COMPARE_TYPE_BEFORE); } /** * 日期大小的比较 格式 yyyy-MM-dd * * @param inDate * 开始日期 * @param outDate * 止始日期 * @param compareType * 日期比较类型 * @return inDate>outDate return true || inDate<outDate return false || * inDate == outDate return true */ public static boolean toCompareDate(String inDate, String outDate, int compareType) { return toCompareDate(inDate, outDate, DEFAULT_DATE_FORMAT, compareType); } /** * 日期大小的比较 格式 yyyy-MM-dd * * @param inDate * 开始日期 * @param outDate * 止始日期 * @param formatStyle * 日期格式 * @return inDate>outDate return true || inDate<outDate return false || * inDate == outDate return true */ public static boolean toCompareDate(String inDate, String outDate, String formatStyle) { return toCompareDate(inDate, outDate, formatStyle, COMPARE_TYPE_BEFORE); } /** * 日期大小的比较 格式 yyyy-MM-dd * * @param inDate * 开始日期 * @param outDate * 止始日期 * @param formatStyle * 时间格式 * @param compareType * 比较类型 * @return inDate>outDate return true || inDate<outDate return false || * inDate == outDate return true */ public static boolean toCompareDate(String inDate, String outDate, String formatStyle, int compareType) { DateFormat dateformat = new SimpleDateFormat(formatStyle, Locale.CHINA); try { long mInDate = dateformat.parse(inDate).getTime(); long mOutDate = dateformat.parse(outDate).getTime(); switch (compareType) { case COMPARE_TYPE_BEFORE: if (mInDate > mOutDate) { return true; } break; case COMPARE_TYPE_EQUAL: if (mInDate == mOutDate) { return true; } break; case COMPARE_TYPE_AFTER: if (mInDate < mOutDate) { return true; } break; default: break; } } catch (Exception e) { logger.error(" compareDate is error :" + e); } return false; } /** * 是否在系统时间之前 * * @param date * 日期 格式 yyyy-MM-dd * @param * @return boolean * @throws */ public static boolean isBeforeSysDate(String date) { return toCompareDate(getSysDate(), date, COMPARE_TYPE_BEFORE); } /** * 是否和系统时间相等 * * @param date * 日期 格式 yyyy-MM-dd * @return boolean * @throws */ public static boolean isEqualSysDate(String date) { return toCompareDate(getSysDate(), date, COMPARE_TYPE_EQUAL); } /** * 是否在系统时间之后 * * @Method: isAfterSysDate 日期 格式 yyyy-MM-dd * @param date * 待检测的日期 * @return 是否在系统时间之后 * @throws */ public static boolean isAfterSysDate(String date) { return toCompareDate(getSysDate(), date, COMPARE_TYPE_AFTER); } /** * 计算距离时间 剩余秒数 (系统时间之前返回0) * * @param bDate * @return * @author Tan.xx * @date 2015年12月1日 下午2:19:32 * */ public static long getSecondsBetween(Date bDate) { long seconds = getSecondsBetween(new Date(), bDate); if (seconds < 0) { return 0; } return seconds; } /** * 计算距离时间差 * * @param bDate * @param eDate * @return * @author Tan.xx * @date 2015年12月1日 下午2:19:32 * */ public static long getSecondsBetween(Date bDate, Date eDate) { if (bDate == null || eDate == null) { return 0; } long end = eDate.getTime(); long start = bDate.getTime(); long milliseconds = (end - start); return milliseconds / 1000; } /** * 转换日期 * * @param dstr * @param formatStyle * @return * @author Tan.xx * @date 2015年12月2日 下午5:08:11 * */ public static Date getDateByStr(String dstr, String formatStyle) { if (StringUtils.isBlank(dstr)) { return null; } SimpleDateFormat sdf = new SimpleDateFormat(formatStyle);// 小写的mm表示的是分钟 try { return sdf.parse(dstr); } catch (ParseException e) { logger.error("日期转换错误 getDateByStr" + dstr + ":" + e); return null; } } /** * 获取时间戳 * * @return * @author Tan.xx * @date 2016年3月23日 下午3:39:26 * */ public static String getTimeStamp() { return String.valueOf(System.currentTimeMillis() / 1000); }
相关文章推荐
- C#有关时间处理的一些方法,两个日期间相差的天、小时、分钟、月数等
- Quartz动态添加、修改和删除定时任务时间的方法以及日期转换cron表达式时间格式工具类
- C#有关时间处理的一些方法,两个日期间相差的天、小时、分钟、月数等
- C#有关时间处理的一些方法,两个日期间相差的天、小时、分钟、月数等
- 自己做工具--js 时间戳转为日期格式封装方法date(format, timestamp)
- C#有关时间处理的一些方法,两个日期间相差的天、小时、分钟、月数等
- javascript中处理时间戳为日期格式的方法
- 时间日期的一些处理方法
- 关于时间格式的一些处理方法
- javascript中处理时间戳为日期格式的方法
- python的一些日期处理方法(工具)
- 今天分享一些关于日期处理Calendar类的方法
- javascript中处理时间戳为日期格式的方法
- 时间日期的一些处理方法(code)
- 处理日期时间最全的一些方法
- C#中日期格式数据的一些处理方法(转)
- C#字符串转换为日期时间格式及其错误处理
- C#日期格式处理方法
- JAVA处理日期时间常用方法:
- JAVA处理日期时间常用方法