此類是我們項目的 日期/時間處理工具,在此做個記錄!
/* * Copyright 2014-2018 xfami.com. All rights reserved. * Support: https://www.xfami.com */ package com.cymc.page.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import org.apache.commons.lang3.time.DateFormatUtils; /** * Utils - 日期/時間轉換 * * @author XFaMi Team * @version 1.0 */ public final class DateTimeUtil { /** * G Era 標志符 Text 公元 y 年 Year 1996; 96 M 年中的月份 Month July; Jul; 07 w 年中的周數 Number 27 W 月份中的周數 Number 2 D 年中的天數 Number 189 d 月份中的天數 Number 10 F 月份中的星期 Number 2 E 星期中的天數 Text Tuesday; Tue a am/pm 標記 Text PM H 一天中的小時數(0-23) Number 0 k 一天中的小時數(1-24) Number 24 K am/pm 中的小時數(0-11) Number 0 h am/pm 中的小時數(1-12) Number 12 m 小時中的分鍾數 Number 30 s 分鍾中的秒數 Number 55 S 毫秒數 Number 978 z 時區 General time zone Pacific Standard Time; PST; GMT-08:00 Z 時區 RFC 822 time zone -0800 */ /** * 中國時區 */ public final static String TIME_ZONE_CN = "GMT+8"; /** * 把時間格式化成如:2002-08-03 8:26:30.400 am 格式的字符串 */ public final static String FMT_yyyyMMddHHmmssSa_12 = "yyyy-MM-dd KK:mm:ss.S a"; /** * 把時間格式化成如:2002-08-03 8:26:16 am 格式的字符串 */ public final static String FMT_yyyyMMddHHmmssa_12 = "yyyy-MM-dd KK:mm:ss a"; /** * 把時間格式化成如:2002-08-03 8:26 am 格式的字符串 */ public final static String FMT_yyyyMMddHHmma_12 = "yyyy-MM-dd KK:mm a"; /** * 把時間格式化成如:2002-08-03 8 am 格式的字符串 */ public final static String FMT_yyyyMMddHHa_12 = "yyyy-MM-dd KK a"; /** * 把時間格式化成如:2002-08-03 08:26:30.400 am 格式的字符串 */ public final static String FMT_yyyyMMddHHmmssSa = "yyyy-MM-dd HH:mm:ss.S a"; /** * 把時間格式化成如:2002-08-03 08:26:30.400 格式的字符串 */ public final static String FMT_yyyyMMddHHmmssS = "yyyy-MM-dd HH:mm:ss.S"; /** * 把時間格式化成如:2002-08-03 08:26:16 格式的字符串 */ public final static String FMT_yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss"; /** * 把時間格式化成如:2002-08-03 08:26 格式的字符串 */ public final static String FMT_yyyyMMddHHmm = "yyyy-MM-dd HH:mm"; /** * 把時間格式化成如:2002-08-03 08 格式的字符串 */ public final static String FMT_yyyyMMddHH = "yyyy-MM-dd HH"; /** * 把時間格式化成如:2002-07-05 am 格式的字符串 */ public final static String FMT_yyyyMMdda = "yyyy-MM-dd a"; /** * 把時間格式化成如:2002-07-05 格式的字符串 */ public final static String FMT_yyyyMMdd = "yyyy-MM-dd"; /** * 把時間格式化成如:2002-07 格式的字符串 */ public final static String FMT_yyyyMM = "yyyy-MM"; /** * 把時間格式化成如:20020803082630400格式的(17位)字符串 */ public final static String FMT_yyyyMMddHHmmssS_17 = "yyyyMMddHHmmssS"; /** * 把時間格式化成如:20020803082630格式的(14位)字符串 */ public final static String FMT_yyyyMMddHHmmss_14 = "yyyyMMddHHmmss"; /** * 把時間格式化成如:20020806 格式的(8位)字符串 */ public final static String FMT_yyyyMMdd_8= "yyyyMMdd"; /** * 把時間格式化成如:200208 格式的(6位)字符串 */ public final static String FMT_yyyyMM_6= "yyyyMM"; /** * 把時間格式化成如:12:08 PM(下午) 格式的字符串 */ public final static String FMT_HHmmA_12 = "KK:mm a"; /** * 把時間格式化成如:0:55 AM上午,CST 格式的字符串 */ public final static String FMT_HHmmAz_12 = "KK:mm a,z"; /** * 把時間格式化成如:0:56 AM上午,中國標准時間 格式的字符串 */ public final static String FMT_HHmmAzzzz_12 = "KK:mm a,zzzz"; /** * 把時間格式化成如:12:08:23 am 格式的字符串 */ public final static String FMT_HHmmssA_12 = "KK:mm:ss a"; /** * 把時間格式化成如:0:55:33 AM上午,CST 格式的字符串 */ public final static String FMT_HHmmssAz_12 = "KK:mm:ss a,z"; /** * 把時間格式化成如:0:56:23 AM上午,中國標准時間 格式的字符串 */ public final static String FMT_HHmmssAzzzz_12 = "KK:mm:ss a,zzzz"; /** * 把時間格式化成如:22:04:45 格式的字符串 */ public final static String FMT_HHmmss = "HH:mm:ss"; /** * 把時間格式化成如:22:04:45.824 格式的字符串 */ public final static String FMT_HHmmssS = "HH:mm:ss.S"; /** * 把時間格式化成如:22:04 格式的字符串 */ public final static String FMT_HHmm = "HH:mm"; /** * 把時間格式化成如:22:04,CST 格式的字符串 */ public final static String FMT_HHmmz = "HH:mm,z"; /** * 把時間格式化成如:22:04,中國標准時間 格式的字符串 */ public final static String FMT_HHmmzzzz = "HH:mm,zzzz"; /** * 把時間格式化成如:Sun,Nov 14,'2004 格式的字符串 */ public final static String FMT_WWMMDDYY_EN = "EEE,MMM d,''yyyy"; /** * 把時間格式化成如:星期日,2004年十一月14號 格式的字符串 */ public final static String FMT_WWMMDDYY_CN = "EEE,yyyy年MMMd號"; /** * 把時間格式化成如:Sun,Nov 14,'2004 格式的字符串 */ public final static String FMT_MMDDYY_EN = "MMM d,''yyyy"; /** * 把時間格式化成如:星期日,2004年十一月14號 格式的字符串 */ public final static String FMT_MMDDYY_CN = "yyyy年MMMd號"; /** * 把時間格式化成如:星期幾 格式的字符串,即可獲得該日這個時間是星期幾 */ public final static String FMT_WW = "EEE"; /** * 常用的格式化時間的格式組,用於本類中格式化字符串成時間型 */ private final static String[] formatStr = { FMT_yyyyMMddHHmmssS, FMT_yyyyMMddHHmmss, FMT_yyyyMMddHHmm, FMT_yyyyMMddHH, FMT_yyyyMMdd, FMT_HHmmss, FMT_HHmmssS, FMT_HHmm, FMT_HHmmz, FMT_HHmmzzzz, FMT_yyyyMMddHHmmssSa_12, FMT_yyyyMMddHHmmssa_12, FMT_yyyyMMddHHmma_12, FMT_yyyyMMddHHa_12, FMT_yyyyMMdda, FMT_HHmmA_12, FMT_HHmmAz_12, FMT_HHmmAzzzz_12, FMT_HHmmssA_12, FMT_HHmmssAz_12, FMT_HHmmssAzzzz_12, FMT_yyyyMMddHHmmssSa }; /** * 私有化構造器,使得不能產生該類對象,類中所有的方法均為靜態方法 */ private DateTimeUtil() { } /** * 根據給出的Date值和格式串采用操作系統的默認所在的國家風格來格式化時間,並返回相應的字符串 * * @param date * 日期對象 * @param formatStr * 日期格式 * @return 如果為null,返回字符串"" */ public static String formatDateTimetoString(Date date, String formatStr) { String reStr = ""; if (date == null || formatStr == null || formatStr.trim().length() < 1) { return reStr; } SimpleDateFormat sdf = new SimpleDateFormat(); sdf.applyPattern(formatStr); reStr = sdf.format(date); return reStr == null ? "" : reStr; } /** * 獲取系統時間 * * @param fmtstr * 日期格式 * @return 系統時間 */ public static Date getSystemDate(String fmtstr) { try { return parseToDate(formatDateTimetoString(getSystemDate(), fmtstr)); } catch (Exception e) { e.printStackTrace(); return getSystemDate(); } } /** * 根據給出的Date值和格式串采用給定的國家所在的國家風格來格式化時間,並返回相應的字符串 * * @param date * 日期對象 * @param formatStr * 日期格式 * @param locale * 日期格式符號要被使用的語言環境 * @return 如果為null,返回字符串"" */ public static String formatDateTimetoString(Date date, String formatStr, Locale locale) { String reStr = ""; if (date == null || formatStr == null || locale == null || formatStr.trim().length() < 1) { return reStr; } SimpleDateFormat sdf = new SimpleDateFormat(formatStr, locale); reStr = sdf.format(date); return reStr == null ? "" : reStr; } /** * 根據給出的Date值字符串和格式串采用操作系統的默認所在的國家風格來格式化時間,並返回相應的字符串 * * @param dateStr * 日期字符串 * @param formatStr * 日期格式 * @return 如果為null,返回"" * @throws Exception * 可能拋出的異常 */ public static String formatDateTimetoString(String dateStr, String formatStr) throws Exception { String dStr = ""; if (dateStr != null && dateStr.trim().length() > 0 && formatStr != null && formatStr.trim().length() > 0) { dStr = formatDateTimetoString(parseToDate(dateStr), formatStr); } return dStr; } /** * 根據給出的Date值字符串和格式串采用指定國家的風格來格式化時間,並返回相應的字符串 * * @param dateStr * 日期字符串 * @param formatStr * 日期格式 * @param locale * 日期格式符號要被使用的語言環境 * @return 如果為null,返回"" * @throws Exception * 可能拋出的異常 */ public static String formatDateTimetoString(String dateStr, String formatStr, Locale locale) throws Exception { String dStr = ""; if (dateStr != null && dateStr.trim().length() > 0 && formatStr != null && formatStr.trim().length() > 0 && locale != null) { dStr = formatDateTimetoString(parseToDate(dateStr, locale), formatStr, locale); } return dStr; } /** * 按指定的格式和操作系統默認國家的風格把給定的日期字符串格式化為一個Date型日期 * * @param dateTimeStr * 日期毫秒字符串 * @param formatStr * 日期格式 * @return java.util.Date類型對象 * @throws Exception * 可能拋出的異常 */ public static Date parseToDate(String dateTimeStr, String formatStr) throws Exception { if (dateTimeStr == null || formatStr == null || dateTimeStr.trim().length() < 1 || formatStr.trim().length() < 1) { throw new IllegalArgumentException("參數dateTimeStr、formatStr不能是null或空格串!"); } SimpleDateFormat sdf = new SimpleDateFormat(formatStr); try { return sdf.parse(dateTimeStr); } catch (ParseException e) { throw new Exception(e); } } /** * 按指定的格式和指定國家的風格把給定的日期字符串格式化為一個Date型日期 * * @param dateTimeStr * 日期字符串 * @param formatStr * 日期格式 * @param locale * 日期格式符號要被使用的語言環境 * @return java.util.Date類型對象 * @throws Exception * 可能拋出的異常 */ public static Date parseToDate(String dateTimeStr, String formatStr, Locale locale) throws Exception { if (dateTimeStr != null && formatStr != null && locale != null && dateTimeStr.trim().length() > 0 && formatStr.trim().length() > 0) { SimpleDateFormat sdf = new SimpleDateFormat(formatStr, locale); try { return sdf.parse(dateTimeStr); } catch (ParseException e) { throw new Exception(e); } } else { throw new IllegalArgumentException("參數dateTimeStr、formatStr、locale不能是null或空格串!"); } } /** * 按操作系統默認國家的風格把給定的日期字符串格式化為一個Date型日期 * * @param dateTimeStr * 日期字符串 * @return java.util.Date類型對象 * @throws Exception * 可能拋出的異常 */ public static Date parseToDate(String dateTimeStr) throws Exception { if (dateTimeStr == null || dateTimeStr.trim().length() < 1) { throw new IllegalArgumentException("參數dateTimeSt不能是null或空格串!"); } int formatStrLength = formatStr.length; int i = 0; for (i = 0; i < formatStrLength; i++) { SimpleDateFormat sdf = new SimpleDateFormat(formatStr[i]); try { return sdf.parse(dateTimeStr); } catch (ParseException e) { } } throw new Exception("日期格式不正確!"); } /** * 根據給出的年月和日返回一個日期型的對象 * * @param year * 年 * @param month * 月 ,1到12 * @param day * 日 ,1到31 * @return java.util.Date類型對象 * @throws Exception * 可能拋出的異常 */ public static Date parseToDate(int year, int month, int day) throws Exception { if (month < 1 || month > 12 || day < 1 || day > 31) { throw new IllegalArgumentException("參數不正確!"); } String yearStr = String.valueOf(year); String monthStr = String.valueOf(month); String dayStr = String.valueOf(day); return parseToDate(yearStr + "-" + monthStr + "-" + dayStr); } /** * 根據給出的年月日、時分秒、返回一個對應的Date型對象 * * @param year * 年 * @param month * 月 ,1到12 * @param day * 日 ,1到31 * @param h * 小時,從0到23 * @param m * 分,從0到60 * @param s * 秒,從0到60 * @return java.util.Date類型對象 * @throws Exception * 可能拋出的異常 */ public static Date parseToDate(int year, int month, int day, int h, int m, int s) throws Exception { if (month < 1 || month > 12 || day < 1 || day > 31 || h < 0 || h > 23 || m < 0 || m > 60 || s < 0 || s > 60) { throw new IllegalArgumentException("參數不正確!"); } String yearStr = String.valueOf(year); String monthStr = String.valueOf(month); String dayStr = String.valueOf(day); String hStr = String.valueOf(h); String mStr = String.valueOf(m); String sStr = String.valueOf(s); return parseToDate(yearStr + "-" + monthStr + "-" + dayStr + " " + hStr + ":" + mStr + ":" + sStr); } /** * 按指定國家的風格把給定的日期字符串格式化為一個Date型日期 * * @param dateTimeStr * 日期字符串 * @param locale * 日期格式符號要被使用的語言環境 * @return java.util.Date類型對象 * @throws Exception * 可能拋出的異常 */ public static Date parseToDate(String dateTimeStr, Locale locale) throws Exception { if (dateTimeStr == null || dateTimeStr.trim().length() < 1 || locale == null) { throw new IllegalArgumentException("參數dateTimeSt、locale不能是null或空格串!"); } int formatStrLength = formatStr.length; int i = 0; SimpleDateFormat sdf = null; for (i = 0; i < formatStrLength; i++) { sdf = new SimpleDateFormat(formatStr[i], locale); try { return sdf.parse(dateTimeStr); } catch (ParseException e) { } } throw new Exception("日期格式不正確!"); } /** * 將給定的日期時間字符串按操作系統默認的國家風格格式化成"yyyy-MM-dd HH:mm:ss"格式的日期時間串 * * @param dateTimeStr * 日期字符串 * @return 如果為null,返回"" * @throws Exception * 可能拋出的異常 */ public static String formatDateTimetoString(String dateTimeStr) throws Exception { return formatDateTimetoString(dateTimeStr, FMT_yyyyMMddHHmmss); } /** * 將給定的日期時間字符串按指定國家的風格格式化成"yyyy-MM-dd HH:mm:ss"格式的日期時間串 * * @param dateTimeStr * 日期字符串 * @param locale * 日期格式符號要被使用的語言環境 * @return 如果為null,返回"" * @throws Exception * 可能拋出的異常 */ public static String formatDateTimetoString(String dateTimeStr, Locale locale) throws Exception { return formatDateTimetoString(dateTimeStr, FMT_yyyyMMddHHmmss, locale); } /** * 將給定的日期時間按操作系統默認的國家內格格式化成"yyyy-MM-dd HH:mm:ss"格式的日期時間串 * * @param dateTime * 日期對象 * @return 如果為null,返回"" */ public static String formatDateTimetoString(Date dateTime) { return formatDateTimetoString(dateTime, FMT_yyyyMMddHHmmss); } /** * 將給定的日期時間按指定國家的風格格式化成"yyyy-MM-dd HH:mm:ss"格式的日期時間串 * * @param dateTime * 日期對象 * @param locale * 日期格式符號要被使用的語言環境 * @return 如果為null,返回"" */ public static String formatDateTimetoString(Date dateTime, Locale locale) { return formatDateTimetoString(dateTime, FMT_yyyyMMddHHmmss, locale); } /** * 將給定的日期字符串按操作系統默認的國家風格格式化成"yyyy-MM-dd"格式的日期串 * * @param dateStr * 日期字符串 * @return 如果為null,返回"" * @throws Exception * 可能拋出的異常 */ public static String formatDatetoString(String dateStr) throws Exception { return formatDateTimetoString(dateStr, FMT_yyyyMMdd); } /** * 將給定的日期字符串按指定國家的風格格式化成"yyyy-MM-dd"格式的日期串 * * @param dateStr * 日期字符串 * @param locale * 日期格式符號要被使用的語言環境 * @return 如果為null,返回"" * @throws Exception * 可能拋出的異常 */ public static String formatDatetoString(String dateStr, Locale locale) throws Exception { return formatDateTimetoString(dateStr, FMT_yyyyMMdd, locale); } /** * 將給定的日期按指定操作系統默認國家的風格格式化成"yyyy-MM-dd"格式的日期串 * * @param d * 日期對象 * @return 如果為null,返回"" */ public static String formatDatetoString(Date d) { return formatDateTimetoString(d, FMT_yyyyMMdd); } /** * 將給定的日期按指定國家的風格格式化成"yyyy-MM-dd"格式的日期串 * * @param d * 日期對象 * @param locale * 日期格式符號要被使用的語言環境 * @return 如果為null,返回"" */ public static String formatDatetoString(Date d, Locale locale) { return formatDateTimetoString(d, FMT_yyyyMMdd, locale); } /** * 將給定的日期時間字符串按操作系統默認的國家風格格式化成"HH:mm:ss"格式的時間串 * * @param dateTimeStr * 日期字符串 * @return 如果為null,返回"" * @throws Exception * 可能拋出的異常 */ public static String formatTimetoString(String dateTimeStr) throws Exception { return formatDateTimetoString(dateTimeStr, FMT_HHmmss); } /** * 將給定的日期時間字符串按指定國家的風格格式化成"HH:mm:ss"格式的時間串 * * @param dateTimeStr * 日期字符串 * @param locale * 日期格式符號要被使用的語言環境 * @return 如果為null,返回"" * @throws Exception * 可能拋出的異常 */ public static String formatTimetoString(String dateTimeStr, Locale locale) throws Exception { return formatDateTimetoString(dateTimeStr, FMT_HHmmss, locale); } /** * 將給定的日期時間按指定操作系統默認國家的風格格式化成"HH:mm:ss"格式的時間串 * * @param dateTimeStr * 日期字符串 * @return 如果為null,返回"" */ public static String formatTimetoString(Date dateTimeStr) { return formatDateTimetoString(dateTimeStr, FMT_HHmmss); } /** * 將給定的日期時間按指定國家的風格格式化成"HH:mm:ss"格式的時間串 * * @param dateTimeStr * 日期字符串 * @param locale * 日期格式符號要被使用的語言環境 * @return 如果為null,返回"" */ public static String formatTimetoString(Date dateTimeStr, Locale locale) { return formatDateTimetoString(dateTimeStr, FMT_HHmmss, locale); } /** * 返回一個時間的年份整數 * * @param d * 日期對象 * @return 年份 */ public static int getYearOfDate(Date d) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); return calendar.get(Calendar.YEAR); } /** * 返回一個時間的月份整數 * * @param d * 日期對象 * @return 月份 */ public static int getMonthOfYear(Date d) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); return calendar.get(Calendar.MONTH) + 1; } /** * 獲取指定日期的1號0點0分0秒 * * @param d * 指定日期 * @return 指定日期的0點0分0秒 */ public static Date getDateByFirstDayOfMonth(Date d) { Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); } /** * 返回一個時間的天份整數,是這個月的第幾天 * * @param d * 日期對象 * @return 天份 */ public static int getDayOfMonth(Date d) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); return calendar.get(Calendar.DAY_OF_MONTH); } /** * 返回一個時間的天份整數,是這個年份的第幾天 * * @param d * 日期對象 * @return 天份 */ public static int getDayOfYear(Date d) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); return calendar.get(Calendar.DAY_OF_YEAR); } /** * 返回一個時間的天份整數,是這個周的第幾天 * * @param d * 日期對象 * @return 天份 */ public static int getDayOfWeek(Date d) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); return calendar.get(Calendar.DAY_OF_WEEK) - 1; } /** * 返回一個時間的周的整數,是這個月的第幾周 * * @param d * 日期對象 * @return 周 */ public static int getWeekOfMonth(Date d) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); return calendar.get(Calendar.WEEK_OF_MONTH); } /** * 返回一個時間的周的整數,是這個年份的第幾周 * * @param d * 日期對象 * @return 周 */ public static int getWeekOfYear(Date d) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); return calendar.get(Calendar.WEEK_OF_YEAR); } /** * 返回該時間所對應的在一天中的小時數的整數,如當前(Date now)是下午3點,返回為15 * * @param d * 日期對象 * @return 小時 */ public static int getHoursOfDay(Date d) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); int hours = calendar.get(Calendar.HOUR_OF_DAY); return hours; } /** * 返回該時間所對應的在一天中的小時數的整數(采用12小時制),如當前(Date now)是下午3點,返回為3 * * @param d * 日期對象 * @return 小時 */ public static int getHoursOfDay12(Date d) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); int hours = calendar.get(Calendar.HOUR); return hours; } /** * 返回該時間所對應的分鍾數中的整數,如now是15點14分,則返回14 * * @param d * 日期對象 * @return 分鍾 */ public static int getMinutesOfHour(Date d) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); int minutes = calendar.get(Calendar.MINUTE); return minutes; } /** * 返回該時間所對應的秒數中的整數,如now是15點14分34秒,則返回34 * * @param d * 日期對象 * @return 秒 */ public static int getSecondsOfMinute(Date d) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); int seconds = calendar.get(Calendar.SECOND); return seconds; } /** * 返回該時間所對應的毫秒數中的整數,如now是15點14分34秒470毫秒,則返回470 * * @param d * 日期對象 * @return 毫秒 */ public static int getMillisecondsOfSecond(Date d) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); int millisecond = calendar.get(Calendar.MILLISECOND); return millisecond; } /** * 返回該時間相對於1970年1月1日開始計算的對應的毫秒數 * * @param d * 日期對象 * @return 毫秒數 */ public static long getTime(Date d) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } return d.getTime(); } /** * 比較兩個時間的先后順序。 如果時間d1在d2之前,返回1,如果時間d1在d2之后,返回-1,如果二者相等,返回0 * * @param d1 * 日期對象 * @param d2 * 日期對象 * @return 如果時間d1在d2之前,返回1,如果時間d1在d2之后,返回-1,如果二者相等,返回0 */ public static int compareTwoDate(Date d1, Date d2) { if (d1 == null || d2 == null) { throw new IllegalArgumentException("參數d1或d2不能是null對象!"); } long dI1 = d1.getTime(); long dI2 = d2.getTime(); if (dI1 > dI2) { return -1; } else if (dI1 < dI2) { return 1; } else { return 0; } } /** * 返回兩個日期之間的毫秒數的差距 * * @param d1 * 日期對象 * @param d2 * 日期對象 * @return 二者至1970年1.1后的毫秒數的差值 */ public static long getMillisecondsOfTwoDate(Date d1, Date d2) { if (d1 == null || d2 == null) { throw new IllegalArgumentException("參數d1或d2不能是null對象!"); } long dI1 = d1.getTime(); long dI2 = d2.getTime(); return (dI1 - dI2); } /** * 獲得兩個日期之間相差的秒數 * * @param d1 * 日期對象 * @param d2 * 日期對象 * @return 兩日期之間相差的秒數 */ public static double getSecondsOfTwoDate(Date d1, Date d2) { if (d1 == null || d2 == null) { throw new IllegalArgumentException("參數d1或d2不能是null對象!"); } long i = getMillisecondsOfTwoDate(d1, d2); return (double) i / 1000; } /** * 獲得兩個日期之間相差的分鍾數 * * @param d1 * 日期對象 * @param d2 * 日期對象 * @return 兩日期之間相差的分鍾數 */ public static double getMinutesOfTwoDate(Date d1, Date d2) { if (d1 == null || d2 == null) { throw new IllegalArgumentException("參數d1或d2不能是null對象!"); } long millions = getMillisecondsOfTwoDate(d1, d2); return (double) millions / 60 / 1000; } /** * 獲得兩個日期之間相差的小時數 * * @param d1 * 日期對象 * @param d2 * 日期對象 * @return 兩日期之間相差的小時數 */ public static double getHoursOfTwoDate(Date d1, Date d2) { if (d1 == null || d2 == null) { throw new IllegalArgumentException("參數d1或d2不能是null對象!"); } long millions = getMillisecondsOfTwoDate(d1, d2); return (double) millions / 60 / 60 / 1000; } /** * 獲得兩個日期之間相差的天數 * * @param d1 * 日期對象 * @param d2 * 日期對象 * @return 兩日期之間相差的天數 */ public static double getDaysOfTwoDate(Date d1, Date d2) { if (d1 == null || d2 == null) { throw new IllegalArgumentException("參數d1或d2不能是null對象!"); } long millions = getMillisecondsOfTwoDate(d1, d2); return (double) millions / 24 / 60 / 60 / 1000; } /** * 把給定的時間加上指定的時間值,可以為負 * * @param d * 需要設定的日期對象 * @param times * 時間值 * @param type * 類型,Calendar.MILLISECOND,毫秒<BR> * Calendar.SECOND,秒<BR> * Calendar.MINUTE,分鍾<BR> * Calendar.HOUR,小時<BR> * Calendar.DATE,日<BR> * @return 如果d為null,返回null */ public static Date addTime(Date d, double times, int type) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } long qv = 1; switch (type) { case Calendar.MILLISECOND: qv = 1; break; case Calendar.SECOND: qv = 1000; break; case Calendar.MINUTE: qv = 1000 * 60; break; case Calendar.HOUR: qv = 1000 * 60 * 60; break; case Calendar.DATE: qv = 1000 * 60 * 60 * 24; break; default: throw new RuntimeException("時間類型不正確!type=" + type); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); long milliseconds = (long) Math.round(Math.abs(times) * qv); if (times > 0) { for (; milliseconds > 0; milliseconds -= 2147483647) { if (milliseconds > 2147483647) { calendar.add(Calendar.MILLISECOND, 2147483647); } else { calendar.add(Calendar.MILLISECOND, (int) milliseconds); } } } else { for (; milliseconds > 0; milliseconds -= 2147483647) { if (milliseconds > 2147483647) { calendar.add(Calendar.MILLISECOND, -2147483647); } else { calendar.add(Calendar.MILLISECOND, -(int) milliseconds); } } } return calendar.getTime(); } /** * 把給定的時間加上指定的年份,可以為負,返回新的被加上了年份的日期對象,不影響參數日期對象值 * * @param d * 需要設定的日期對象 * @param years * 年份 * @return 新日期對象 */ public static Date addYears(Date d, int years) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.add(Calendar.YEAR, years); return calendar.getTime(); } /** * 把給定的時間加上指定的月份,可以為負 * * @param d * 需要設定的日期對象 * @param months * 月份 * @return 新日期對象 */ public static Date addMonths(Date d, int months) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.add(Calendar.MONTH, months); return calendar.getTime(); } /** * 把給定的時間加上指定的天數,可以為負 * * @param d * 需要設定的日期對象 * @param days * 天數 * @return 新日期對象 */ public static Date addDays(Date d, int days) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.add(Calendar.HOUR, days * 24); return calendar.getTime(); } /** * 把給定的時間加上指定的小時,可以為負 * * @param d * 需要設定的日期對象 * @param hours * 小時 * @return 新日期對象 */ public static Date addHours(Date d, int hours) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.add(Calendar.HOUR, hours); return calendar.getTime(); } /** * 把給定的時間加上指定的分鍾,可以為負 * * @param d * 需要設定的日期對象 * @param minutes * 分鍾 * @return 新日期對象 */ public static Date addMinutes(Date d, int minutes) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.add(Calendar.MINUTE, minutes); return calendar.getTime(); } /** * 把給定的時間加上指定的秒數,可以為負 * * @param d * 需要設定的日期對象 * @param seconds * 秒 * @return 新日期對象 */ public static Date addSeconds(Date d, int seconds) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.add(Calendar.SECOND, seconds); return calendar.getTime(); } /** * 把給定的時間加上指定的毫秒數,可以為負 * * @param d * 需要設定的日期對象 * @param milliseconds * 毫秒 * @return 新日期對象 */ public static Date addMilliseconds(Date d, int milliseconds) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.add(Calendar.MILLISECOND, milliseconds); return calendar.getTime(); } /** * 設置一個日期對象的年份是新的給定的年份 * * @param d * 需要設定的日期對象 * @param year * 新的年份 * @return 新日期對象 */ public static Date setYearOfDate(Date d, int year) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(Calendar.YEAR, year); return calendar.getTime(); } /** * 設置一個日期對象的月份是新的給定的月份 * * @param d * 需要設定的日期對象 * @param month * 新的月份 * @return 新日期對象 */ public static Date setMonthOfDate(Date d, int month) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(Calendar.MONTH, month); return calendar.getTime(); } /** * 設置一個日期對象的天是新的給定的天 * * @param d * 需要設定的日期對象 * @param day * 新的天 * @return 新日期對象 */ public static Date setDayOfDate(Date d, int day) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(Calendar.DAY_OF_MONTH, day); return calendar.getTime(); } /** * 設置一個日期對象的小時是新的給定的小時 * * @param d * 需要設定的日期對象 * @param hour * 新的小時數 * @return 新日期對象 */ public static Date setHourOfDate(Date d, int hour) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(Calendar.HOUR_OF_DAY, hour); return calendar.getTime(); } /** * 設置一個日期對象的分鍾是新的給定的分鍾數 * * @param d * 需要設定的日期對象 * @param minute * 新的分鍾數 * @return 新日期對象 */ public static Date setMinuteOfDate(Date d, int minute) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(Calendar.MINUTE, minute); return calendar.getTime(); } /** * 設置一個日期對象的秒數是新的給定的分鍾數 * * @param d * 需要設定的日期對象 * @param second * 新的秒數 * @return 新日期對象 */ public static Date setSecondOfDate(Date d, int second) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(Calendar.SECOND, second); return calendar.getTime(); } /** * 設置一個日期對象的毫秒數是新的給定的分鍾數 * * @param d * 需要設定的日期對象 * @param millisecond * 新的毫秒數 * @return 新日期對象 */ public static Date setMillisecondOfDate(Date d, int millisecond) { if (d == null) { throw new IllegalArgumentException("參數d不能是null對象!"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(Calendar.MILLISECOND, millisecond); return calendar.getTime(); } /** * 返回指定日期的月份的天數量 * * @param d * 日期對象 * @return 天數 */ public static int getDaysOfMonth(Date d) { int year = getYearOfDate(d); int month = getMonthOfYear(d); return getDaysOfMonth(year, month); } /** * 返回指定日期的月份的天數量 * * @param year * 年 * @param month * 月 * @return 天數 */ public static int getDaysOfMonth(int year, int month) { int days = 0; if (month == 2) { if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { days = 29; } else { days = 28; } } if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) { days = 30; } if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) { days = 31; } return days; } /** * 返回系統時間,以日期對象形式返回 * * @return 系統時間 */ public static Date getSystemDate() { return new Date(System.currentTimeMillis()); } /** * 返回系統時間,以毫秒形式返回 * * @return 毫秒數 */ public static long getSystemTime() { return System.currentTimeMillis(); } /** * 返回24小時前的時間 * * @param date * 指定日期 * @return 新日期對象 */ public static Date getLastDay(Date date) { long day = date.getTime(); long lastDay = day - 24 * 60 * 60 * 1000; return new Date(lastDay); } /** * 返回24小時后的時間 * * @param date * 指定日期 * @return 新日期對象 */ public static Date getTomorrow(Date date) { long day = date.getTime(); long tomorrow = day + 24 * 60 * 60 * 1000; return new Date(tomorrow); } /** * 取得30天前的這個時間 * * @return 新日期對象 */ public static Date getDayLastMonth() { long day = new Date().getTime(); long dayLastMonth = day - 24 * 60 * 60 * 1000 * 20; return new Date(dayLastMonth); } /** * 取得30天后的這個時間 * * @return 新日期對象 */ public static Date getDayNextMonth() { long day = new Date().getTime(); long dayNextMonth = day + 20 * 24 * 60 * 60 * 1000; return new Date(dayNextMonth); } /** * 計算兩個時間見得月份差,可為負數 * * @param sDate * 開始時間 * @param eDate * 結束時間 * @return 月份差 */ public static int getMonthCount(Date sDate, Date eDate) { String sDateStr = DateTimeUtil.formatDateTimetoString(sDate, "MM"); String eDateStr = DateTimeUtil.formatDateTimetoString(eDate, "MM"); int monthCount = Integer.parseInt(eDateStr) - Integer.parseInt(sDateStr) + 1; return monthCount; } /** * 計算兩個時間見得年份差,可為負數 * * @param sDate * 開始時間 * @param eDate * 結束時間 * @return 年份差 */ public static int getYearCount(Date sDate, Date eDate) { String sDateStr = DateTimeUtil.formatDateTimetoString(sDate, "yyyy"); String eDateStr = DateTimeUtil.formatDateTimetoString(eDate, "yyyy"); return Integer.parseInt(eDateStr) - Integer.parseInt(sDateStr); } /** * 取得下個月的這天,比如2月1日可取得3月1日,此方法有很大局限性,不能用於月末的天數 * * @param date * 指定日期 * @return 新日期對象 */ public static Date getDayNextMonth(Date date) { String yearStr = DateTimeUtil.formatDateTimetoString(date, "yyyy"); String monthStr = DateTimeUtil.formatDateTimetoString(date, "MM"); String dayStr = DateTimeUtil.formatDateTimetoString(date, "dd"); int year = Integer.parseInt(yearStr); int month = Integer.parseInt(monthStr); if (month == 12) { month = 1; year = year + 1; yearStr = String.valueOf(year); monthStr = String.valueOf(month); } String dateStr = yearStr + "-" + monthStr + "-" + dayStr; try { date = DateTimeUtil.parseToDate(dateStr); } catch (Exception e) { e.printStackTrace(); } return date; } /** * 獲取當月開始時0點0分0秒 * * @return 日期對象 */ public static Date getCurrentMouthStart() { Date d = getSystemDate(); d = setDayOfDate(d, 1); d = setHourOfDate(d, 0); d = setMinuteOfDate(d, 0); d = setSecondOfDate(d, 0); return d; } /** * 返回下月的這天 * * @param date * 指定日期 * @return 日期對象 */ public static Date getDateNextMonth(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, +1); return cal.getTime(); } /** * 默認方法,計算指定時間與當前時間之間的相差的天數 * * @param date * 指定日期 * @return 所差天數 */ public static Integer daysDifference(Date date) { long l1 = get24HourMill(date); long l2 = get24HourMill(new Date()); return (int) ((l2 - l1) / 86400 / 1000); } /** * 獲取指定日期的 0點0分0秒 * * @param d * 指定日期 * @return 指定日期的0點0分0秒 */ private static long get24HourMill(Date d) { Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTimeInMillis(); } /** * 獲取指定日期的 0點0分0秒 * * @param d * 指定日期 * @return 指定日期的0點0分0秒 */ public static Date getZeroDate(Date d) { if (d == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); } /** * 獲取指定日期的 23點59分59秒 * * @param d * 指定日期 * @return 指定日期的0點0分0秒 */ public static Date getLastDate(Date date) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); } /** * 不確定表示方式的時間差函數 * * @param startDate * 指定時間 * @param endDate * 結束時間 * @return 時間差指定格式字符串 */ private static String otherDiff(Date startDate, Date endDate) { String[] type = new String[] { "年", "個月", "星期", "天", "小時", "分鍾", "秒", "秒" }; Object[] obj = timeDifference(startDate, endDate); String value = "1秒前"; for (int i = 0; i < obj.length; i++) { if (!"0".equals(obj[i].toString())) { value = obj[i].toString() + type[i] + "前"; break; } } return value; } /** * 動態表示方式的時間差函數 * * @param startDate * 指定時間 * @param endDate * 結束時間 * @return 時間差指定格式字符串 */ public static String dynDiff(Date startDate, Date endDate) { String startDay = DateFormatUtils.format(startDate, "dd"); String endtDay = DateFormatUtils.format(endDate, "dd"); String value = ""; if (startDay.equals(endtDay)) { value = DateFormatUtils.format(startDate, " HH:mm"); } else { value = otherDiff(startDate, endDate); } return value; } /** * 資源表示方式的時間差函數 * * @param startDate * 指定時間 * @param endDate * 結束時間 * @return 時間差指定格式字符串 */ public static String resDiff(Date startDate, Date endDate) { Object[] obj = timeDifference(startDate, endDate); String value = ""; if (Long.parseLong(obj[3].toString()) > 7) { value = DateFormatUtils.format(startDate, "yyyy-MM-dd HH:mm"); } else { value = otherDiff(startDate, endDate); } return value; } /** * 時間差 * * @param startTime * 開始時間 * @param endTime * 結束時間 * @return 返回時間差數組:(年,月,周,天,時,分,秒,毫秒) */ private static Object[] timeDifference(Date startTime, Date endTime) { if (startTime == null || endTime == null) { return new Object[] { 0, 0, 0, 0, 0, 0, 0 }; } else { Calendar start = Calendar.getInstance(); Calendar end = Calendar.getInstance(); start.setTime(startTime); end.setTime(endTime); long startMs = start.getTimeInMillis(); long endMs = end.getTimeInMillis(); long l_differ = endMs - startMs;// 毫秒數 long ll_differ = l_differ / 1000;// 秒 long second_differ = l_differ / 1000;// 秒 long year_differ = second_differ / (60 * 60 * 24 * 365);// 得到年數 long month_differ = second_differ / (60 * 60 * 24 * 30);// 得到月數 long week_differ = second_differ / (60 * 60 * 24 * 7);// 得到周數 long day_differ = second_differ / (60 * 60 * 24);// 得到天數 second_differ = second_differ - day_differ * 60 * 60 * 24;// 天 long hour_differ = second_differ / (60 * 60);// 時 second_differ = second_differ - hour_differ * 60 * 60; long minute_differ = second_differ / 60;// 分 second_differ = second_differ - minute_differ * 60; return new Object[] { year_differ, month_differ, week_differ, day_differ, hour_differ, minute_differ, second_differ, ll_differ }; } } /** * 流逝時間 * @return 時間 * @throws Exception */ public static String getPastDate(Date date) throws Exception { String timeStr; Date currDate = getSystemDate(); // 今天以前 if (date.before(getZeroDate(currDate))) { timeStr = DateTimeUtil.formatDatetoString(date); } else { // 時 double hours = DateTimeUtil.getHoursOfTwoDate(currDate, date); if (hours < 24 && hours >= 1) { timeStr = String.valueOf((int) hours) + "小時前"; } else { // 分 double minutes = DateTimeUtil.getMinutesOfTwoDate(currDate, date); if (minutes < 60 && minutes >= 1) { timeStr = String.valueOf((int) minutes) + "分鍾前"; } else { // 秒 double seconds = DateTimeUtil.getSecondsOfTwoDate(currDate, date); if (seconds < 60 && seconds >= 1) { // timeStr = String.valueOf(seconds)+"秒前"; timeStr = "剛剛"; } else { timeStr = "-"; } } } } return timeStr; } }