import com.google.common.collect.Lists;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Days;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.ISODateTimeFormat;
import org.springframework.util.Assert;
/**
* @author
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DateTimeUtil {
private static final String DATE_STRING_NOT_NULL = "The 'dateString' must not be null!";
private static final String DATE_NOT_NULL = "The 'date' must not be null!";
/**
* 默认日期格式:yyyy-MM-dd
*/
public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
/**
* 时间格式:yyyy-MM-dd HH
*/
public static final String YYYYMM_HH_DATE_PATTERN = "yyyy-MM-dd HH";
/**
* 默认时间格式:yyyyMM
*/
public static final String YYYYMM_DATE_PATTERN = "yyyyMM";
/**
* 默认时间格式:yyyyMMdd
*/
public static final String YYYYMMDD_DATE_PATTERN = "yyyyMMdd";
public static final String YYYY_MM_DD = "yyyy/MM/dd";
/**
* 默认时间格式:yyyy-MM-dd HH:mm:ss
*/
public static final String DEFAULT_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 取得系统当前年份
*/
public static int currentYear() {
return DateTime.now().getYear();
}
/**
* 取得当前系统日期
*/
public static Date currentDate() {
return DateTime.now().toDate();
}
/**
* 取得系统当前日期,返回默认日期格式的字符串。
*/
public static String nowDate(String strFormat) {
Assert.notNull(strFormat, DATE_STRING_NOT_NULL);
return new DateTime().toString(strFormat, Locale.CHINESE);
}
/**
* 取得当前系统时间戳
*/
public static Timestamp currentTimestamp() {
return new Timestamp(new DateTime().getMillis());
}
/**
* 将日期字符串转换为java.util.Date对象
*
* @param pattern 日期格式
*/
public static Date toDate(String dateString, String pattern) {
Assert.notNull(dateString, DATE_STRING_NOT_NULL);
Assert.notNull(pattern, "The 'pattern' must not be null!");
return DateTime.parse(dateString, DateTimeFormat.forPattern(pattern)).toDate();
}
public static Date toDate(Date date, String pattern) {
Assert.notNull(date, DATE_NOT_NULL);
Assert.notNull(pattern, "The 'pattern' must not be null!");
return DateTime.parse(toDateString(date, pattern), DateTimeFormat.forPattern(pattern)).toDate();
}
/**
* 将日期字符串转换为java.util.Date对象
*
* @param dateString example:"20XX-05-03T15:11:45.7009265+08:00"
*/
public static Date toISODate(String dateString) {
Assert.notNull(dateString, DATE_STRING_NOT_NULL);
return DateTime.parse(dateString, ISODateTimeFormat.dateTime()).toDate();
}
/**
* 将日期字符串转换为java.util.Date对象,使用默认日期格式
*/
public static Date toDate(String dateString) {
Assert.notNull(dateString, DATE_STRING_NOT_NULL);
return DateTime.parse(dateString).toDate();
}
/**
* 将时间字符串转换为java.util.Date对象
*/
public static Date toDateTime(String dateString) {
Assert.notNull(dateString, DATE_STRING_NOT_NULL);
return DateTime.parse(dateString, DateTimeFormat.forPattern(DEFAULT_DATETIME_PATTERN)).toDate();
}
/**
* 将java.util.Date对象转换为字符串
*/
public static String toDateString(Date date, String pattern) {
Assert.notNull(date, DATE_NOT_NULL);
Assert.notNull(pattern, "The 'pattern' must not be null!");
return new DateTime(date).toString(pattern, Locale.CHINESE);
}
/**
* 将java.util.Date对象转换为字符串,使用默认日期格式
*/
public static String toDateString(Date date) {
Assert.notNull(date, DATE_NOT_NULL);
return new DateTime(date).toString(DEFAULT_DATE_PATTERN, Locale.CHINESE);
}
/**
* 将int 10位时间搓转换为字符串,使用默认日期格式
*/
public static String toDateString(int time) {
Assert.notNull(time, DATE_STRING_NOT_NULL);
return new DateTime(new Date(time * 1000L)).toString(DEFAULT_DATETIME_PATTERN, Locale.CHINESE);
}
/**
* 将long 13位时间搓转换为字符串,使用默认日期格式
*/
public static String toDateString(long time) {
Assert.notNull(time, DATE_STRING_NOT_NULL);
return new DateTime(new Date(time)).toString(DEFAULT_DATETIME_PATTERN, Locale.CHINESE);
}
/**
* 将java.util.Date对象转换为时间字符串,使用默认日期格式
*/
public static String toDateTimeString(Date date) {
Assert.notNull(date, DATE_NOT_NULL);
return new DateTime(date).toString(DEFAULT_DATETIME_PATTERN, Locale.CHINESE);
}
/**
* 日期相减
*/
public static Date diffDate(Date date, Integer days) {
Assert.notNull(date, DATE_NOT_NULL);
Assert.notNull(days, "The 'days' must not be null!");
return new DateTime(date).minusDays(days).toDate();
}
/**
* 返回毫秒
*
* @param date 日期
* @return 返回毫秒
*/
public static long getMillis(Date date) {
Assert.notNull(date, DATE_NOT_NULL);
return new DateTime(date).getMillis();
}
/**
* 日期相加
*
* @param date 日期
* @param days 天数
* @return 返回相加后的日期
*/
public static Date addDate(Date date, Integer days) {
Assert.notNull(date, DATE_NOT_NULL);
Assert.notNull(days, "The 'days' must not be null!");
return new DateTime(date).plusDays(days).toDate();
}
/**
* 日期增加年数
*/
public static Date addYear(Date date, Integer years) {
Assert.notNull(date, DATE_NOT_NULL);
Assert.notNull(years, DATE_STRING_NOT_NULL);
return new DateTime(date).plusYears(years).toDate();
}
/**
* 日期增加月数
*/
public static Date addMonth(Date date, Integer months) {
Assert.notNull(date, DATE_NOT_NULL);
Assert.notNull(months, DATE_STRING_NOT_NULL);
return new DateTime(date).plusMonths(months).toDate();
}
/**
* 日期增加小时
*/
public static Date addHours(Date date, Integer hours) {
Assert.notNull(date, DATE_NOT_NULL);
Assert.notNull(hours, "The 'hours' must not be null!");
return new DateTime(date).plusHours(hours).toDate();
}
/**
* 日期增加分钟
*/
public static Date addMinutes(Date date, Integer minutes) {
Assert.notNull(date, DATE_NOT_NULL);
Assert.notNull(minutes, "The 'minutes' must not be null!");
return new DateTime(date).plusMinutes(minutes).toDate();
}
/**
* 日期增加秒
*/
public static Date addSeconds(Date date, Integer seconds) {
Assert.notNull(date, DATE_NOT_NULL);
Assert.notNull(seconds, "The 'seconds' must not be null!");
return new DateTime(date).plusSeconds(seconds).toDate();
}
/**
* 根据季度获得相应的月份
*
* @param quarters 季度
* @return 返回相应的月份
*/
public static String getMonth(String quarters) {
Assert.notNull(quarters, "The 'quarters' must not be null!");
String month;
int m = Integer.parseInt(quarters);
m = m * 3 - 2;
if (m > 0 && m < 10) {
month = "0" + String.valueOf(m);
} else {
month = String.valueOf(m);
}
return month;
}
/**
* 根据月份获得相应的季度
*
* @param month 月份
* @return 返回相应的季度
*/
public static String getQuarters(String month) {
Assert.notNull(month, "The 'month' must not be null!");
String quarters = null;
int m = Integer.parseInt(month);
if (m == 1 || m == 2 || m == 3) {
quarters = "1";
}
if (m == 4 || m == 5 || m == 6) {
quarters = "2";
}
if (m == 7 || m == 8 || m == 9) {
quarters = "3";
}
if (m == 10 || m == 11 || m == 12) {
quarters = "4";
}
return quarters;
}
/**
* 获取日期所在星期的第一天,这里设置第一天为星期日
*/
public static String getFirstDateOfWeek(String datestr) {
Assert.notNull(datestr, DATE_STRING_NOT_NULL);
DateTime dt = DateTime.parse(datestr);
return dt.plusDays(-(dt.getDayOfWeek()) + 1).toString(DEFAULT_DATE_PATTERN);
}
/**
* 获取日期所在当年的第几周
*/
public static int getWeekOfYear(String datestr) {
Assert.notNull(datestr, DATE_STRING_NOT_NULL);
return DateTime.parse(datestr).weekOfWeekyear().get();
}
/**
* 通过日期字符串yyyy-MM-dd HH:mm:ss 获取星期
*/
public static String getWeekday(String datestr) {
Assert.notNull(datestr, DATE_STRING_NOT_NULL);
try {
switch (DateTime.parse(datestr).dayOfWeek().get()) {
case 1:
return "星期一";
case 2:
return "星期二";
case 3:
return "星期三";
case 4:
return "星期四";
case 5:
return "星期五";
case 6:
return "星期六";
default:
return "星期天";
}
} catch (Exception ex) {
throw new SysException(ex);
}
}
public static Date getDate(Object object) {
Assert.notNull(object, "The 'object' must not be null!");
if (object instanceof String) {
return DateTime.parse((String) object).toDate();
} else if (object instanceof Date || object instanceof Timestamp) {
return (Date) object;
} else if (object instanceof Long) {
return new DateTime((Long) object).toDate();
} else {
throw new SysException("this object can't to date!");
}
}
public static Date fromTimeticks(Long ticks) {
Assert.notNull(ticks, "The 'ticks' must not be null!");
return new DateTime(ticks).toDate();
}
public static Long toTimeticks(Date time) {
return time.getTime();
}
/**
* 获取UTC时间戳(10位)
*/
public static int getUTCOfSeconds() {
return (int) (new DateTime(DateTimeZone.UTC).toDate().getTime() / 1000);
}
/**
* 根据Date获取UTC时间戳(10位)
*/
public static int getUTCOfSeconds(Date date) {
return (int) (date.getTime() / 1000);
}
/**
* 获取UTC零点的时间戳(10位)
*/
public static int getUTCMinDayOfSeconds(Date date) {
Assert.notNull(date, DATE_NOT_NULL);
return (int) (new DateTime(date, DateTimeZone.UTC).millisOfDay().withMinimumValue().toDate().getTime() / 1000);
}
/**
* 获取UTC 24点的时间戳(10位)
*/
public static int getUTCMaxDayOfSeconds() {
return (int) (DateTime.now(DateTimeZone.UTC).millisOfDay().withMaximumValue().toDate().getTime() / 1000);
}
/**
* 获取UTC 24点的时间戳(10位)
*/
public static int getUTCMaxDayOfSeconds(Date date) {
Assert.notNull(date, DATE_NOT_NULL);
return (int) (new DateTime(date, DateTimeZone.UTC).millisOfDay().withMaximumValue().toDate().getTime() / 1000);
}
/**
* 获取UTC时间戳(以毫秒为单位)
*/
public static long getUTCOfMillis() {
return DateTime.now(DateTimeZone.UTC).toDate().getTime();
}
/**
* 获取当前月第一天零点时间戳seconds:
*/
public static int getUTCMinMonthOfSeconds() {
return getUTCMinDayOfSeconds(DateTime.now().dayOfMonth().withMinimumValue().toDate());
}
/**
* 获取当前月最后一天的24点时间戳seconds:
*/
public static int getUTCMaxMonthOfSeconds() {
return getUTCMaxDayOfSeconds(DateTime.now().dayOfMonth().withMaximumValue().toDate());
}
/**
* 获取当前时间的7天之前的0点时间戳seconds:
*/
public static int getUTCBefore7DayOfSeconds() {
return getUTCMinDayOfSeconds(DateTime.now().minusDays(7).toDate());
}
/**
* 获取当前时间的X天之前的0点时间戳seconds:
*/
public static int getUTCBeforeDayOfSeconds(int beforeDay) {
return getUTCMinDayOfSeconds(DateTime.now().minusDays(beforeDay).toDate());
}
/**
* 将UTC时间字符串转换为java.util.Date(yyyy-MM-dd)对象
*/
public static String toUTCShortStringTime(int time) {
Assert.notNull(time, DATE_STRING_NOT_NULL);
return new DateTime(new Date(time * 1000L), DateTimeZone.UTC).toString(DEFAULT_DATE_PATTERN);
}
/**
* 两个时间差X天
*/
public static int betweenDays(Date startTime, Date endTime) {
DateTime _startTime = new DateTime(startTime);
DateTime _endTime = new DateTime(endTime);
return Days.daysBetween(_startTime, _endTime).getDays();
}
/**
* 获取时间之后天数的
*/
public static List<String> days(Date startTime, int days) {
List<String> _days = Lists.newArrayList();
Date time = startTime;
for (int i = 0; i < days; i++) {
_days.add(toDateString(time, DEFAULT_DATE_PATTERN));
time = DateTimeUtil.addDate(time, 1);
}
return _days;
}
public static Long getSecondDay(Long day) {
return 24 * 60 * 60 * day;
}
public static boolean isToday(Date time) {
if (EmptyUtils.isEmpty(time)) {
return false;
}
DateTime.Property property = DateTime.now().millisOfDay();
return time.getTime() >= property.withMinimumValue().getMillis() && time.getTime() <= property
.withMaximumValue().getMillis();
}
}
DateTimeUtil
最新推荐文章于 2024-07-07 12:44:01 发布