import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import org.apache.commons.lang3.time.DateFormatUtils; /** * 類名稱:DateUtil * 類描述: 時間操作工具 * @version */ public class DateUtil { /** * 生成ISO-8601 規范的時間格式 * @param date * @return */ public static String formatISO8601DateString(Date date){ String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; return DateFormatUtils.format(date, pattern); } /** * 獲取反時間戳 * @return */ public static Long getCurrentReverseTime(){ long longTime = System.currentTimeMillis()*1000000 + CalculateUtil.getNext(999999); return Long.MAX_VALUE - longTime; } /** * 獲取原時間戳 * @param reverseTime * @return */ public static Long recoverReverseTime(Long reverseTime){ long longTime = Long.MAX_VALUE - reverseTime; return longTime/1000000; } /** * 生成頁面普通展示時間 * @param date * @return */ public static String formatNormalDateString(Date date){ String pattern = "yyyy-MM-dd HH:mm:ss"; return DateFormatUtils.format(date, pattern); } /** * 日期轉換為字符串 yyyy-MM-dd * * @param date * @return */ public static String getShotDate(Date date) { if (date == null){ return ""; } return dateStr(date, "yyyy-MM-dd"); } /** * 日期轉換為字符串 格式自定義 * * @param date * @param f * @return */ public static String dateStr(Date date, String f) { if (date == null){ return ""; } SimpleDateFormat format = new SimpleDateFormat(f); String str = format.format(date); return str; } /** * 獲得當前日期 * @return */ public static Date getNow() { Calendar cal = Calendar.getInstance(); Date currDate = cal.getTime(); return currDate; } /** * 獲得當前日期,精確到毫秒 * @return */ public static Timestamp getNowInMillis() { Timestamp timeStamep = new Timestamp(getNow().getTime()); return timeStamep; } public static Date getDateByStrTime(String dateStr, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); try { return sdf.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } return null; } /** * 計算兩個日期之間相差的天數 * @param date1 * @param date2 * @return date1>date2時結果<0, date1=date2時結果=0, date2>date1時結果>0 */ public static int daysBetween(Date date1, Date date2, String format){ DateFormat sdf=new SimpleDateFormat(format); Calendar cal = Calendar.getInstance(); try { Date d1 = sdf.parse(DateUtil.dateStr(date1, format)); Date d2 = sdf.parse(DateUtil.dateStr(date2, format)); cal.setTime(d1); long time1 = cal.getTimeInMillis(); cal.setTime(d2); long time2 = cal.getTimeInMillis(); return Integer.parseInt(String.valueOf((time2 - time1) / 86400000L)); } catch (ParseException e) { e.printStackTrace(); } return 0; } /** * 計算兩個日期之間相差的小時數 * @param date1 * @param date2 * @return */ public static int hoursBetween(Date date1, Date date2) { DateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); try { Date d1 = sdf.parse(DateUtil.dateStr(date1, "yyyyMMdd")); Date d2 = sdf.parse(DateUtil.dateStr(date2, "yyyyMMdd")); cal.setTime(d1); cal.setTime(d1); long time1 = cal.getTimeInMillis(); cal.setTime(d2); long time2 = cal.getTimeInMillis(); return Integer.parseInt(String.valueOf((time2 - time1) / 3600000L)); } catch (ParseException e) { e.printStackTrace(); } return 0; } /** * 計算兩個日期相差的時間 */ public static void getBetweenDate () { String dateStart = "2017111520"; String dateStop = "2017111620"; SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHH"); Date d1 = null; Date d2 = null; try { d1 = format.parse(dateStart); d2 = format.parse(dateStop); //毫秒ms long diff = d2.getTime() - d1.getTime(); long diffSeconds = diff / 1000 % 60; long diffMinutes = diff / (60 * 1000) % 60; long diffHours = diff / (60 * 60 * 1000) % 24; long diffDays = diff / (24 * 60 * 60 * 1000); System.out.print("兩個時間相差:"); System.out.print(diffDays + " 天, "); System.out.print(diffHours + " 小時, "); System.out.print(diffMinutes + " 分鍾, "); System.out.print(diffSeconds + " 秒."); } catch (Exception e) { e.printStackTrace(); } } /** * 計算兩個日期之間相差的小時數 * @param date1 * @param date2 * @return date1>date2時結果<0, date1=date2時結果=0, date2>date1時結果>0 */ public static int hoursBetweenByDateStr(Date date1, Date date2) { DateFormat sdf=new SimpleDateFormat("yyyyMMddHH"); Calendar cal = Calendar.getInstance(); try { Date d1 = sdf.parse(DateUtil.dateStr(date1,"yyyyMMddHH")); Date d2 = sdf.parse(DateUtil.dateStr(date2,"yyyyMMddHH")); cal.setTime(d1); long time1 = cal.getTimeInMillis(); cal.setTime(d2); long time2 = cal.getTimeInMillis(); return Integer.parseInt(String.valueOf(((time2 - time1) / 3600000L))); } catch (ParseException e) { e.printStackTrace(); } return 0; } /** * 計算兩個日期之間相差的分鍾數 * @param date1 * @param date2 * @return date1>date2時結果<0, date1=date2時結果=0, date2>date1時結果>0 */ public static int minuteBetweenByDateStr(Date date1, Date date2) { DateFormat sdf=new SimpleDateFormat("yyyyMMddHHmm"); Calendar cal = Calendar.getInstance(); try { Date d1 = sdf.parse(DateUtil.dateStr(date1,"yyyyMMddHHmm")); Date d2 = sdf.parse(DateUtil.dateStr(date2,"yyyyMMddHHmm")); cal.setTime(d1); long time1 = cal.getTimeInMillis(); cal.setTime(d2); long time2 = cal.getTimeInMillis(); return Integer.parseInt(String.valueOf(((time2 - time1) / 60000L))); } catch (ParseException e) { e.printStackTrace(); } return 0; } /** * 計算兩個日期之間相差的秒數 * @param date1 * @param date2 * @return date1>date2時結果<0, date1=date2時結果=0, date2>date1時結果>0 */ public static int secondBetweenByDateStr(Date date1, Date date2) { DateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss"); Calendar cal = Calendar.getInstance(); try { Date d1 = sdf.parse(DateUtil.dateStr(date1, "yyyyMMddHHmmss")); Date d2 = sdf.parse(DateUtil.dateStr(date2, "yyyyMMddHHmmss")); cal.setTime(d1); long time1 = cal.getTimeInMillis(); cal.setTime(d2); long time2 = cal.getTimeInMillis(); return Integer.parseInt(String.valueOf(((time2 - time1) / 1000L))); } catch (ParseException e) { e.printStackTrace(); } return 0; } public static void main(String[] args) { System.out.println(hoursBetweenByDateStr(getDateByStrTime("2018010223", "yyyyMMddHH"), getDateByStrTime("2018010323", "yyyyMMddHH"))); System.out.println(minuteBetweenByDateStr(getDateByStrTime("201801052359", "yyyyMMddHHmm"), getDateByStrTime("201801060000", "yyyyMMddHHmm"))); //2.活動結束時間>0 ( 默認2018年1月5號 20:30結束) if( DateUtil.minuteBetweenByDateStr (DateUtil.getDateByStrTime("201801111348","yyyyMMddHHmm"), DateUtil.getNow()) > 0){ System.out.println(DateUtil.minuteBetweenByDateStr (DateUtil.getDateByStrTime("201801111348","yyyyMMddHHmm"), DateUtil.getNow())); }else{ System.out.println("時間未結束"); } System.out.println(DateUtil.secondBetweenByDateStr(DateUtil.getDateByStrTime("20180110150200","yyyyMMddHHmmss"), DateUtil.getNow())); } }
