sdfMap = SDF_THREAD_LOCAL.get();
//noinspection ConstantConditions
SimpleDateFormat simpleDateFormat = sdfMap.get(pattern);
if (simpleDateFormat == null) {
simpleDateFormat = new SimpleDateFormat(pattern);
sdfMap.put(pattern, simpleDateFormat);
}
return simpleDateFormat;
}
private TimeUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Milliseconds to the formatted time string.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param millis The milliseconds.
* @return the formatted time string
*/
public static String millis2String(final long millis) {
return millis2String(millis, getDefaultFormat());
}
/**
* Milliseconds to the formatted time string.
*
* @param millis The milliseconds.
* @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
* @return the formatted time string
*/
public static String millis2String(long millis, @NonNull final String pattern) {
return millis2String(millis, getSafeDateFormat(pattern));
}
/**
* Milliseconds to the formatted time string.
*
* @param millis The milliseconds.
* @param format The format.
* @return the formatted time string
*/
public static String millis2String(final long millis, @NonNull final DateFormat format) {
return format.format(new Date(millis));
}
/**
* Formatted time string to the milliseconds.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @return the milliseconds
*/
public static long string2Millis(final String time) {
return string2Millis(time, getDefaultFormat());
}
/**
* Formatted time string to the milliseconds.
*
* @param time The formatted time string.
* @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
* @return the milliseconds
*/
public static long string2Millis(final String time, @NonNull final String pattern) {
return string2Millis(time, getSafeDateFormat(pattern));
}
/**
* Formatted time string to the milliseconds.
*
* @param time The formatted time string.
* @param format The format.
* @return the milliseconds
*/
public static long string2Millis(final String time, @NonNull final DateFormat format) {
try {
return format.parse(time).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
/**
* Formatted time string to the date.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @return the date
*/
public static Date string2Date(final String time) {
return string2Date(time, getDefaultFormat());
}
/**
* Formatted time string to the date.
*
* @param time The formatted time string.
* @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
* @return the date
*/
public static Date string2Date(final String time, @NonNull final String pattern) {
return string2Date(time, getSafeDateFormat(pattern));
}
/**
* Formatted time string to the date.
*
* @param time The formatted time string.
* @param format The format.
* @return the date
*/
public static Date string2Date(final String time, @NonNull final DateFormat format) {
try {
return format.parse(time);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Date to the formatted time string.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param date The date.
* @return the formatted time string
*/
public static String date2String(final Date date) {
return date2String(date, getDefaultFormat());
}
/**
* Date to the formatted time string.
*
* @param date The date.
* @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
* @return the formatted time string
*/
public static String date2String(final Date date, @NonNull final String pattern) {
return getSafeDateFormat(pattern).format(date);
}
/**
* Date to the formatted time string.
*
* @param date The date.
* @param format The format.
* @return the formatted time string
*/
public static String date2String(final Date date, @NonNull final DateFormat format) {
try {
return format.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* Date to the milliseconds.
*
* @param date The date.
* @return the milliseconds
*/
public static long date2Millis(final Date date) {
return date.getTime();
}
/**
* Milliseconds to the date.
*
* @param millis The milliseconds.
* @return the date
*/
public static Date millis2Date(final long millis) {
return new Date(millis);
}
/**
* Return the time span, in unit.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time1 The first formatted time string.
* @param time2 The second formatted time string.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the time span, in unit
*/
public static long getTimeSpan(final String time1,
final String time2,
@TimeConstants.Unit final int unit) {
return getTimeSpan(time1, time2, getDefaultFormat(), unit);
}
/**
* Return the time span, in unit.
*
* @param time1 The first formatted time string.
* @param time2 The second formatted time string.
* @param format The format.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the time span, in unit
*/
public static long getTimeSpan(final String time1,
final String time2,
@NonNull final DateFormat format,
@TimeConstants.Unit final int unit) {
return millis2TimeSpan(string2Millis(time1, format) - string2Millis(time2, format), unit);
}
/**
* Return the time span, in unit.
*
* @param date1 The first date.
* @param date2 The second date.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the time span, in unit
*/
public static long getTimeSpan(final Date date1,
final Date date2,
@TimeConstants.Unit final int unit) {
return millis2TimeSpan(date2Millis(date1) - date2Millis(date2), unit);
}
/**
* Return the time span, in unit.
*
* @param millis1 The first milliseconds.
* @param millis2 The second milliseconds.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the time span, in unit
*/
public static long getTimeSpan(final long millis1,
final long millis2,
@TimeConstants.Unit final int unit) {
return millis2TimeSpan(millis1 - millis2, unit);
}
/**
* Return the fit time span.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time1 The first formatted time string.
* @param time2 The second formatted time string.
* @param precision The precision of time span.
*
* - precision = 0, return null
* - precision = 1, return 天
* - precision = 2, return 天, 小时
* - precision = 3, return 天, 小时, 分钟
* - precision = 4, return 天, 小时, 分钟, 秒
* - precision >= 5,return 天, 小时, 分钟, 秒, 毫秒
*
* @return the fit time span
*/
public static String getFitTimeSpan(final String time1,
final String time2,
final int precision) {
long delta = string2Millis(time1, getDefaultFormat()) - string2Millis(time2, getDefaultFormat());
return millis2FitTimeSpan(delta, precision);
}
/**
* Return the fit time span.
*
* @param time1 The first formatted time string.
* @param time2 The second formatted time string.
* @param format The format.
* @param precision The precision of time span.
*
* - precision = 0, return null
* - precision = 1, return 天
* - precision = 2, return 天, 小时
* - precision = 3, return 天, 小时, 分钟
* - precision = 4, return 天, 小时, 分钟, 秒
* - precision >= 5,return 天, 小时, 分钟, 秒, 毫秒
*
* @return the fit time span
*/
public static String getFitTimeSpan(final String time1,
final String time2,
@NonNull final DateFormat format,
final int precision) {
long delta = string2Millis(time1, format) - string2Millis(time2, format);
return millis2FitTimeSpan(delta, precision);
}
/**
* Return the fit time span.
*
* @param date1 The first date.
* @param date2 The second date.
* @param precision The precision of time span.
*
* - precision = 0, return null
* - precision = 1, return 天
* - precision = 2, return 天, 小时
* - precision = 3, return 天, 小时, 分钟
* - precision = 4, return 天, 小时, 分钟, 秒
* - precision >= 5,return 天, 小时, 分钟, 秒, 毫秒
*
* @return the fit time span
*/
public static String getFitTimeSpan(final Date date1, final Date date2, final int precision) {
return millis2FitTimeSpan(date2Millis(date1) - date2Millis(date2), precision);
}
/**
* Return the fit time span.
*
* @param millis1 The first milliseconds.
* @param millis2 The second milliseconds.
* @param precision The precision of time span.
*
* - precision = 0, return null
* - precision = 1, return 天
* - precision = 2, return 天, 小时
* - precision = 3, return 天, 小时, 分钟
* - precision = 4, return 天, 小时, 分钟, 秒
* - precision >= 5,return 天, 小时, 分钟, 秒, 毫秒
*
* @return the fit time span
*/
public static String getFitTimeSpan(final long millis1,
final long millis2,
final int precision) {
return millis2FitTimeSpan(millis1 - millis2, precision);
}
/**
* Return the current time in milliseconds.
*
* @return the current time in milliseconds
*/
public static long getNowMills() {
return System.currentTimeMillis();
}
/**
* Return the current formatted time string.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @return the current formatted time string
*/
public static String getNowString() {
return millis2String(System.currentTimeMillis(), getDefaultFormat());
}
/**
* Return the current formatted time string.
*
* @param format The format.
* @return the current formatted time string
*/
public static String getNowString(@NonNull final DateFormat format) {
return millis2String(System.currentTimeMillis(), format);
}
/**
* Return the current date.
*
* @return the current date
*/
public static Date getNowDate() {
return new Date();
}
/**
* Return the time span by now, in unit.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the time span by now, in unit
*/
public static long getTimeSpanByNow(final String time, @TimeConstants.Unit final int unit) {
return getTimeSpan(time, getNowString(), getDefaultFormat(), unit);
}
/**
* Return the time span by now, in unit.
*
* @param time The formatted time string.
* @param format The format.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the time span by now, in unit
*/
public static long getTimeSpanByNow(final String time,
@NonNull final DateFormat format,
@TimeConstants.Unit final int unit) {
return getTimeSpan(time, getNowString(format), format, unit);
}
/**
* Return the time span by now, in unit.
*
* @param date The date.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the time span by now, in unit
*/
public static long getTimeSpanByNow(final Date date, @TimeConstants.Unit final int unit) {
return getTimeSpan(date, new Date(), unit);
}
/**
* Return the time span by now, in unit.
*
* @param millis The milliseconds.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the time span by now, in unit
*/
public static long getTimeSpanByNow(final long millis, @TimeConstants.Unit final int unit) {
return getTimeSpan(millis, System.currentTimeMillis(), unit);
}
/**
* Return the fit time span by now.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @param precision The precision of time span.
*
* - precision = 0,返回 null
* - precision = 1,返回天
* - precision = 2,返回天和小时
* - precision = 3,返回天、小时和分钟
* - precision = 4,返回天、小时、分钟和秒
* - precision >= 5,返回天、小时、分钟、秒和毫秒
*
* @return the fit time span by now
*/
public static String getFitTimeSpanByNow(final String time, final int precision) {
return getFitTimeSpan(time, getNowString(), getDefaultFormat(), precision);
}
/**
* Return the fit time span by now.
*
* @param time The formatted time string.
* @param format The format.
* @param precision The precision of time span.
*
* - precision = 0,返回 null
* - precision = 1,返回天
* - precision = 2,返回天和小时
* - precision = 3,返回天、小时和分钟
* - precision = 4,返回天、小时、分钟和秒
* - precision >= 5,返回天、小时、分钟、秒和毫秒
*
* @return the fit time span by now
*/
public static String getFitTimeSpanByNow(final String time,
@NonNull final DateFormat format,
final int precision) {
return getFitTimeSpan(time, getNowString(format), format, precision);
}
/**
* Return the fit time span by now.
*
* @param date The date.
* @param precision The precision of time span.
*
* - precision = 0,返回 null
* - precision = 1,返回天
* - precision = 2,返回天和小时
* - precision = 3,返回天、小时和分钟
* - precision = 4,返回天、小时、分钟和秒
* - precision >= 5,返回天、小时、分钟、秒和毫秒
*
* @return the fit time span by now
*/
public static String getFitTimeSpanByNow(final Date date, final int precision) {
return getFitTimeSpan(date, getNowDate(), precision);
}
/**
* Return the fit time span by now.
*
* @param millis The milliseconds.
* @param precision The precision of time span.
*
* - precision = 0,返回 null
* - precision = 1,返回天
* - precision = 2,返回天和小时
* - precision = 3,返回天、小时和分钟
* - precision = 4,返回天、小时、分钟和秒
* - precision >= 5,返回天、小时、分钟、秒和毫秒
*
* @return the fit time span by now
*/
public static String getFitTimeSpanByNow(final long millis, final int precision) {
return getFitTimeSpan(millis, System.currentTimeMillis(), precision);
}
/**
* Return the friendly time span by now.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @return the friendly time span by now
*
* - 如果小于 1 秒钟内,显示刚刚
* - 如果在 1 分钟内,显示 XXX秒前
* - 如果在 1 小时内,显示 XXX分钟前
* - 如果在 1 小时外的今天内,显示今天15:32
* - 如果是昨天的,显示昨天15:32
* - 其余显示,2016-10-15
* - 时间不合法的情况全部日期和时间信息,如星期六 十月 27 14:21:20 CST 2007
*
*/
public static String getFriendlyTimeSpanByNow(final String time) {
return getFriendlyTimeSpanByNow(time, getDefaultFormat());
}
/**
* Return the friendly time span by now.
*
* @param time The formatted time string.
* @param format The format.
* @return the friendly time span by now
*
* - 如果小于 1 秒钟内,显示刚刚
* - 如果在 1 分钟内,显示 XXX秒前
* - 如果在 1 小时内,显示 XXX分钟前
* - 如果在 1 小时外的今天内,显示今天15:32
* - 如果是昨天的,显示昨天15:32
* - 其余显示,2016-10-15
* - 时间不合法的情况全部日期和时间信息,如星期六 十月 27 14:21:20 CST 2007
*
*/
public static String getFriendlyTimeSpanByNow(final String time,
@NonNull final DateFormat format) {
return getFriendlyTimeSpanByNow(string2Millis(time, format));
}
/**
* Return the friendly time span by now.
*
* @param date The date.
* @return the friendly time span by now
*
* - 如果小于 1 秒钟内,显示刚刚
* - 如果在 1 分钟内,显示 XXX秒前
* - 如果在 1 小时内,显示 XXX分钟前
* - 如果在 1 小时外的今天内,显示今天15:32
* - 如果是昨天的,显示昨天15:32
* - 其余显示,2016-10-15
* - 时间不合法的情况全部日期和时间信息,如星期六 十月 27 14:21:20 CST 2007
*
*/
public static String getFriendlyTimeSpanByNow(final Date date) {
return getFriendlyTimeSpanByNow(date.getTime());
}
/**
* Return the friendly time span by now.
*
* @param millis The milliseconds.
* @return the friendly time span by now
*
* - 如果小于 1 秒钟内,显示刚刚
* - 如果在 1 分钟内,显示 XXX秒前
* - 如果在 1 小时内,显示 XXX分钟前
* - 如果在 1 小时外的今天内,显示今天15:32
* - 如果是昨天的,显示昨天15:32
* - 其余显示,2016-10-15
* - 时间不合法的情况全部日期和时间信息,如星期六 十月 27 14:21:20 CST 2007
*
*/
public static String getFriendlyTimeSpanByNow(final long millis) {
long now = System.currentTimeMillis();
long span = now - millis;
if (span < 0)
// U can read http://www.apihome.cn/api/java/Formatter.html to understand it.
return String.format("%tc", millis);
if (span < 1000) {
return "刚刚";
} else if (span < TimeConstants.MIN) {
return String.format(Locale.getDefault(), "%d秒前", span / TimeConstants.SEC);
} else if (span < TimeConstants.HOUR) {
return String.format(Locale.getDefault(), "%d分钟前", span / TimeConstants.MIN);
}
// 获取当天 00:00
long wee = getWeeOfToday();
if (millis >= wee) {
return String.format("今天%tR", millis);
} else if (millis >= wee - TimeConstants.DAY) {
return String.format("昨天%tR", millis);
} else {
return String.format("%tF", millis);
}
}
private static long getWeeOfToday() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTimeInMillis();
}
/**
* Return the milliseconds differ time span.
*
* @param millis The milliseconds.
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the milliseconds differ time span
*/
public static long getMillis(final long millis,
final long timeSpan,
@TimeConstants.Unit final int unit) {
return millis + timeSpan2Millis(timeSpan, unit);
}
/**
* Return the milliseconds differ time span.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the milliseconds differ time span
*/
public static long getMillis(final String time,
final long timeSpan,
@TimeConstants.Unit final int unit) {
return getMillis(time, getDefaultFormat(), timeSpan, unit);
}
/**
* Return the milliseconds differ time span.
*
* @param time The formatted time string.
* @param format The format.
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the milliseconds differ time span.
*/
public static long getMillis(final String time,
@NonNull final DateFormat format,
final long timeSpan,
@TimeConstants.Unit final int unit) {
return string2Millis(time, format) + timeSpan2Millis(timeSpan, unit);
}
/**
* Return the milliseconds differ time span.
*
* @param date The date.
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the milliseconds differ time span.
*/
public static long getMillis(final Date date,
final long timeSpan,
@TimeConstants.Unit final int unit) {
return date2Millis(date) + timeSpan2Millis(timeSpan, unit);
}
/**
* Return the formatted time string differ time span.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param millis The milliseconds.
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the formatted time string differ time span
*/
public static String getString(final long millis,
final long timeSpan,
@TimeConstants.Unit final int unit) {
return getString(millis, getDefaultFormat(), timeSpan, unit);
}
/**
* Return the formatted time string differ time span.
*
* @param millis The milliseconds.
* @param format The format.
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the formatted time string differ time span
*/
public static String getString(final long millis,
@NonNull final DateFormat format,
final long timeSpan,
@TimeConstants.Unit final int unit) {
return millis2String(millis + timeSpan2Millis(timeSpan, unit), format);
}
/**
* Return the formatted time string differ time span.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the formatted time string differ time span
*/
public static String getString(final String time,
final long timeSpan,
@TimeConstants.Unit final int unit) {
return getString(time, getDefaultFormat(), timeSpan, unit);
}
/**
* Return the formatted time string differ time span.
*
* @param time The formatted time string.
* @param format The format.
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the formatted time string differ time span
*/
public static String getString(final String time,
@NonNull final DateFormat format,
final long timeSpan,
@TimeConstants.Unit final int unit) {
return millis2String(string2Millis(time, format) + timeSpan2Millis(timeSpan, unit), format);
}
/**
* Return the formatted time string differ time span.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param date The date.
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the formatted time string differ time span
*/
public static String getString(final Date date,
final long timeSpan,
@TimeConstants.Unit final int unit) {
return getString(date, getDefaultFormat(), timeSpan, unit);
}
/**
* Return the formatted time string differ time span.
*
* @param date The date.
* @param format The format.
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the formatted time string differ time span
*/
public static String getString(final Date date,
@NonNull final DateFormat format,
final long timeSpan,
@TimeConstants.Unit final int unit) {
return millis2String(date2Millis(date) + timeSpan2Millis(timeSpan, unit), format);
}
/**
* Return the date differ time span.
*
* @param millis The milliseconds.
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the date differ time span
*/
public static Date getDate(final long millis,
final long timeSpan,
@TimeConstants.Unit final int unit) {
return millis2Date(millis + timeSpan2Millis(timeSpan, unit));
}
/**
* Return the date differ time span.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the date differ time span
*/
public static Date getDate(final String time,
final long timeSpan,
@TimeConstants.Unit final int unit) {
return getDate(time, getDefaultFormat(), timeSpan, unit);
}
public static Date getDate(String time) {
try {
Date parse = getDefaultFormat().parse(time);
return parse;
} catch (Exception e) {
e.printStackTrace();
}
return getNowDate();
}
/**
* Return the date differ time span.
*
* @param time The formatted time string.
* @param format The format.
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the date differ time span
*/
public static Date getDate(final String time,
@NonNull final DateFormat format,
final long timeSpan,
@TimeConstants.Unit final int unit) {
return millis2Date(string2Millis(time, format) + timeSpan2Millis(timeSpan, unit));
}
/**
* Return the date differ time span.
*
* @param date The date.
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the date differ time span
*/
public static Date getDate(final Date date,
final long timeSpan,
@TimeConstants.Unit final int unit) {
return millis2Date(date2Millis(date) + timeSpan2Millis(timeSpan, unit));
}
/**
* Return the milliseconds differ time span by now.
*
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the milliseconds differ time span by now
*/
public static long getMillisByNow(final long timeSpan, @TimeConstants.Unit final int unit) {
return getMillis(getNowMills(), timeSpan, unit);
}
/**
* Return the formatted time string differ time span by now.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the formatted time string differ time span by now
*/
public static String getStringByNow(final long timeSpan, @TimeConstants.Unit final int unit) {
return getStringByNow(timeSpan, getDefaultFormat(), unit);
}
/**
* Return the formatted time string differ time span by now.
*
* @param timeSpan The time span.
* @param format The format.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the formatted time string differ time span by now
*/
public static String getStringByNow(final long timeSpan,
@NonNull final DateFormat format,
@TimeConstants.Unit final int unit) {
return getString(getNowMills(), format, timeSpan, unit);
}
/**
* Return the date differ time span by now.
*
* @param timeSpan The time span.
* @param unit The unit of time span.
*
* - {@link TimeConstants#MSEC}
* - {@link TimeConstants#SEC }
* - {@link TimeConstants#MIN }
* - {@link TimeConstants#HOUR}
* - {@link TimeConstants#DAY }
*
* @return the date differ time span by now
*/
public static Date getDateByNow(final long timeSpan, @TimeConstants.Unit final int unit) {
return getDate(getNowMills(), timeSpan, unit);
}
/**
* Return whether it is today.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isToday(final String time) {
return isToday(string2Millis(time, getDefaultFormat()));
}
/**
* Return whether it is today.
*
* @param time The formatted time string.
* @param format The format.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isToday(final String time, @NonNull final DateFormat format) {
return isToday(string2Millis(time, format));
}
/**
* Return whether it is today.
*
* @param date The date.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isToday(final Date date) {
return isToday(date.getTime());
}
/**
* Return whether it is today.
*
* @param millis The milliseconds.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isToday(final long millis) {
long wee = getWeeOfToday();
return millis >= wee && millis < wee + TimeConstants.DAY;
}
/**
* Return whether it is leap year.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isLeapYear(final String time) {
return isLeapYear(string2Date(time, getDefaultFormat()));
}
/**
* Return whether it is leap year.
*
* @param time The formatted time string.
* @param format The format.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isLeapYear(final String time, @NonNull final DateFormat format) {
return isLeapYear(string2Date(time, format));
}
/**
* Return whether it is leap year.
*
* @param date The date.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isLeapYear(final Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
return isLeapYear(year);
}
/**
* Return whether it is leap year.
*
* @param millis The milliseconds.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isLeapYear(final long millis) {
return isLeapYear(millis2Date(millis));
}
/**
* Return whether it is leap year.
*
* @param year The year.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isLeapYear(final int year) {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
/**
* Return the day of week in Chinese.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @return the day of week in Chinese
*/
public static String getChineseWeek(final String time) {
return getChineseWeek(string2Date(time, getDefaultFormat()));
}
/**
* Return the day of week in Chinese.
*
* @param time The formatted time string.
* @param format The format.
* @return the day of week in Chinese
*/
public static String getChineseWeek(final String time, @NonNull final DateFormat format) {
return getChineseWeek(string2Date(time, format));
}
/**
* Return the day of week in Chinese.
*
* @param date The date.
* @return the day of week in Chinese
*/
public static String getChineseWeek(final Date date) {
return new SimpleDateFormat("E", Locale.CHINA).format(date);
}
/**
* Return the day of week in Chinese.
*
* @param millis The milliseconds.
* @return the day of week in Chinese
*/
public static String getChineseWeek(final long millis) {
return getChineseWeek(new Date(millis));
}
/**
* Return the day of week in US.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @return the day of week in US
*/
public static String getUSWeek(final String time) {
return getUSWeek(string2Date(time, getDefaultFormat()));
}
/**
* Return the day of week in US.
*
* @param time The formatted time string.
* @param format The format.
* @return the day of week in US
*/
public static String getUSWeek(final String time, @NonNull final DateFormat format) {
return getUSWeek(string2Date(time, format));
}
/**
* Return the day of week in US.
*
* @param date The date.
* @return the day of week in US
*/
public static String getUSWeek(final Date date) {
return new SimpleDateFormat("EEEE", Locale.US).format(date);
}
/**
* Return the day of week in US.
*
* @param millis The milliseconds.
* @return the day of week in US
*/
public static String getUSWeek(final long millis) {
return getUSWeek(new Date(millis));
}
/**
* Return whether it is am.
*
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isAm() {
Calendar cal = Calendar.getInstance();
return cal.get(GregorianCalendar.AM_PM) == 0;
}
/**
* Return whether it is am.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isAm(final String time) {
return getValueByCalendarField(time, getDefaultFormat(), GregorianCalendar.AM_PM) == 0;
}
/**
* Return whether it is am.
*
* @param time The formatted time string.
* @param format The format.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isAm(final String time,
@NonNull final DateFormat format) {
return getValueByCalendarField(time, format, GregorianCalendar.AM_PM) == 0;
}
/**
* Return whether it is am.
*
* @param date The date.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isAm(final Date date) {
return getValueByCalendarField(date, GregorianCalendar.AM_PM) == 0;
}
/**
* Return whether it is am.
*
* @param millis The milliseconds.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isAm(final long millis) {
return getValueByCalendarField(millis, GregorianCalendar.AM_PM) == 0;
}
/**
* Return whether it is am.
*
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isPm() {
return !isAm();
}
/**
* Return whether it is am.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isPm(final String time) {
return !isAm(time);
}
/**
* Return whether it is am.
*
* @param time The formatted time string.
* @param format The format.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isPm(final String time,
@NonNull final DateFormat format) {
return !isAm(time, format);
}
/**
* Return whether it is am.
*
* @param date The date.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isPm(final Date date) {
return !isAm(date);
}
/**
* Return whether it is am.
*
* @param millis The milliseconds.
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isPm(final long millis) {
return !isAm(millis);
}
/**
* Returns the value of the given calendar field.
*
* @param field The given calendar field.
*
* - {@link Calendar#ERA}
* - {@link Calendar#YEAR}
* - {@link Calendar#MONTH}
* - ...
* - {@link Calendar#DST_OFFSET}
*
* @return the value of the given calendar field
*/
public static int getValueByCalendarField(final int field) {
Calendar cal = Calendar.getInstance();
return cal.get(field);
}
/**
* Returns the value of the given calendar field.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @param field The given calendar field.
*
* - {@link Calendar#ERA}
* - {@link Calendar#YEAR}
* - {@link Calendar#MONTH}
* - ...
* - {@link Calendar#DST_OFFSET}
*
* @return the value of the given calendar field
*/
public static int getValueByCalendarField(final String time, final int field) {
return getValueByCalendarField(string2Date(time, getDefaultFormat()), field);
}
/**
* Returns the value of the given calendar field.
*
* @param time The formatted time string.
* @param format The format.
* @param field The given calendar field.
*
* - {@link Calendar#ERA}
* - {@link Calendar#YEAR}
* - {@link Calendar#MONTH}
* - ...
* - {@link Calendar#DST_OFFSET}
*
* @return the value of the given calendar field
*/
public static int getValueByCalendarField(final String time,
@NonNull final DateFormat format,
final int field) {
return getValueByCalendarField(string2Date(time, format), field);
}
/**
* Returns the value of the given calendar field.
*
* @param date The date.
* @param field The given calendar field.
*
* - {@link Calendar#ERA}
* - {@link Calendar#YEAR}
* - {@link Calendar#MONTH}
* - ...
* - {@link Calendar#DST_OFFSET}
*
* @return the value of the given calendar field
*/
public static int getValueByCalendarField(final Date date, final int field) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(field);
}
/**
* Returns the value of the given calendar field.
*
* @param millis The milliseconds.
* @param field The given calendar field.
*
* - {@link Calendar#ERA}
* - {@link Calendar#YEAR}
* - {@link Calendar#MONTH}
* - ...
* - {@link Calendar#DST_OFFSET}
*
* @return the value of the given calendar field
*/
public static int getValueByCalendarField(final long millis, final int field) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
return cal.get(field);
}
private static final String[] CHINESE_ZODIAC =
{"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};
/**
* Return the Chinese zodiac.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @return the Chinese zodiac
*/
public static String getChineseZodiac(final String time) {
return getChineseZodiac(string2Date(time, getDefaultFormat()));
}
/**
* Return the Chinese zodiac.
*
* @param time The formatted time string.
* @param format The format.
* @return the Chinese zodiac
*/
public static String getChineseZodiac(final String time, @NonNull final DateFormat format) {
return getChineseZodiac(string2Date(time, format));
}
/**
* Return the Chinese zodiac.
*
* @param date The date.
* @return the Chinese zodiac
*/
public static String getChineseZodiac(final Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return CHINESE_ZODIAC[cal.get(Calendar.YEAR) % 12];
}
/**
* Return the Chinese zodiac.
*
* @param millis The milliseconds.
* @return the Chinese zodiac
*/
public static String getChineseZodiac(final long millis) {
return getChineseZodiac(millis2Date(millis));
}
/**
* Return the Chinese zodiac.
*
* @param year The year.
* @return the Chinese zodiac
*/
public static String getChineseZodiac(final int year) {
return CHINESE_ZODIAC[year % 12];
}
private static final int[] ZODIAC_FLAGS = {20, 19, 21, 21, 21, 22, 23, 23, 23, 24, 23, 22};
private static final String[] ZODIAC = {
"水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座",
"狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"
};
/**
* Return the zodiac.
* The pattern is {@code yyyy-MM-dd HH:mm:ss}.
*
* @param time The formatted time string.
* @return the zodiac
*/
public static String getZodiac(final String time) {
return getZodiac(string2Date(time, getDefaultFormat()));
}
/**
* Return the zodiac.
*
* @param time The formatted time string.
* @param format The format.
* @return the zodiac
*/
public static String getZodiac(final String time, @NonNull final DateFormat format) {
return getZodiac(string2Date(time, format));
}
/**
* Return the zodiac.
*
* @param date The date.
* @return the zodiac
*/
public static String getZodiac(final Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
return getZodiac(month, day);
}
/**
* Return the zodiac.
*
* @param millis The milliseconds.
* @return the zodiac
*/
public static String getZodiac(final long millis) {
return getZodiac(millis2Date(millis));
}
/**
* Return the zodiac.
*
* @param month The month.
* @param day The day.
* @return the zodiac
*/
public static String getZodiac(final int month, final int day) {
return ZODIAC[day >= ZODIAC_FLAGS[month - 1]
? month - 1
: (month + 10) % 12];
}
private static long timeSpan2Millis(final long timeSpan, @TimeConstants.Unit final int unit) {
return timeSpan * unit;
}
private static long millis2TimeSpan(final long millis, @TimeConstants.Unit final int unit) {
return millis / unit;
}
static String millis2FitTimeSpan(long millis, int precision) {
if (precision <= 0) return null;
precision = Math.min(precision, 5);
String[] units = {"天", "小时", "分钟", "秒", "毫秒"};
if (millis == 0) return 0 + units[precision - 1];
StringBuilder sb = new StringBuilder();
if (millis < 0) {
sb.append("-");
millis = -millis;
}
int[] unitLen = {86400000, 3600000, 60000, 1000, 1};
for (int i = 0; i < precision; i++) {
if (millis >= unitLen[i]) {
long mode = millis / unitLen[i];
millis -= mode * unitLen[i];
sb.append(mode).append(units[i]);
}
}
return sb.toString();
}
public static Date stringToDate(String strTime)
throws ParseException {
SimpleDateFormat formatter = getSafeDateFormat("yyyy-MM-dd");
Date date = null;
date = formatter.parse(strTime);
return date;
}
public static long[] getHmsFromSecond(long timeSecond) {
long[] result = new long[3];
try{
result[0] = timeSecond / 3600;
timeSecond = timeSecond % 3600;
long minute = timeSecond / 60;//得到分
result[1] = minute;
result[2] = timeSecond % 60;
}catch (Exception e){
e.printStackTrace();
}
return result;
}
public static String secondToTime(int second) {
// int hour = second / 3600; // 得到分钟数
// second = second % 3600;//剩余的秒数
int minute = second / 60;//得到分
second = second % 60;//剩余的秒
return String.format(Locale.getDefault(), "%02d:%02d", minute, second);
}
public static String msToTime(int ms) {
return secondToTime(ms / 1000);
}
public static String msToTime(long ms) {
return secondToTime(ms / 1000);
}
public static String secondToTime(long second) {
// int hour = second / 3600; // 得到分钟数
// second = second % 3600;//剩余的秒数
long minute = second / 60;//得到分
second = second % 60;//剩余的秒
return String.format(Locale.getDefault(), "%02d:%02d", minute, second);
}
public static boolean isLessThanTargetTime(String targetMinutes, String startTime) {
try {
long time = TimeUtils.string2Date(startTime).getTime();
long cTime = System.currentTimeMillis();
if (cTime >= time) {
return true;
}
long limitMinutes = Long.parseLong(targetMinutes);
if (Math.abs(time - cTime) < limitMinutes * 60 * 1000) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}