package com.roshi.utils.common;
import java.lang.reflect.Constructor;
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 java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;
public final class DateUtils {
public static final DateFormat YM = new SimpleDateFormat("yyyyMM");
public static final DateFormat YMD = new SimpleDateFormat("yyyyMMdd");
public static final DateFormat YMD_DASH = new SimpleDateFormat("yyyy-MM-dd");
public static final DateFormat YMD_DOT = new SimpleDateFormat("yyyy.MM.dd");
public static final DateFormat YMD_DASH_HM = new SimpleDateFormat("yyyy-MM-dd HH:mm");
public static final DateFormat FULL_TIME = new SimpleDateFormat("yyyyMMddHHmmss");
public static final DateFormat FULL_TIME_DASH = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final DateFormat FULL_TIME_SLASH = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
public static final DateFormat TIMESTAMP = new SimpleDateFormat("yyyyMMddHHmmssSSS");
public static final DateFormat TIMESTAMP_DASH = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
public static final String[] INTERVAL_CN = {
"年","个月","周","周","天","天","天","天","上午/下午","小时","小时","分","秒","毫秒"
};
private static final long[] MILLS = new long[]{
TimeUnit.DAYS.toMillis(1),
TimeUnit.DAYS.toMillis(1),
TimeUnit.DAYS.toMillis(1),
TimeUnit.DAYS.toMillis(1),
TimeUnit.HOURS.toMillis(12),
TimeUnit.HOURS.toMillis(1),
TimeUnit.HOURS.toMillis(1),
TimeUnit.MINUTES.toMillis(1),
TimeUnit.SECONDS.toMillis(1), 1};
/*
* 工具类,私有类构造方法,禁止使用new生成对象
*/
private DateUtils() {}
/**
* 获得系统日期(无时分秒)
*
* @return
*/
public static Date getSystemDate() {
return truncation(Calendar.getInstance().getTime());
}
/**
* 获得系统时间
*
* @return
*/
public static Date getSystemTime() {
return Calendar.getInstance().getTime();
}
/**
* 获得系统时间
*
* @return
*/
public static <T extends Date> T getSystemTime(Class<T> clz) {
Constructor<T> con;
try {
con = clz.getConstructor(long.class);
return con.newInstance(getSystemTime().getTime());
} catch (Exception e) {
return null;
}
}
/**
* 获得系统日期无格式的字符串(无时分秒)
*
* @return
*/
public static String getSystemDateNoFormat() {
return formatDateNone(getSystemDate());
}
/**
* 获得系统日期中国显示格式的字符串(无时分秒)
*
* @return
*/
public static String getSystemDateDash() {
return formatDateDash(getSystemDate());
}
/**
* 获得系统日期(yyyy.MM.dd)的字符串(无时分秒)
*
* @return
*/
public static String getSystemDateDot() {
return formatDateDot(getSystemDate());
}
/**
* 获得系统日期(yyyy-MM-dd HH:mm)的字符串(带小时和分钟)
*
* @return
*/
public static String getSystemDateDashWithHM() {
return formatDateDashWithHM(getSystemDate());
}
/**
* 获得系统时间戳中国显示格式的字符串
*
* @return
*/
public static String getSystemFullTimeDash() {
return formatFullTimeDash(getSystemTime());
}
/**
* 获得系统时间无格式的字符串
*
* @return
*/
public static String getSystemFullTimeNoFormat() {
return formatFullTimeNone(getSystemTime());
}
/**
* 获得系统时间戳无格式的字符串
*
* @return
*/
public static String getSystemTimestampNoFormat() {
return formatTimestampNone(getSystemTime());
}
/**
* 获得系统时间戳中国显示格式的字符串
*
* @return
*/
public static String getSystemTimestampDash() {
return formatTimestampDash(getSystemTime());
}
/**
* 将日期对象格式化为无分隔字符串(yyyyMMdd,如:20140103)
*
* @param d
* @return
*/
public static String formatDateNone(Date d) {
if (d == null) return StringUtils.EMPTY;
return YMD.format(d);
}
/**
* 将日期对象格式化为中国显示格式的字符串(yyyy-MM-dd,如:2014-01-03)
*
* @param d
* @return
*/
public static String formatDateDash(Date d) {
if (d == null) return StringUtils.EMPTY;
return YMD_DASH.format(d);
}
/**
* 将日期对象格式化为字符串(yyyy.MM.dd,如:2014.01.03)
*
* @param d
* @return
*/
public static String formatDateDot(Date d) {
if (d == null) return StringUtils.EMPTY;
return YMD_DOT.format(d);
}
/**
* 将日期对象格式化为字符串(yyyy-MM-dd HH:mm,如:2014.01.03)
*
* @param d
* @return
*/
public static String formatDateDashWithHM(Date d) {
if (d == null) return StringUtils.EMPTY;
return YMD_DASH_HM.format(d);
}
/**
* 将完整的时间对象格式化为无分隔字符串
* (yyyyMMddHHmmss,如:20140103132450为 2014年1月3日 13点24分50秒)
*
* @param d
* @return
*/
public static String formatFullTimeNone(Date d) {
if (d == null) return StringUtils.EMPTY;
return FULL_TIME.format(d);
}
/**
* 将完整的时间对象格式化为中国显示格式的字符串
* (yyyy-MM-dd HH:mm:ss,如:2014-01-03 20:14:30为 2014年1月3日 20点14分30秒)
*
* @param d
* @return
*/
public static String formatFullTimeDash(Date d) {
if (d == null) return StringUtils.EMPTY;
return FULL_TIME_DASH.format(d);
}
/**
* 将完整的时间对象格式化为字符串
* (yyyy/MM/dd HH:mm:ss,如:2014/01/03 20:14:30为 2014年1月3日 20点14分30秒)
*
* @param d
* @return
*/
public static String formatFullTimeSlash(Date d) {
if (d == null) return StringUtils.EMPTY;
return FULL_TIME_SLASH.format(d);
}
/**
* 将时间戳对象格式化为无分隔字符串
* (yyyyMMddHHmmssSSS,如:20140103132450834为 2014年1月3日 13点24分50秒834毫秒)
*
* @param d
* @return
*/
public static String formatTimestampNone(Date d) {
if (d == null) return StringUtils.EMPTY;
return TIMESTAMP.format(d);
}
/**
* 将时间戳对象格式化为中国显示格式的字符串
* (yyyy-MM-dd HH:mm:ss.SSS,如:2014-01-03 20:14:30.223为 2014年1月3日 20点14分30秒223毫秒)
*
* @param d
* @return
*/
public static String formatTimestampDash(Date d) {
if (d == null) return StringUtils.EMPTY;
return TIMESTAMP_DASH.format(d);
}
/**
* 将时间戳对象自定义的格式
*
* @param d
* @param pattern 自定义格式字符串
* @return
*/
public static String formatCustomize(Date d, String pattern) {
if (d == null) return StringUtils.EMPTY;
if (StringUtils.isEmpty(pattern)) {
return TIMESTAMP_DASH.format(d);
}
return new SimpleDateFormat(pattern).format(d);
}
/**
* 格式化现在时间到输入时间的差值
*
* @param day 指定日期
* @return
*/
public static String formatIntervalFrowNow(Date day, int scale, String prefix, String suffix) {
if (day == null) return StringUtils.EMPTY;
Calendar current = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.setTime(day);
if (!current.before(end)) return StringUtils.EMPTY;
StringBuilder sb = new StringBuilder();
if (prefix != null) sb.append(prefix).append(' ');
if (suffix == null) suffix = StringUtils.EMPTY;
int tf, df = scale >>> 4;
if (df == 0) {
df = scale;
tf = scale;
} else {
tf = scale & 15;
}
int d = end.get(Calendar.HOUR_OF_DAY) < current.get(Calendar.HOUR_OF_DAY) ? -1 : 0;
d += end.get(Calendar.DATE) - current.get(Calendar.DATE);
int m = d < 0 ? -1 : 0;
int y = end.get(Calendar.YEAR) - current.get(Calendar.YEAR);
m += end.get(Calendar.MONTH) - current.get(Calendar.MONTH);
if (m < 0) { y--; m+=12; }
if (d < 0) { d+=current.getMaximum(Calendar.DAY_OF_MONTH); }
if (df == Calendar.DAY_OF_YEAR) {
d = (int) ((end.getTimeInMillis() - current.getTimeInMillis()) / MILLS[0]);
if (d > 0) sb.append(d).append(INTERVAL_CN[Calendar.DAY_OF_YEAR - 1]);
} else if (df < Calendar.AM_PM) {
if (y > 0) sb.append(y).append(INTERVAL_CN[Calendar.YEAR - 1]);
if (df == Calendar.YEAR) return sb.append(suffix).toString();
if (m > 0) sb.append(m).append(INTERVAL_CN[Calendar.MONTH - 1]);
if (df == Calendar.MONTH) return sb.append(suffix).toString();
if (df == Calendar.DATE) {
if (d > 0) sb.append(d).append(INTERVAL_CN[Calendar.DAY_OF_MONTH - 1]);
} else {
int w = d / 7;
if (w > 0) sb.append(w).append(INTERVAL_CN[Calendar.WEEK_OF_MONTH - 1]);
if (df < Calendar.DATE) return sb.append(suffix).toString();
w = d % 7;
if (d > 0) sb.append(w).append(INTERVAL_CN[Calendar.DAY_OF_WEEK - 1]);
}
} else {
if (y > 0) sb.append(y).append(INTERVAL_CN[Calendar.YEAR - 1]);
if (m > 0) sb.append(m).append(INTERVAL_CN[Calendar.MONTH - 1]);
if (d > 0) sb.append(d).append(INTERVAL_CN[Calendar.DAY_OF_MONTH - 1]);
}
if (tf > 10) {
long mills = (end.getTimeInMillis() - current.getTimeInMillis()) % MILLS[0], val;
int max = tf - 4;
boolean hasBefore = false;
for (int i = Calendar.HOUR_OF_DAY - Calendar.DATE; i < max; i++) {
val = mills / MILLS[i];
if (hasBefore || val > 0) {
hasBefore = true;
sb.append(val).append(INTERVAL_CN[i + 4]);
}
mills = mills % MILLS[i];
}
}
return sb.append(suffix).toString();
}
/**
* 将输入的年月字符串转换为日期 {@link Date}类型
* 默认字符串日期格式为(yyyyMM)
*
* @param date
* @return
*/
public static Date getYearMonth(String ym) {
if (StringUtils.isEmpty(ym)) {
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, 1);
return c.getTime();
}
try {
return YM.parse(ym);
} catch (ParseException e) {
// 日期格式错误时返回空对象
return null;
}
}
/**
* 将输入的字符串转换为日期 {@link Date}类型
* 默认字符串日期格式为(yyyyMMdd)
*
* @param date
* @return
*/
public static Date getDate(String date) {
if (StringUtils.isEmpty(date)) return getSystemDate();
try {
return YMD.parse(date);
} catch (ParseException e) {
// 日期格式错误时返回空对象
return null;
}
}
/**
* 将输入的字符串转换为日期 {@link Date}类型
* 默认字符串日期格式为(yyyy-MM-dd)
*
* @param date
* @return
*/
public static Date getDateDash(String date) {
if (StringUtils.isEmpty(date)) return getSystemDate();
try {
return YMD_DASH.parse(date);
} catch (ParseException e) {
// 日期格式错误时返回空对象
return null;
}
}
/**
* 将输入的字符串转换为日期 {@link Date}类型
* 默认字符串日期格式为(yyyy.MM.dd)
*
* @param date
* @return
*/
public static Date getDateDot(String date) {
if (StringUtils.isEmpty(date)) return getSystemDate();
try {
return YMD_DOT.parse(date);
} catch (ParseException e) {
// 日期格式错误时返回空对象
return null;
}
}
/**
* 将输入的字符串转换为时间 {@link Date}类型
* 默认字符串日期格式为(yyyy-MM-dd HH:mm:ss)
*
* @param date
* @return
*/
public static Date getDateFullDash(String date) {
if (StringUtils.isEmpty(date)) return getSystemTime();
try {
return FULL_TIME_DASH.parse(date);
} catch (ParseException e) {
// 日期格式错误时返回空对象
return null;
}
}
/**
* 将输入的字符串转换为时间 {@link Date}类型
* 默认字符串日期格式为(yyyy/MM/dd HH:mm:ss)
*
* @param date
* @return
*/
public static Date getDateFullSlash(String date) {
if (StringUtils.isEmpty(date)) return getSystemTime();
try {
return FULL_TIME_SLASH.parse(date);
} catch (ParseException e) {
// 日期格式错误时返回空对象
return null;
}
}
/**
* 将输入的字符串转换为时间 {@link Date}类型
* 默认字符串日期格式为(yyyy-MM-dd HH:mm:ss)
*
* @param date
* @return
*/
public static <T extends Date> T getDateFullDash(String date, Class<T> clz) {
if (StringUtils.isEmpty(date)) return null;
try {
Constructor<T> con = clz.getConstructor(long.class);
return con.newInstance(FULL_TIME_DASH.parse(date).getTime());
} catch (Exception e) {
// 日期格式错误时返回空对象
return null;
}
}
/**
* 将输入的字符串转换为时间戳 {@link Date}类型
* 默认字符串日期格式为(yyyy-MM-dd HH:mm:ss.SSS)
*
* @param date
* @return
*/
public static Timestamp getDateTimestampDash(String date) {
if (StringUtils.isEmpty(date)) return getSystemTime(Timestamp.class);
try {
return new Timestamp(TIMESTAMP_DASH.parse(date).getTime());
} catch (ParseException e) {
// 日期格式错误时返回空对象
return null;
}
}
/**
* 将输入的字符串转换为日期 {@link Date}类型
*
* @param date
* @param format
* @return
*/
public static Date getDate(String date, String format) {
if (StringUtils.isEmpty(date)) return getSystemDate();
try {
return new SimpleDateFormat(format).parse(date);
} catch (ParseException e) {
// 日期格式错误时返回空对象
return null;
}
}
/**
* 获得输入天的结束时间
*
* @param day 指定日期
* @return
*/
public static long calcDayEndInterval(Date day, TimeUnit unit) {
long start, time;
if (day == null) {
start = System.currentTimeMillis();
time = truncation(start, Calendar.DATE);
} else {
start = day.getTime();
time = truncation(day.getTime(), Calendar.DATE);
}
if (unit == TimeUnit.MILLISECONDS) {
return time + MILLS[0] - 1 - start;
} else {
return unit.convert(time + MILLS[0] - 1 - start, TimeUnit.MILLISECONDS);
}
}
/**
* 获得输入天的结束时间
*
* @param day 指定日期
* @return
*/
public static long getDayEndMillis(Date day) {
long time;
if (day == null) {
time = truncation(System.currentTimeMillis(), Calendar.DATE);
} else {
time = truncation(day.getTime(), Calendar.DATE);
}
return time + MILLS[0] - 1;
}
/**
* 获得输入天的结束时间
*
* @param day 指定日期
* @return
*/
public static Date getDayEndTime(Date day) {
long time;
if (day == null) {
time = truncation(System.currentTimeMillis(), Calendar.DATE);
} else {
time = truncation(day.getTime(), Calendar.DATE);
}
time = time + MILLS[0] - 1000;
return new Date(time);
}
/**
* 获得输入天的结束时间
*
* @param day 指定日期
* @return
*/
public static long getEndMillis(Date day, int scale, int add) {
long time;
if (day == null) {
time = truncation(System.currentTimeMillis(), scale);
} else {
time = truncation(day.getTime(), scale);
}
long level = 0;
if (Calendar.MILLISECOND <= scale) {
return time;
} else if (Calendar.DATE <= scale) {
level = MILLS[scale - Calendar.DATE];
} else {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
c.add(scale, add + 1);
c.add(Calendar.SECOND, -1);
return c.getTimeInMillis();
}
return time + (add + 1) * level - 1l;
}
/**
* 获得输入天的结束时间
*
* @param day 指定日期
* @return
*/
public static Date getEndTime(Date day, int scale, int add) {
long time;
if (day == null) {
time = truncation(System.currentTimeMillis(), scale);
} else {
time = truncation(day.getTime(), scale);
}
long level = 0;
if (Calendar.MILLISECOND < scale) {
// 当前指定格式不支持
return new Date(time);
} else if (Calendar.DATE <= scale) {
level = MILLS[scale - Calendar.DATE];
} else {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
c.add(scale, add + 1);
c.add(Calendar.SECOND, -1);
return c.getTime();
}
time = time + (add + 1) * level - 1000l;
return new Date(time);
}
/**
* 清空日期对象的时分秒
*
* @param d 日期对象
* @return
*/
public static Date truncation(Date d) {
return truncation(d, Calendar.DATE);
}
/**
* 清空日期对象的指定时间域之后的时间
*
* @param d
* @param scale
* @return
*/
public static Date truncation(Date d, int scale) {
if (d == null) return null;
return new Date(truncation(d.getTime(), scale));
}
private static long truncation(long time, int scale) {
long level = 0;
int offset = 0;
if (Calendar.DATE <= scale) {
level = MILLS[scale - Calendar.DATE];
} else if (scale == Calendar.WEEK_OF_YEAR || scale == Calendar.WEEK_OF_MONTH) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
if (c.get(Calendar.DAY_OF_WEEK) < 2) {
c.add(scale, -1);
}
c.set(Calendar.DAY_OF_WEEK, 2);
time = c.getTimeInMillis();
level = MILLS[0];
} else if (scale == Calendar.MONTH) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
c.set(Calendar.DAY_OF_MONTH, 1);
time = c.getTimeInMillis();
level = MILLS[0];
} else if (scale == Calendar.YEAR) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
c.set(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, 1);
time = c.getTimeInMillis();
level = MILLS[0];
} else {
// 当前指定格式不支持
return time;
}
if (scale <= Calendar.AM_PM) {
// 获得时区偏移量
offset = TimeZone.getDefault().getOffset(time);
}
return ((time + offset) / level) * level - offset;
}
// public static void main(String[] args) {
// System.out.println(formatIntervalFrowNow(getEndTime(getSystemTime(), Calendar.DATE, 30), Calendar.DAY_OF_WEEK << 4 | Calendar.MINUTE, "还有", "结束"));
// }
}
java 时间各种转换处理
最新推荐文章于 2023-07-14 17:30:30 发布