# 时间相关 ``` java import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** *
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/8/2
 *     desc  : 时间相关的工具类
 * 
*/ public class TimeUtils { private TimeUtils() { throw new UnsupportedOperationException("u can't fuck me..."); } /** *

在工具类中经常使用到工具类的格式化描述,这个主要是一个日期的操作类,所以日志格式主要使用 SimpleDateFormat的定义格式.

* 格式的意义如下: 日期和时间模式
*

日期和时间格式由日期和时间模式字符串指定。在日期和时间模式字符串中,未加引号的字母 'A' 到 'Z' 和 'a' 到 'z' * 被解释为模式字母,用来表示日期或时间字符串元素。文本可以使用单引号 (') 引起来,以免进行解释。"''" * 表示单引号。所有其他字符均不解释;只是在格式化时将它们简单复制到输出字符串,或者在分析时与输入字符串进行匹配。 *

* 定义了以下模式字母(所有其他字符 'A' 到 'Z' 和 'a' 到 'z' 都被保留):
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
字母日期或时间元素表示示例
GEra 标志符TextAD
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
*
     *                     yyyy-MM-dd 1969-12-31
     *                     yyyy-MM-dd 1970-01-01
     *               yyyy-MM-dd HH:mm 1969-12-31 16:00
     *               yyyy-MM-dd HH:mm 1970-01-01 00:00
     *              yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
     *              yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
     *       yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
     *       yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
     *     yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
     *     yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000
     * 
*/ public static final SimpleDateFormat DEFAULT_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /** * 各时间单位与毫秒的倍数 */ public static final int UNIT_MSEC = 1; public static final int UNIT_SEC = 1000; public static final int UNIT_MIN = 60000; public static final int UNIT_HOUR = 3600000; public static final int UNIT_DAY = 86400000; /** * 将时间戳转为时间字符串 *

格式为yyyy-MM-dd HH:mm:ss

* * @param milliseconds 毫秒时间戳 * @return 时间字符串 */ public static String milliseconds2String(long milliseconds) { return milliseconds2String(milliseconds, DEFAULT_SDF); } /** * 将时间戳转为时间字符串 *

格式为用户自定义

* * @param milliseconds 毫秒时间戳 * @param format 时间格式 * @return 时间字符串 */ public static String milliseconds2String(long milliseconds, SimpleDateFormat format) { return format.format(new Date(milliseconds)); } /** * 将时间字符串转为时间戳 *

格式为yyyy-MM-dd HH:mm:ss

* * @param time 时间字符串 * @return 毫秒时间戳 */ public static long string2Milliseconds(String time) { return string2Milliseconds(time, DEFAULT_SDF); } /** * 将时间字符串转为时间戳 *

格式为用户自定义

* * @param time 时间字符串 * @param format 时间格式 * @return 毫秒时间戳 */ public static long string2Milliseconds(String time, SimpleDateFormat format) { try { return format.parse(time).getTime(); } catch (ParseException e) { e.printStackTrace(); } return -1; } /** * 将时间字符串转为Date类型 *

格式为yyyy-MM-dd HH:mm:ss

* * @param time 时间字符串 * @return Date类型 */ public static Date string2Date(String time) { return string2Date(time, DEFAULT_SDF); } /** * 将时间字符串转为Date类型 *

格式为用户自定义

* * @param time 时间字符串 * @param format 时间格式 * @return Date类型 */ public static Date string2Date(String time, SimpleDateFormat format) { return new Date(string2Milliseconds(time, format)); } /** * 将Date类型转为时间字符串 *

格式为yyyy-MM-dd HH:mm:ss

* * @param time Date类型时间 * @return 时间字符串 */ public static String date2String(Date time) { return date2String(time, DEFAULT_SDF); } /** * 将Date类型转为时间字符串 *

格式为用户自定义

* * @param time Date类型时间 * @param format 时间格式 * @return 时间字符串 */ public static String date2String(Date time, SimpleDateFormat format) { return format.format(time); } /** * 将Date类型转为时间戳 * * @param time Date类型时间 * @return 毫秒时间戳 */ public static long date2Milliseconds(Date time) { return time.getTime(); } /** * 将时间戳转为Date类型 * * @param milliseconds 毫秒时间戳 * @return Date类型时间 */ public static Date milliseconds2Date(long milliseconds) { return new Date(milliseconds); } /** * 毫秒时间戳单位转换(单位:unit) * * @param milliseconds 毫秒时间戳 * @param unit * @return unit时间戳 */ private static long milliseconds2Unit(long milliseconds, int unit) { switch (unit) { case UNIT_MSEC: case UNIT_SEC: case UNIT_MIN: case UNIT_HOUR: case UNIT_DAY: return Math.abs(milliseconds) / unit; } return -1; } /** * 获取两个时间差(单位:unit) *

time1和time2格式都为yyyy-MM-dd HH:mm:ss

* * @param time1 时间字符串1 * @param time2 时间字符串2 * @param unit * @return unit时间戳 */ public static long getIntervalTime(String time1, String time2, int unit) { return getIntervalTime(time1, time2, unit, DEFAULT_SDF); } /** * 获取两个时间差(单位:unit) *

time1和time2格式都为format

* * @param time1 时间字符串1 * @param time2 时间字符串2 * @param unit * @param format 时间格式 * @return unit时间戳 */ public static long getIntervalTime(String time1, String time2, int unit, SimpleDateFormat format) { return milliseconds2Unit(string2Milliseconds(time1, format) - string2Milliseconds(time2, format), unit); } /** * 获取两个时间差(单位:unit) *

time1和time2都为Date类型

* * @param time1 Date类型时间1 * @param time2 Date类型时间2 * @param unit * @return unit时间戳 */ public static long getIntervalTime(Date time1, Date time2, int unit) { return milliseconds2Unit(date2Milliseconds(time2) - date2Milliseconds(time1), unit); } /** * 获取当前时间 * * @return 毫秒时间戳 */ public static long getCurTimeMills() { return System.currentTimeMillis(); } /** * 获取当前时间 *

格式为yyyy-MM-dd HH:mm:ss

* * @return 时间字符串 */ public static String getCurTimeString() { return milliseconds2String(getCurTimeMills()); } /** * 获取当前时间 *

格式为用户自定义

* * @param format 时间格式 * @return 时间字符串 */ public static String getCurTimeString(SimpleDateFormat format) { return milliseconds2String(getCurTimeMills(), format); } /** * 获取当前时间 *

Date类型

* * @return Date类型时间 */ public static Date getCurTimeDate() { return new Date(); } /** * 获取与当前时间的差(单位:unit) *

time格式为yyyy-MM-dd HH:mm:ss

* * @param time 时间字符串 * @param unit * @return unit时间戳 */ public static long getIntervalByNow(String time, int unit) { return getIntervalByNow(time, unit, DEFAULT_SDF); } /** * 获取与当前时间的差(单位:unit) *

time格式为format

* * @param time 时间字符串 * @param unit * @param format 时间格式 * @return unit时间戳 */ public static long getIntervalByNow(String time, int unit, SimpleDateFormat format) { return getIntervalTime(getCurTimeString(), time, unit, format); } /** * 获取与当前时间的差(单位:unit) *

time为Date类型

* * @param time Date类型时间 * @param unit * @return unit时间戳 */ public static long getIntervalByNow(Date time, int unit) { return getIntervalTime(getCurTimeDate(), time, unit); } /** * 判断闰年 * * @param year 年份 * @return true: 闰年
false: 平年 */ public static boolean isLeapYear(int year) { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } } ```