这篇文章主要讲解了“Java常用的时间相关转化有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java常用的时间相关转化有哪些”吧!
下面代码的一些变量基本解释说明
datePattern:时间对应的字符串格式
date: 时间
dateStr:字符串格式的时间
指定的几个常量:
public static final long DAYTIMESTAMP = 24 * 60 * 60 * 1000L; public static final String SHORTDATEFORMATER = "yyyy-MM-dd"; public static final String LONGDATEFORMATER = "yyyy-MM-dd HH:mm:ss";
public static final String convertDateToString(String datePattern, Date date) { String returnValue = null; if (date != null) { SimpleDateFormat df = new SimpleDateFormat(datePattern); returnValue = df.format(date); } return (returnValue); }
public static final Date convertStringToDate(String datePattern,String dateStr) { if( StringUtils.isBlank(dateStr) ){ return null; } SimpleDateFormat df = null; Date date = null; df = new SimpleDateFormat(datePattern); try { date = df.parse(dateStr); } catch (ParseException pe) { log.error("异常![{}]",pe); return null; } return (date); }
public static final boolean isNonExpired(Date date){ Calendar calendarNow = Calendar.getInstance(); calendarNow.setTime(calendarNow.getTime()); Calendar calendarGiven = Calendar.getInstance(); calendarGiven.setTime(date); return calendarNow.before(calendarGiven); }
public static final boolean isExpired(Date date){ Calendar calendarNow = Calendar.getInstance(); calendarNow.setTime(calendarNow.getTime()); Calendar calendarGiven = Calendar.getInstance(); calendarGiven.setTime(date); return calendarNow.after(calendarGiven); }
public static final int compare( Date firstDate,Date secondDate ){ return firstDate.compareTo(secondDate); }
备注:如果第一个日期参数大于第二个日期返回 1;如果两个日期相等返回0;如果第一个日期小于第二个日期 返回-1
public static Date DateMinus(Date date,int month){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, -month); return calendar.getTime(); }
public static String getDaysBefore(Date date, int days) { Date td = new Date(date.getTime() - DAYTIMESTAMP * days); return DateUtils.convertDateToString(SHORTDATEFORMATER, td); }
public static List<String> getDaysBeforeArray(Date date, int days){ List<String> resultList = new ArrayList<>(); for (int i = days-1; i >= 0; i--) { resultList.add(getDaysBefore(date, i)); } return resultList; }
备注:配合第七条使用
public static Date getDayStartTimeByDate(Date date){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar.getTime(); }
public static Date getDayEndOfDay(Date date){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); return calendar.getTime(); }
public static Date getDayBeforeHour(){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY)-1); return calendar.getTime(); }
public static String getdifferMinute(Date endDate, Date nowDate){ long nm = 1000 * 60; // 获得两个时间的毫秒时间差异 long diff = endDate.getTime() - nowDate.getTime(); return String.valueOf(diff/nm); }
备注:endDate 相对大的时间;nowDate 相对小的时间;可以在入参的时候就判断好,或者可以在方法内优化,即调用第五条操作根据返回值进行操作就可以。
public static int differentDaysByMillisecond(Date date1,Date date2){ return (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24)); }
public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate ) { List<Date> dates = new ArrayList<>(); try{ dates.add(beginDate);// 把开始时间加入集合 Calendar cal = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 cal.setTime(beginDate); while (true) { // 根据日历的规则,为给定的日历字段添加或减去指定的时间量 cal.add(Calendar.DAY_OF_MONTH, 1); // 测试此日期是否在指定日期之后 if (endDate.after(cal.getTime())) { dates.add(cal.getTime()); } else { break; } } dates.add(endDate);// 把结束时间加入集合 }catch(Exception e){ log.error("获取时间集合异常"); } return dates; }
public static String getMonthFirstDay() { SimpleDateFormat format = new SimpleDateFormat(SHORTDATEFORMATER); Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, 0); c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天 return format.format(c.getTime()); }
public static String parseDate(Long timeStamp){ String resDate = ""; if(null != timeStamp){ Date date = new Date(timeStamp); SimpleDateFormat smf = new SimpleDateFormat(LONGDATEFORMATER); resDate= smf.format(date); } return resDate; }
public static int getWeekOfYear(String dateStr,int startCalendar){ SimpleDateFormat format = new SimpleDateFormat(SHORTDATEFORMATER); Calendar calendar = Calendar.getInstance(); try { Date date = format.parse(dateStr); calendar.setFirstDayOfWeek(startCalendar); calendar.setTime(date); } catch (Exception error) { error.printStackTrace(); } return calendar.get(Calendar.WEEK_OF_YEAR); }
感谢各位的阅读,以上就是“Java常用的时间相关转化有哪些”的内容了,经过本文的学习后,相信大家对Java常用的时间相关转化有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。