java中工具類->時間
阿新 • • 發佈:2018-11-10
public class DateUtil { /** * 獲得拼接的時間 * @auther zy * @param time * @return */ public static Long getLongTimes(String time) { if(ObjectUtils.isEmpty(time)){ return null; } String a=getDay(); Long now=0L; try { DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); now = fmt.parse(a+" "+time).getTime(); } catch (ParseException e) { e.printStackTrace(); return null; } return now; } /** * 獲取YYYY格式 * @return */ public static String getSdfTimes() { SimpleDateFormat sdfTimes = new SimpleDateFormat("yyyyMMddHHmmss"); long currentTimeMillis = System.currentTimeMillis(); Date date = new Date(currentTimeMillis); return sdfTimes.format(date); } public static Date getNowDate() { long currentTimeMillis = System.currentTimeMillis(); return new Date(currentTimeMillis); } /** * 由long 轉為yyyy-MM-dd HH:mm:ss * @return */ public static String getIntegerToSdf(Long time) { SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdfTime.format(new Date(time*1000)); } /** * 由long yyyy/MM/dd * @return */ public static String getIntegerToSTime(Long time) { SimpleDateFormat sTimes = new SimpleDateFormat("yyyy/MM/dd"); return sTimes.format(new Date(time*1000)); } public static String getIntegerToTime(Long time) { SimpleDateFormat sTimes = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); return sTimes.format(new Date(time*1000)); } /** * 由long 轉為yyyy-MM-dd * @return */ public static String getIntegerToNyr(Long time) { SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd"); return sdfDay.format(new Date(time*1000)); } /** * 由long 轉為yyyy.MM.dd * @return */ public static String getIntegerNyr(Long time) { SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy.MM.dd"); return sdfDay.format(new Date(time*1000)); } /** * 由10位秒數轉年月 * @param time * @return */ public static String getIntegerYearMonth(Integer time){ SimpleDateFormat sdfDay = new SimpleDateFormat("yyyyMM"); return sdfDay.format(new Date(time*1000L)); } /** * 由10位秒數轉年月日 * @param time * @return */ public static String getIntegerYearMonthDay(Integer time){ SimpleDateFormat sdfDay = new SimpleDateFormat("yyyyMMdd"); return sdfDay.format(new Date(time*1000L)); } /** * 由10位秒數轉年月日 * @param time * @return */ public static String getIntegerYearMonthDay(){ SimpleDateFormat sdfDay = new SimpleDateFormat("yyyyMMdd"); return sdfDay.format(new Date(System.currentTimeMillis())); } /** * 由long 當前時間 * @return */ public static long getNowLong() { return System.currentTimeMillis()/1000; } public static int getNowInt() { return (int) (System.currentTimeMillis()/1000); } /** * 獲取YYYY格式 * @return */ public static String getYear() { SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy"); return sdfYear.format(getNowDate()); } /** * 獲取YYYY-MM-DD格式 * @return */ public static String getDay() { SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd"); return sdfDay.format(getNowDate()); } /** * 獲取YYYYMMDD格式 * @return */ public static String getDays(){ SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd"); return sdfDays.format(getNowDate()); } /** * 獲取YYYY-MM-DD HH:mm:ss格式 * @return */ public static String getTime() { SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdfTime.format(getNowDate()); } /** * @Title: compareDate * @param s * @param e * @return boolean * @throws * @author fh */ public static boolean compareDate(String s, String e) { if(fomatDate(s)==null||fomatDate(e)==null){ return false; } return fomatDate(s).getTime() >fomatDate(e).getTime(); } /** * 格式化日期 * @return */ public static Date fomatDate(String date) { DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); try { return fmt.parse(date); } catch (ParseException e) { e.printStackTrace(); return null; } } /** * 格式日期時分秒 * @return */ /** * 校驗日期是否合法 * @return */ public static boolean isValidDate(String s) { DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); try { fmt.parse(s); return true; } catch (Exception e) { // 如果throw java.text.ParseException或者NullPointerException,就說明格式不對 return false; } } /** * @param startTime * @param endTime * @return */ public static int getDiffYear(String startTime,String endTime) { DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); try { //long aa=0; int years=(int) (((fmt.parse(endTime).getTime()-fmt.parse(startTime).getTime())/ (1000 * 60 * 60 * 24))/365); return years; } catch (Exception e) { // 如果throw java.text.ParseException或者NullPointerException,就說明格式不對 return 0; } } /** * <li>功能描述:時間相減得到天數 * @param beginDateStr * @param endDateStr * @return * long * @author Administrator */ public static long getDaySub(String beginDateStr,String endDateStr){ long day=0; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date beginDate = null; Date endDate = null; try { beginDate = format.parse(beginDateStr); endDate= format.parse(endDateStr); } catch (ParseException e) { e.printStackTrace(); } day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000); //System.out.println("相隔的天數="+day); return day; } public static Long getDaySub(Date startDate, Date endDate){ long result = 0; result = (endDate.getTime() - startDate.getTime()) / (24*60*60*1000L); return result; } /** * 得到n天之後的日期1 * @param days * @return */ public static String getAfterDayDate1(String days) { int daysInt = Integer.parseInt(days); Calendar canlendar = Calendar.getInstance(); // java.util包 canlendar.add(Calendar.DATE, daysInt); // 日期減 如果不夠減會將月變動 Date date = canlendar.getTime(); SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd"); String dateStr = sdfd.format(date); return dateStr; } /** * 得到n天之後的日期 * @param days * @return */ public static String getAfterDayDate(Integer days) { Calendar canlendar = Calendar.getInstance(); // java.util包 canlendar.add(Calendar.DATE, days); // 日期減 如果不夠減會將月變動 Date date = canlendar.getTime(); SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd"); String dateStr = sdfd.format(date); return dateStr; } public static Integer getAfterDayDate(Integer date, Integer days) { SimpleDateFormat sdfd = new SimpleDateFormat("yyyyMMdd"); Date parse; try { parse = sdfd.parse(date + ""); } catch (ParseException e) { parse = new Date(); } Calendar canlendar = Calendar.getInstance(); // java.util包 canlendar.setTime(parse); canlendar.add(Calendar.DATE, days); // 日期減 如果不夠減會將月變動 Date _date = canlendar.getTime(); Integer dateStr = Integer.valueOf(sdfd.format(_date)); return dateStr; } /** * 得到n天之後是周幾 * @param days * @return */ public static String getAfterDayWeek(String days) { int daysInt = Integer.parseInt(days); Calendar canlendar = Calendar.getInstance(); // java.util包 canlendar.add(Calendar.DATE, daysInt); // 日期減 如果不夠減會將月變動 Date date = canlendar.getTime(); SimpleDateFormat sdf = new SimpleDateFormat("E"); String dateStr = sdf.format(date); return dateStr; } /** * * @param start 減數 * @param end 被減數 * @param days 當前與某個時間相差天數 * @return */ public static boolean checkIsOutTime(long start ,long now,Integer days) { String eday=getDay(); boolean flag=false; if(now!=0) { eday=getIntegerToNyr(now); } if(start !=0) { String sday=getIntegerToNyr(start); if(getDaySub(sday,eday)>days) { flag=true; } } return flag; } /** * * @param start 減數 * @param end 被減數 資料庫是秒 * @param days 相減得到的天數 * @return */ public static Integer getSubDays(long start ,long now) { if(now ==0) { now=getNowLong();//1504337048-1494235952 } double day=(now-start)/86400.00; Integer days=0; days=(int)Math.ceil(day); return days; } /** * 獲取當日0點的時間戳 * @Author wang chunru * @Description * @param * @return long * @throws */ public static long getZeroTime(){ long current = System.currentTimeMillis(); long zero = current/(1000*3600*24)*(1000*3600*24) - TimeZone.getDefault().getRawOffset(); return zero/1000; } /** * 獲取今天00:00:00時間 * @return */ public static Integer getStartTimeOfDay(){ return getStartTimeOfDay(new Date()); } /** * 獲取今天23:59:59時間 * @return */ public static Integer getEndTimeOfDay(){ return getEndTimeOfDay(new Date()); } /** * 獲取傳入日期的00:00:00時間 * @param date * @return */ public static Integer getStartTimeOfDay(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); Date start = calendar.getTime(); return (int) (start.getTime()/1000); } public static Date getStartTimeOfDay2(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); Date start = calendar.getTime(); return start; } /** * 獲取當前時間當天開始時間 * @param date * @return */ public static Date getStartDateOfDay(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); Date start = calendar.getTime(); return start; } /** * 獲取傳入日期的23:59:59時間 * @param date * @return */ public static Integer getEndTimeOfDay(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); calendar.add(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.SECOND, -1); Date end = calendar.getTime(); return (int) (end.getTime()/1000); } /** * 獲取傳入日期的結束日期 * @param date * @return */ public static Date getEndDateOfDay(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); calendar.add(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.SECOND, -1); Date end = calendar.getTime(); return end; } /** * 獲取月起始日期 * @param time * @return */ public static Date getStartDateOfMonth(String time) { SimpleDateFormat sf=new SimpleDateFormat("yyyyMM"); Date theDate; try { theDate = sf.parse(time); } catch (Exception e) { theDate = new Date(); } GregorianCalendar gcLast=(GregorianCalendar)Calendar.getInstance(); gcLast.setTime(theDate); gcLast.set(Calendar.DAY_OF_MONTH, 1); return gcLast.getTime(); } public static Date getStartDateOfMonth(Date date) { Date theDate = date; GregorianCalendar gcLast=(GregorianCalendar)Calendar.getInstance(); gcLast.setTime(theDate); gcLast.set(Calendar.DAY_OF_MONTH, 1); return gcLast.getTime(); } /** * 獲取月結束日期 * @param time * @return */ public static Date getEndDateOfMonth(String time){ SimpleDateFormat sf=new SimpleDateFormat("yyyyMM"); Date theDate; try { theDate = sf.parse(time); } catch (Exception e) { theDate = new Date(); } Calendar gcLast = Calendar.getInstance(); gcLast.setTime(theDate); gcLast.set(Calendar.DATE, gcLast.getActualMaximum(Calendar.DATE)); return gcLast.getTime(); } /** * 獲取月結束日期 * @param time * @return */ public static Date getEndDateOfMonth(Date time){ Date theDate = time; Calendar gcLast = Calendar.getInstance(); gcLast.setTime(theDate); gcLast.set(Calendar.DATE, gcLast.getActualMaximum(Calendar.DATE)); return gcLast.getTime(); } /** * 獲取月結束日期 * @param time * @return */ public static Date getEndDateOfMonth(){ Date theDate = new Date(); Calendar gcLast = Calendar.getInstance(); gcLast.setTime(theDate); gcLast.set(Calendar.DATE, gcLast.getActualMaximum(Calendar.DATE)); return gcLast.getTime(); } /** * 獲取前一天日期 * @param date * @return */ public static Date getAfterDate(Date date){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.add(Calendar.DAY_OF_MONTH, 1); Date end = calendar.getTime(); return end; } /** * 獲取後一天日期 * @param date * @return */ public static Date getBeforeDate(Date date){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, -1); Date start = calendar.getTime(); return start; } /** * 獲取後一天日期 * @return */ public static Date getAfterDate(){ return getAfterDate(new Date()); } /** * 獲取前一天日期 * @return */ public static Date getBeforeDate(){ return getBeforeDate(new Date()); } public static String getYearMonth(Date date){ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); String format = sdf.format(date); return format; } /** * 獲取幾個月後或幾個月前yyyyMM格式日期 * @param time * @return */ public static String getLaterMonth(Integer time){ Calendar calendar = Calendar.getInstance();//日曆物件 calendar.setTime(new Date());//設定當前日期 calendar.add(Calendar.MONTH, time);//月份減一 Date date = calendar.getTime(); return getYearMonth(date); } public static Date getLaterMonth2(Integer time){ Calendar calendar = Calendar.getInstance();//日曆物件 calendar.setTime(new Date());//設定當前日期 calendar.add(Calendar.MONTH, time);//月份減一 Date date = calendar.getTime(); return date; } /** * 獲取兩個日期之間的年月yyyyMM * @throws ParseException */ public static List<String> getBetweenYearMonth(String startTime,String endTime){ ArrayList<String> result = new ArrayList<String>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");//格式化為年月 Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); try { min.setTime(sdf.parse(startTime.toString())); max.setTime(sdf.parse(endTime.toString())); } catch (Exception e) { e.printStackTrace(); return result; } min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1); max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2); Calendar curr = min; while (curr.before(max)) { result.add(sdf.format(curr.getTime())); curr.add(Calendar.MONTH, 1); } return result; } /** * 獲取兩個日期之間的年月日yyyyMMdd * @throws ParseException */ public static List<String> getBetweenYearMonthDay(Integer startTime,Integer endTime){ List<String> list = new ArrayList<>(); Calendar start = Calendar.getInstance(); start.set(Integer.valueOf(startTime.toString().substring(0, 4)), Integer.valueOf(startTime.toString().substring(4, 6))-1, Integer.valueOf(startTime.toString().substring(6, 8))); Long startTIme = start.getTimeInMillis(); Calendar end = Calendar.getInstance(); end.set(Integer.valueOf(endTime.toString().substring(0, 4)), Integer.valueOf(endTime.toString().substring(4, 6))-1, Integer.valueOf(endTime.toString().substring(6, 8))); Long endTime1 = end.getTimeInMillis(); Long oneDay = 1000 * 60 * 60 * 24l; Long time = startTIme; while (time <= endTime1) { Date d = new Date(time); DateFormat df = new SimpleDateFormat("yyyyMMdd"); list.add(df.format(d)); time += oneDay; } return list; } /** * 獲取間隔天數 * @param startTime * @param endTime * @return */ public static Integer getDaysBetweenYearMonthDay(Integer startTime,Integer endTime){ List<String> list = new ArrayList<>(); Calendar start = Calendar.getInstance(); start.set(Integer.valueOf(startTime.toString().substring(0, 4)), Integer.valueOf(startTime.toString().substring(4, 6))-1, Integer.valueOf(startTime.toString().substring(6, 8))); Long startTIme = start.getTimeInMillis(); Calendar end = Calendar.getInstance(); end.set(Integer.valueOf(endTime.toString().substring(0, 4)), Integer.valueOf(endTime.toString().substring(4, 6))-1, Integer.valueOf(endTime.toString().substring(6, 8))); Long endTime1 = end.getTimeInMillis(); Long oneDay = 1000 * 60 * 60 * 24l; Long time = startTIme; while (time <= endTime1) { Date d = new Date(time); DateFormat df = new SimpleDateFormat("yyyyMMdd"); list.add(df.format(d)); time += oneDay; } return list.size() - 1; } /** * 獲取間隔天數 * @param startTime * @param endTime * @return */ public static Long getDaysBetweenYearMonthDay(Date startTime,Date endTime){ Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); min.setTime(startTime); max.setTime(endTime); long _min = min.getTimeInMillis(); long _max = max.getTimeInMillis(); long result = (_max -_min) / (1000*3600*24); return result + 1L; } /** * 根據yyyyMMdd獲取日期 * @param time * @return */ public static Date getDateFormInt(Integer time){ SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); Date parse = new Date(); try { parse = format.parse(time.toString()); } catch (ParseException e) { e.printStackTrace(); } return parse; } /** * 根據yyyyMMdd hhmmss數獲取詳細時間 * @param time * @return */ public static Date getDateFormatInt(String time){ SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd hhmmss"); Date parse = new Date(); try { parse = format.parse(time.toString()); } catch (ParseException e) { e.printStackTrace(); throw new CoreServiceException(CoreServiceExcepType.FORMAT_ERROR); } return parse; } /** * 根據yyyyMMdd hhmmss數獲取詳細時間 * @param time * @return */ public static Date getDateFormatStr(String time){ SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); Date parse = new Date(); try { parse = format.parse(time.toString()); } catch (ParseException e) { e.printStackTrace(); throw new CoreServiceException(CoreServiceExcepType.FORMAT_ERROR); } return parse; } /** * 根據yyyy-MM-dd hh:mm:ss數獲取詳細時間 * @param time * @return */ public static Date getDateFormat(String time){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date parse = new Date(); try { parse = format.parse(time.toString()); } catch (ParseException e) { e.printStackTrace(); throw new CoreServiceException(CoreServiceExcepType.FORMAT_ERROR); } return parse; } /** * * @param seconds * @return */ public static String getVoteDetailDateFormat(Date date) { SimpleDateFormat format = new SimpleDateFormat("YYYY' 年 'MM' 月 'dd' 日 '"); try { String format2 = format.format(date); return format2; } catch (Exception e) { e.printStackTrace(); throw new CoreServiceException(CoreServiceExcepType.FORMAT_ERROR); } } public static Integer getYYYYMMDDByCurrSeconds(Integer seconds){ Long currMills = seconds * 1000L; Date date = new Date(currMills); SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); String sResult = format.format(date); return Integer.parseInt(sResult); } public static String getYYYYMMByCurrDate(Date date) { SimpleDateFormat smf = new SimpleDateFormat("yyyyMM"); String format = smf.format(date); return format; } /** * 格式化 yyyyMMdd * @param date * @return */ public static Integer getYYYYMMDDByDate(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); String sResult = format.format(date); return Integer.parseInt(sResult); } /** * 格式化 yyyy/MM/dd HH:mm:ss * @param date * @return */ public static String getCommonFormatByDate(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String sResult = format.format(date); return sResult; } /** * 格式化 yyyy.MM.dd HH:mm:ss * @param date * @return */ public static String getCommonFormatByDate3(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); String sResult = format.format(date); return sResult; } /** * 格式化 yyyy-MM-dd HH:mm:ss * @param date * @return */ public static String getCommonFormatByDate1(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String sResult = format.format(date); return sResult; } public static String getAliTimestamp(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); format.setTimeZone(new SimpleTimeZone(0, "GMT")); String sResult = format.format(date); return sResult; } /** * 格式化 yyyy-MM-dd * @param date * @return */ public static String getCommonFormatByDate2(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String sResult = format.format(date); return sResult; } public static String getMongoCreateTime(){ SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); String time = format.format(new Date()); return time; } public static String getMongoCreateTime(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); String time = format.format(date); return time; } public static String formatMongoCreateTime(String time){ SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); try { Date parse = format.parse(time); String commonFormatByDate = getCommonFormatByDate(parse); return commonFormatByDate; } catch (ParseException e) { e.printStackTrace(); return ""; } } public static String formatMongoCreateTime2(String time){ SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); try { Date parse = format.parse(time); String commonFormatByDate = getCommonFormatByDate3(parse); return commonFormatByDate; } catch (ParseException e) { e.printStackTrace(); return ""; } } public static Date formatMongoCreateTimeToDate(String time){ SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); try { Date parse = format.parse(time); return parse; } catch (ParseException e) { e.printStackTrace(); return null; } } public static String commonDateToMongoDate(String date){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date parse = format.parse(date); String commonFormatByDate = getMongoCreateTime(parse);; return commonFormatByDate; } catch (ParseException e) { e.printStackTrace(); return ""; } } public static Integer getWeekOfDate(String date) { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); Date dt; try { dt = format.parse(date); } catch (ParseException e) { dt = new Date(); } Integer[] weekDays = {0, 1, 2, 3, 4, 5, 6}; Calendar cal = Calendar.getInstance(); cal.setTime(dt); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; return weekDays[w]; } /** * HH:mm * @param date * @return */ public static String getHMS(long timetemp) { Calendar calendar=Calendar.getInstance(); calendar.setTimeInMillis(timetemp); SimpleDateFormat fmat=new SimpleDateFormat("HH:mm"); String time=fmat.format(calendar.getTime()); return time; } }