總結(jié)Java常用的時間相關轉(zhuǎn)化
Java常用的時間相關轉(zhuǎn)化
下面代碼的一些變量基本解釋說明
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";
1.時間轉(zhuǎn)化為指定格式的字符串
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); }
2.指定格式的字符串轉(zhuǎn)時間
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); }
3.判斷日期是否未過期
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); }
4.判斷日期是否過期
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); }
5.判斷兩個日期大小
public static final int compare( Date firstDate,Date secondDate ){ return firstDate.compareTo(secondDate); }
備注:如果第一個日期參數(shù)大于第二個日期返回 1;如果兩個日期相等返回0;如果第一個日期小于第二個日期 返回-1
6.獲取指定時間前n個月的時間
public static Date DateMinus(Date date,int month){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, -month); return calendar.getTime(); }
7.獲取指定日期之前指定天,包含傳入的那一天
public static String getDaysBefore(Date date, int days) { Date td = new Date(date.getTime() - DAYTIMESTAMP * days); return DateUtils.convertDateToString(SHORTDATEFORMATER, td); }
8.獲取指定日期之前指定天的數(shù)組,包含傳入的那一天
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; }
備注:配合第七條使用
9.獲取指定時間的0點
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(); }
10.獲取指定日期的最后一秒
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(); }
11.獲取當時時間前一個小時時間
public static Date getDayBeforeHour(){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY)-1); return calendar.getTime(); }
12.獲取兩個時間之間相差的分鐘數(shù)
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 相對小的時間;可以在入?yún)⒌臅r候就判斷好,或者可以在方法內(nèi)優(yōu)化,即調(diào)用第五條操作根據(jù)返回值進行操作就可以。
13.獲取兩個時間之間間隔多少天
public static int differentDaysByMillisecond(Date date1,Date date2){ return (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24)); }
14.獲取兩個時間之間的日期集合
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) { // 根據(jù)日歷的規(guī)則,為給定的日歷字段添加或減去指定的時間量 cal.add(Calendar.DAY_OF_MONTH, 1); // 測試此日期是否在指定日期之后 if (endDate.after(cal.getTime())) { dates.add(cal.getTime()); } else { break; } } dates.add(endDate);// 把結(jié)束時間加入集合 }catch(Exception e){ log.error("獲取時間集合異常"); } return dates; }
15.獲取當月月初第一天
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()); }
16.時間戳格式化
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; }
17.獲取今天是當前年第n周
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); }
備注:startCalendar是指從周幾作為本周的開始周期 例:以周五作為一周的開始則startCalendar傳值為Calendar.FRIDAY
總結(jié):目前常用到的時間相關的操作大概就是這些,其中一些沒有覆蓋到的可以通過上面相關操作調(diào)整就能得到,如有遺漏請在評論中補充,我及時調(diào)整增加。
到此這篇關于總結(jié)Java常用的時間相關轉(zhuǎn)化的文章就介紹到這了,更多相關Java時間相關轉(zhuǎn)化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java調(diào)用微信現(xiàn)金紅包接口的心得與體會總結(jié)
這篇文章主要介紹了java調(diào)用微信現(xiàn)金紅包接口的心得與體會總結(jié),有需要的朋友可以了解一下。2016-11-11

Java報錯:ClassCastException問題解決方法

使用Java對數(shù)據(jù)庫進行基本的查詢和更新操作

springboot項目編譯提示無效的源發(fā)行版17解決辦法