package com.common.core.utils;
|
|
import org.apache.commons.lang3.StringUtils;
|
import org.joda.time.DateTime;
|
|
import java.text.DateFormat;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.time.LocalDateTime;
|
import java.time.ZoneId;
|
import java.time.ZoneOffset;
|
import java.util.*;
|
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
public class DateUtils {
|
/**
|
* 年-月-日 时:分:秒 时间格式
|
*/
|
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
/**
|
* 年-月-日 时:分 时间格式
|
*/
|
public static final String DATE_TIME_MINUTE_FORMAT = "yyyy-MM-dd HH:mm";
|
/**
|
* 年/月/日 时:分 时间格式
|
*/
|
public static final String DATE_TIME_MINUTE_FORMAT1 = "yyyy/MM/dd HH:mm";
|
/**
|
* 日/月/年 时:分 时间格式
|
*/
|
public static final String DATE_TIME_MINUTE_FORMAT2 = "dd/MM/yyyy HH:mm";
|
/**
|
* 2020-08-09T02:35:20.000+0000 格林威治时间
|
*/
|
public static final String DATE_TIME_GL_ST_FORMAT = "yyyy-MM-dd'T'hh:mm:ss.SSSZ";
|
|
public static final String DATE_TIME_GL_ST_FORMAT_ZT = "yyyy-MM-dd'T'HH:mm:ssXXX";
|
public static final String DATE_TIME_GL_ST_FORMAT_ZT_1 = "yyyy-MM-dd'T'HH:mm:ss'Z'";
|
/**
|
* 年-月-日 时间格式
|
*/
|
public static final String DATE_FORMAT = "yyyy-MM-dd";
|
|
/**
|
* 年-月-日 时间格式
|
*/
|
public static final String YEAR_MONTH_FORMAT = "yyyy-MM-";
|
|
/**
|
* 小时格式
|
*/
|
public static final String HOUR_FORMAT = "HH";
|
/**
|
* 年月日 时间格式
|
*/
|
public static final String DATE_FORMAT_NO_SPACER = "yyyyMMdd";
|
/**
|
* 月日 时间格式
|
*/
|
public static final String MONTH_DAY_FORMAT_NO_SPACER = "MMdd";
|
/**
|
* 年月 时间格式
|
*/
|
public static final String YEAR_MONTH_FORMAT_NO_SPACER = "yyyyMM";
|
|
public static final String YEAR_FORMAT = "yyyy";
|
|
public static final String MONTH_FORMAT = "MM";
|
|
public static final String DAY_OF_MONTH_FORMAT = "dd";
|
|
public static final String BJ_TIME_ZONE = "+8";
|
|
/**
|
* 默认格式化类型集合
|
*/
|
public static final Map<Pattern, String> SUPPORT_FORMAT_MAP;
|
/**
|
* 年-月-日T时:分:秒+时:分 时间格式正则匹配表达式
|
*/
|
private static final Pattern DATE_TIME_PATTERN_ZT = Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\b\\+[0-9]{2}:[0-9]{2}$");
|
/**
|
* 年-月-日 时:分:秒 时间格式正则匹配表达式
|
*/
|
private static final Pattern DATE_TIME_PATTERN = Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$");
|
/**
|
* 年-月-日 时:分 时间格式正则匹配表达式
|
*/
|
private static final Pattern DATE_TIME_MINUTE_PATTERN = Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$");
|
/**
|
* 年/月/日 时:分 时间格式正则匹配表达式
|
*/
|
private static final Pattern DATE_TIME_MINUTE_PATTERN1 = Pattern.compile("^[0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}$");
|
/**
|
* 日/月年 时:分 时间格式正则匹配表达式
|
*/
|
private static final Pattern DATE_TIME_MINUTE_PATTERN2 = Pattern.compile("[0-9]{2}/[0-9]{2}/^[0-9]{4} [0-9]{2}:[0-9]{2}$");
|
|
/**
|
* 年-月-日 时间格式正则匹配表达式
|
*/
|
private static final Pattern DATE_PATTERN = Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}$");
|
/**
|
* 年月日 时间格式正则匹配表达式
|
*/
|
private static final Pattern DATE_PATTERN_NO_SPACER = Pattern.compile("^[0-9]{4}[0-9]{2}[0-9]{2}$");
|
/**
|
* 月日 时间格式正则匹配表达式
|
*/
|
private static final Pattern MONTH_DAY_PATTERN_NO_SPACER = Pattern.compile("^[0-9]{2}[0-9]{2}$");
|
/**
|
* 年日 时间格式正则匹配表达式
|
*/
|
private static final Pattern YEAR_MONTH_PATTERN_NO_SPACER = Pattern.compile("^[0-9]{4}[0-9]{2}$");
|
|
/**
|
* 3
|
*/
|
private static final Integer NUM_3 = 3;
|
|
/**
|
* 5
|
*/
|
private static final Integer NUM_5 = 5;
|
|
static {
|
SUPPORT_FORMAT_MAP = new ConcurrentHashMap<Pattern, String>();
|
SUPPORT_FORMAT_MAP.put(DATE_TIME_PATTERN, DATE_TIME_FORMAT);
|
SUPPORT_FORMAT_MAP.put(DATE_TIME_MINUTE_PATTERN, DATE_TIME_MINUTE_FORMAT);
|
SUPPORT_FORMAT_MAP.put(DATE_TIME_MINUTE_PATTERN1, DATE_TIME_MINUTE_FORMAT1);
|
SUPPORT_FORMAT_MAP.put(DATE_TIME_MINUTE_PATTERN2, DATE_TIME_MINUTE_FORMAT2);
|
SUPPORT_FORMAT_MAP.put(DATE_PATTERN, DATE_FORMAT);
|
SUPPORT_FORMAT_MAP.put(DATE_PATTERN_NO_SPACER, DATE_FORMAT_NO_SPACER);
|
SUPPORT_FORMAT_MAP.put(YEAR_MONTH_PATTERN_NO_SPACER, YEAR_MONTH_FORMAT_NO_SPACER);
|
SUPPORT_FORMAT_MAP.put(MONTH_DAY_PATTERN_NO_SPACER, MONTH_DAY_FORMAT_NO_SPACER);
|
SUPPORT_FORMAT_MAP.put(DATE_TIME_PATTERN_ZT, DATE_TIME_GL_ST_FORMAT_ZT);
|
}
|
|
/**
|
* 字符串转日期类型
|
*
|
* @param dateString
|
* @return
|
*/
|
public static Date convert(String dateString) {
|
Date date = null;
|
if (StringUtils.isEmpty(dateString)) {
|
return null;
|
}
|
for (Pattern pattern : SUPPORT_FORMAT_MAP.keySet()) {
|
Matcher matcher = pattern.matcher(dateString);
|
if (matcher.matches()) {
|
try {
|
String format = SUPPORT_FORMAT_MAP.get(pattern);
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
sdf.setLenient(false);
|
date = sdf.parse(dateString);
|
} catch (ParseException ignored) {
|
|
}
|
break;
|
}
|
}
|
return date;
|
}
|
|
/**
|
* 带时区字符串转日期类型
|
*
|
* @param dateString
|
* @return
|
*/
|
public static Date timeZoneConvert(String dateString) {
|
Date date = null;
|
if (StringUtils.isNotBlank(dateString)) {
|
Matcher matcher = DATE_TIME_PATTERN_ZT.matcher(dateString);
|
if (matcher.matches()) {
|
try {
|
String format = SUPPORT_FORMAT_MAP.get(DATE_TIME_PATTERN_ZT);
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
sdf.setLenient(false);
|
date = sdf.parse(dateString);
|
} catch (ParseException ignored) {
|
}
|
}
|
}
|
return date;
|
}
|
|
/**
|
* 根据格式,转化字符串为日期
|
*
|
* @param dateString
|
* @param format
|
* @return
|
*/
|
public static Date convert(String dateString, String format) {
|
if (StringUtils.isBlank(dateString)) {
|
return null;
|
}
|
boolean matches = false;
|
for (String fmt : SUPPORT_FORMAT_MAP.values()) {
|
if (fmt.equals(format)) {
|
matches = true;
|
break;
|
}
|
}
|
if (!matches) {
|
throw new IllegalArgumentException(format + " is not a valid date format");
|
}
|
Date date = null;
|
try {
|
date = new SimpleDateFormat(format).parse(dateString);
|
} catch (ParseException ignored) {
|
|
}
|
return date;
|
}
|
|
/**
|
* 根据格式,转化字符串为日期
|
*
|
* @param dateString
|
* @param format
|
* @return
|
*/
|
public static Date convertSimple(String dateString, String format) {
|
if (StringUtils.isBlank(dateString)) {
|
return null;
|
}
|
Date date = null;
|
try {
|
date = new SimpleDateFormat(format).parse(dateString);
|
} catch (ParseException ignored) {
|
|
}
|
return date;
|
}
|
|
/**
|
* 根据时间字符串解析成Date对象,该方法会自适应时间字符串格式
|
*
|
* @param dateText 时间字符串
|
*/
|
public static Date parse(String dateText) {
|
return DateUtils.convert(dateText);
|
}
|
|
/**
|
* 根据时间字符串,解析成指定格式的的Date对象
|
*
|
* @param dateText 时间字符串
|
* @param format 指定解析格式
|
*/
|
public static Date parse(String dateText, String format) {
|
return DateUtils.convert(dateText, format);
|
}
|
|
/**
|
* 格式化日期为yyyy-MM-dd HH:mm:ss格式字符串
|
*
|
* @param date 日期对象
|
*/
|
public static String format(Date date) {
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
|
simpleDateFormat.applyPattern(DATE_TIME_FORMAT);
|
return simpleDateFormat.format(date);
|
}
|
|
/**
|
* 格式化日期为指定格式字符串
|
*
|
* @param date 日期对象
|
*/
|
public static String format(Date date, String pattern) {
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
|
simpleDateFormat.applyPattern(pattern);
|
return simpleDateFormat.format(date);
|
}
|
|
/**
|
* 格式化日期为指定格式字符串
|
*
|
* @param date 日期对象
|
*/
|
public static String formatToUtc(Date date, String pattern) {
|
SimpleDateFormat df = new SimpleDateFormat(pattern);
|
df.setTimeZone(new SimpleTimeZone(0, "UTC"));
|
return df.format(date);
|
}
|
|
/**
|
* 格式化日期为yyyy-MM-dd HH:mm:ss格式字符串
|
*
|
* @param timestamp 时间戳
|
*/
|
public static String format(long timestamp) {
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
|
simpleDateFormat.applyPattern(DATE_TIME_FORMAT);
|
return simpleDateFormat.format(DateUtils.newDate(timestamp));
|
}
|
|
/**
|
* 格式化日期为指定格式字符串
|
*
|
* @param timestamp 时间戳
|
*/
|
public static String format(long timestamp, String pattern) {
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
|
simpleDateFormat.applyPattern(pattern);
|
return simpleDateFormat.format(DateUtils.newDate(timestamp));
|
}
|
|
/**
|
* 格式化日期为时间戳
|
*/
|
public static long format(String dateText) {
|
if (StringUtils.isEmpty(dateText)) {
|
return 0L;
|
}
|
Date date=DateUtils.convert(dateText);
|
return date==null ? 0L : date.getTime();
|
}
|
|
/**
|
* 获取当前时间的日期对象
|
*/
|
public static Date now() {
|
return DateUtils.newDate(DateUtils.nowTimestamp());
|
}
|
|
/**
|
* 获取当前时间时间戳
|
*/
|
public static long nowTimestamp() {
|
return System.currentTimeMillis();
|
}
|
|
/**
|
* 根据时间戳创建日期对象
|
*
|
* @param timestamp 时间戳
|
*/
|
public static Date newDate(long timestamp) {
|
return new Date(timestamp);
|
}
|
|
/**
|
* 根据指定时间创建日期对象
|
*
|
* @param year 年
|
* @param month 月
|
* @param day 日
|
* @param hour 时
|
* @param minute 分
|
* @param second 秒
|
* @param millisecond 毫秒
|
*/
|
public static Date newDate(int year, int month, int day, int hour, int minute, int second, int millisecond) {
|
DateTime dateTime = new DateTime(year, month, day, hour, minute, second, millisecond);
|
return dateTime.toDate();
|
}
|
|
/**
|
* 根据指定时间创建日期对象
|
*
|
* @param year 年
|
* @param month 月
|
* @param day 日
|
* @param hour 时
|
* @param minute 分
|
* @param second 秒
|
*/
|
public static Date newDate(int year, int month, int day, int hour, int minute, int second) {
|
return DateUtils.newDate(year, month, day, hour, minute, second, 0);
|
}
|
|
/**
|
* 根据指定时间创建日期对象
|
*
|
* @param year 年
|
* @param month 月
|
* @param day 日
|
* @param hour 时
|
* @param minute 分
|
*/
|
public static Date newDate(int year, int month, int day, int hour, int minute) {
|
return DateUtils.newDate(year, month, day, hour, minute, 0, 0);
|
}
|
|
/**
|
* 根据指定时间创建日期对象
|
*
|
* @param year 年
|
* @param month 月
|
* @param day 日
|
* @param hour 时
|
*/
|
public static Date newDate(int year, int month, int day, int hour) {
|
return DateUtils.newDate(year, month, day, hour, 0, 0, 0);
|
}
|
|
/**
|
* 根据指定时间创建日期对象
|
*
|
* @param year 年
|
* @param month 月
|
* @param day 日
|
*/
|
public static Date newDate(int year, int month, int day) {
|
return DateUtils.newDate(year, month, day, 0, 0, 0, 0);
|
}
|
|
/**
|
* 日期加减 年月日时分秒毫秒数 正数加负数减
|
*
|
* @param date 日期
|
* @param years 加减年数
|
* @param months 加减月数
|
* @param days 加减天数
|
* @param hours 加减小时数
|
* @param minutes 加减分钟数
|
* @param seconds 加减秒数
|
* @param milliseconds 加减毫秒数
|
*/
|
public static Date add(Date date, int years, int months, int days, int hours, int minutes, int seconds, int milliseconds) {
|
return DateUtils.add(date.getTime(), years, months, days, hours, minutes, seconds, milliseconds);
|
}
|
|
/**
|
* 日期加减 年月日时分秒毫秒数 正数加负数减
|
*
|
* @param timestamp 时间戳
|
* @param years 加减年数,如:1、-1
|
* @param months 加减月数,如:1、-1
|
* @param days 加减天数,如:1、-1
|
* @param hours 加减小时数,如:1、-1
|
* @param minutes 加减分钟数,如:1、-1
|
* @param seconds 加减秒数,如:1、-1
|
* @param milliseconds 加减毫秒数,如:1、-1
|
*/
|
public static Date add(long timestamp, int years, int months, int days, int hours, int minutes, int seconds, int milliseconds) {
|
int zero = 0;
|
DateTime dateTime = new DateTime(timestamp);
|
if (zero != years) {
|
dateTime = dateTime.plusYears(years);
|
}
|
if (zero != months) {
|
dateTime = dateTime.plusMonths(months);
|
}
|
if (zero != days) {
|
dateTime = dateTime.plusDays(days);
|
}
|
if (zero != hours) {
|
dateTime = dateTime.plusHours(hours);
|
}
|
if (zero != minutes) {
|
dateTime = dateTime.plusMinutes(minutes);
|
}
|
if (zero != seconds) {
|
dateTime = dateTime.plusSeconds(seconds);
|
}
|
if (zero != milliseconds) {
|
dateTime = dateTime.plusMillis(milliseconds);
|
}
|
return dateTime.toDate();
|
}
|
|
/**
|
* 获取本周的第一天
|
*
|
* @return Long
|
**/
|
public static Long getWeekStart(long timestamp) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTimeInMillis(timestamp);
|
Integer date = cal.get(Calendar.DATE);
|
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
if (cal.get(Calendar.DATE) > date) {
|
cal.add(Calendar.DATE, -7);
|
}
|
Date time = cal.getTime();
|
return format(new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00");
|
}
|
|
/**
|
* 获取本周的最后一天
|
*
|
* @return Long
|
**/
|
public static Long getWeekEnd(long timestamp) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTimeInMillis(timestamp);
|
Integer date = cal.get(Calendar.DATE);
|
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
|
if (cal.get(Calendar.DATE) < date) {
|
cal.add(Calendar.DATE, 7);
|
}
|
Date time = cal.getTime();
|
return format(new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59");
|
}
|
|
/**
|
* 获取本天的最后一秒
|
*
|
* @return Long
|
**/
|
public static Date getDayEnd(Date date) {
|
if (date == null) {
|
return null;
|
}
|
return parse(new SimpleDateFormat("yyyy-MM-dd").format(date.getTime()) + " 23:59:59");
|
}
|
|
/**
|
* 获取本天的第一秒
|
*
|
* @return Long
|
**/
|
public static Date getDayStart(Date date) {
|
if (date == null) {
|
return null;
|
}
|
return parse(new SimpleDateFormat("yyyy-MM-dd").format(date.getTime()) + " 00:00:00");
|
}
|
|
/**
|
* 根据时间戳获取版本号,Calendar取周是从周日开始计算,需要进行修改为以周一为准
|
*
|
* @param timestamp 时间戳
|
* @return 版本号
|
*/
|
public static Integer getDateVersion(long timestamp) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTimeInMillis(timestamp);
|
calendar.setFirstDayOfWeek(Calendar.MONDAY);
|
calendar.setMinimalDaysInFirstWeek(4);
|
return calendar.get(Calendar.YEAR) * 1000 + calendar.get(Calendar.WEEK_OF_YEAR);
|
}
|
|
/**
|
* 获取当前时间的版本号
|
*
|
* @return 版本号
|
*/
|
public static Integer getNowVersion() {
|
return getDateVersion(nowTimestamp());
|
}
|
|
|
/**
|
* 获取年月周
|
*
|
* @return String
|
**/
|
public static Map<String, String> getYearMonthWeekStr(long timestamp) {
|
// 当月第一天
|
Calendar firstDay = Calendar.getInstance();
|
firstDay.setTimeInMillis(timestamp);
|
firstDay.set(Calendar.DAY_OF_MONTH, 1);
|
|
// 当月最后一天
|
Calendar lastDay = Calendar.getInstance();
|
lastDay.setTimeInMillis(timestamp);
|
lastDay.add(Calendar.MONTH, 1);
|
lastDay.set(Calendar.DAY_OF_MONTH, 0);
|
|
// 第一个周日
|
Calendar firsSunday = Calendar.getInstance();
|
firsSunday.setTimeInMillis(firstDay.getTimeInMillis());
|
firsSunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
|
if (firsSunday.getTimeInMillis() != firstDay.getTimeInMillis()) {
|
firsSunday.add(Calendar.DATE, 7);
|
}
|
|
|
// 最后一个周一
|
Calendar lastMonday = Calendar.getInstance();
|
lastMonday.setTimeInMillis(lastDay.getTimeInMillis());
|
lastMonday.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
if (lastMonday.getTimeInMillis() > lastDay.getTimeInMillis()) {
|
lastMonday.add(Calendar.DATE, -7);
|
}
|
|
String year = "";
|
String month = "";
|
String week = "";
|
DateFormat df = new SimpleDateFormat("MMM", Locale.ENGLISH);
|
if (firsSunday.getTimeInMillis() >= timestamp) {
|
// 三天以上算本月
|
if (firsSunday.get(Calendar.DATE) > NUM_3) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTimeInMillis(timestamp);
|
year = cal.get(Calendar.YEAR) + "";
|
month = df.format(cal.getTime());
|
|
week = cal.get(Calendar.WEEK_OF_MONTH) + "th Week";
|
} else {
|
// 三天以内算上月
|
// 上月第一天
|
Calendar preFirstDay = Calendar.getInstance();
|
preFirstDay.setTimeInMillis(timestamp);
|
preFirstDay.set(Calendar.DAY_OF_MONTH, 1);
|
preFirstDay.add(Calendar.MONTH, -1);
|
|
// 上月第一个周日
|
Calendar preFirsSunday = Calendar.getInstance();
|
preFirsSunday.setTimeInMillis(preFirstDay.getTimeInMillis());
|
preFirsSunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
|
if (preFirsSunday.getTimeInMillis() != preFirstDay.getTimeInMillis()) {
|
preFirsSunday.add(Calendar.DATE, 7);
|
}
|
|
// 上月最后一天
|
Calendar preLastDay = Calendar.getInstance();
|
preLastDay.setTimeInMillis(firstDay.getTimeInMillis());
|
preLastDay.add(Calendar.DATE, -1);
|
|
year = preLastDay.get(Calendar.YEAR) + "";
|
month = df.format(preLastDay.getTime());
|
if (preFirsSunday.get(Calendar.DATE) > NUM_3) {
|
week = preLastDay.get(Calendar.WEEK_OF_MONTH) + "th Week";
|
} else {
|
week = (preLastDay.get(Calendar.WEEK_OF_MONTH) - 1) + "th Week";
|
}
|
}
|
} else if (lastMonday.getTimeInMillis() <= timestamp) {
|
// 本月在最后一周占4天及以上
|
if (lastDay.get(Calendar.DAY_OF_WEEK) >= NUM_5 || lastDay.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTimeInMillis(timestamp);
|
year = cal.get(Calendar.YEAR) + "";
|
month = df.format(cal.getTime());
|
if (firsSunday.get(Calendar.DATE) > NUM_3) {
|
week = cal.get(Calendar.WEEK_OF_MONTH) + "th Week";
|
} else {
|
week = (cal.get(Calendar.WEEK_OF_MONTH) - 1) + "th Week";
|
}
|
} else {
|
// 下月第一天
|
Calendar nextFirstDay = Calendar.getInstance();
|
nextFirstDay.setTimeInMillis(timestamp);
|
nextFirstDay.add(Calendar.MONTH, 1);
|
nextFirstDay.set(Calendar.DAY_OF_MONTH, 1);
|
|
year = nextFirstDay.get(Calendar.YEAR) + "";
|
month = df.format(nextFirstDay.getTime());
|
week = nextFirstDay.get(Calendar.WEEK_OF_MONTH) + "th Week";
|
}
|
} else {
|
Calendar cal = Calendar.getInstance();
|
cal.setTimeInMillis(timestamp);
|
|
year = cal.get(Calendar.YEAR) + "";
|
month = df.format(cal.getTime());
|
if (firsSunday.get(Calendar.DATE) > NUM_3) {
|
week = cal.get(Calendar.WEEK_OF_MONTH) + "th Week";
|
} else {
|
week = (cal.get(Calendar.WEEK_OF_MONTH) - 1) + "th Week";
|
}
|
}
|
|
Map<String, String> result = new HashMap<String, String>();
|
result.put("year", year);
|
result.put("month", month);
|
result.put("week", week);
|
return result;
|
}
|
|
/**
|
* 添加年份
|
*
|
* @param date
|
* @param years
|
* @return
|
*/
|
public static Date addYears(Date date, int years) {
|
return DateUtils.addYears(date.getTime(), years);
|
}
|
|
/**
|
* 添加年份
|
*
|
* @param timestamp
|
* @param years
|
* @return
|
*/
|
public static Date addYears(long timestamp, int years) {
|
return DateUtils.add(timestamp, years, 0, 0, 0, 0, 0, 0);
|
}
|
|
/**
|
* 添加月份
|
*
|
* @param date
|
* @param months
|
* @return
|
*/
|
public static Date addMonths(Date date, int months) {
|
return DateUtils.addMonths(date.getTime(), months);
|
}
|
|
/**
|
* 添加月份
|
*
|
* @param timestamp
|
* @param months
|
* @return
|
*/
|
public static Date addMonths(long timestamp, int months) {
|
return DateUtils.add(timestamp, 0, months, 0, 0, 0, 0, 0);
|
}
|
|
/**
|
* 添加日期
|
*
|
* @param date
|
* @param days
|
* @return
|
*/
|
public static Date addDays(Date date, int days) {
|
return DateUtils.addDays(date.getTime(), days);
|
}
|
|
/**
|
* 添加日期
|
*
|
* @param timestamp
|
* @param days
|
* @return
|
*/
|
public static Date addDays(long timestamp, int days) {
|
return DateUtils.add(timestamp, 0, 0, days, 0, 0, 0, 0);
|
}
|
|
/**
|
* 添加小时
|
*
|
* @param date
|
* @param hours
|
* @return
|
*/
|
public static Date addHours(Date date, int hours) {
|
return DateUtils.addHours(date.getTime(), hours);
|
}
|
|
/**
|
* 添加小时
|
*
|
* @param timestamp
|
* @param hours
|
* @return
|
*/
|
public static Date addHours(long timestamp, int hours) {
|
return DateUtils.add(timestamp, 0, 0, 0, hours, 0, 0, 0);
|
}
|
|
/**
|
* 添加分钟
|
*
|
* @param date
|
* @param minutes
|
* @return
|
*/
|
public static Date addMinutes(Date date, int minutes) {
|
return DateUtils.addMinutes(date.getTime(), minutes);
|
}
|
|
/**
|
* 添加分钟
|
*
|
* @param timestamp
|
* @param minutes
|
* @return
|
*/
|
public static Date addMinutes(long timestamp, int minutes) {
|
return DateUtils.add(timestamp, 0, 0, 0, 0, minutes, 0, 0);
|
}
|
|
/**
|
* 获取当前月份
|
*
|
* @return
|
*/
|
public static int getCurrentMonth() {
|
DateTime dateTime = DateTime.now();
|
return dateTime.getMonthOfYear();
|
}
|
|
/**
|
* 通过时间秒毫秒数判断两个时间的间隔
|
*
|
* @param date1
|
* @param date2
|
* @return
|
*/
|
public static int differentDaysByMillisecond(Date date1, Date date2) {
|
int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
|
return days;
|
}
|
|
/**
|
* 取两个日期的中间日期,默认取大
|
*
|
* @param start
|
* @param end
|
* @return
|
*/
|
public static Date getMeddleDate(Date start, Date end) {
|
int days = (int) ((end.getTime() - start.getTime()) / (1000 * 3600 * 24));
|
return DateUtils.addDays(start, (int) Math.floor(Double.valueOf(days) / 2));
|
}
|
|
/**
|
* 通过时间秒毫秒数判断两个时间的间隔
|
*
|
* @param date1
|
* @param date2
|
* @return
|
*/
|
public static int differentSeconds(Date date1, Date date2) {
|
int days = (int) ((date2.getTime() - date1.getTime()) / (1000));
|
return days;
|
}
|
|
/**
|
* 按日期递增规则生成分区列表
|
*
|
* @param start
|
* @param end
|
* @return
|
*/
|
public static List<String> generateDayList(String start, String end, String pattern) {
|
List<String> partitionList = new ArrayList<String>();
|
partitionList.add(start);
|
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
Date startDate;
|
Date endDate;
|
try {
|
startDate = sdf.parse(start);
|
endDate = sdf.parse(end);
|
} catch (Exception e) {
|
|
return null;
|
}
|
Calendar startCal = Calendar.getInstance();
|
Calendar endCal = Calendar.getInstance();
|
startCal.setTime(startDate);
|
endCal.setTime(endDate);
|
while (startCal.before(endCal)) {
|
startCal.add(Calendar.DAY_OF_MONTH, 1);
|
partitionList.add(sdf.format(startCal.getTime()));
|
}
|
return partitionList;
|
}
|
|
/**
|
* 格式化utc时间
|
*
|
* @param utcDate
|
* @return
|
*/
|
public static String formatUtcDate(String utcDate) {
|
DateFormat df = new SimpleDateFormat(DateUtils.DATE_TIME_GL_ST_FORMAT);
|
DateFormat df2 = new SimpleDateFormat(DateUtils.DATE_TIME_FORMAT);
|
|
Date date = null;
|
try {
|
date = df.parse(utcDate);
|
} catch (ParseException e) {
|
return utcDate;
|
}
|
return df2.format(date);
|
}
|
|
/**
|
* 变更日期字符串格式
|
*
|
* @return
|
*/
|
public static String convertDateStrPattern(String dateStr, String sourcePattern, String targetPattern) {
|
DateFormat df = new SimpleDateFormat(sourcePattern);
|
DateFormat df2 = new SimpleDateFormat(targetPattern);
|
|
Date date = null;
|
try {
|
date = df.parse(dateStr);
|
} catch (ParseException e) {
|
return null;
|
}
|
return df2.format(date);
|
}
|
|
/**
|
* 格式化utc时间
|
*
|
* @param localDate
|
* @return
|
*/
|
public static Date localToUTC(Date localDate) {
|
long localTimeInMillis = localDate.getTime();
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTimeInMillis(localTimeInMillis);
|
int zoneOffset = calendar.get(Calendar.ZONE_OFFSET);
|
int dstOffset = calendar.get(Calendar.DST_OFFSET);
|
calendar.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
|
Date utcDate = new Date(calendar.getTimeInMillis());
|
return utcDate;
|
}
|
|
/**
|
* @param date1
|
* @param date2
|
* @return
|
*/
|
public static int differentDays(Date date1, Date date2) {
|
Calendar cal1 = Calendar.getInstance();
|
cal1.setTime(date1);
|
|
Calendar cal2 = Calendar.getInstance();
|
cal2.setTime(date2);
|
int day1 = cal1.get(Calendar.DAY_OF_YEAR);
|
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
|
|
int year1 = cal1.get(Calendar.YEAR);
|
int year2 = cal2.get(Calendar.YEAR);
|
if (year1 != year2) {
|
int timeDistance = 0;
|
for (int i = year1; i < year2; i++) {
|
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {//闰年
|
timeDistance += 366;
|
} else {//不是闰年
|
timeDistance += 365;
|
}
|
}
|
return timeDistance + (day2 - day1);
|
} else { //不同年
|
|
System.out.println("判断day2 - day1 : " + (day2 - day1));
|
return day2 - day1;
|
}
|
}
|
|
public static Long timestamp2dateLong(Long timestamp) {
|
return DateUtils.format(DateUtils.format(timestamp).substring(0, 10));
|
}
|
|
public static boolean isValidDate(String str, String pattern) {
|
boolean convertSuccess = true;
|
SimpleDateFormat format = new SimpleDateFormat(pattern);
|
try {
|
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
|
format.setLenient(false);
|
format.parse(str);
|
} catch (ParseException e) {
|
convertSuccess = false;
|
}
|
return convertSuccess;
|
}
|
}
|