|
@@ -0,0 +1,625 @@
|
|
|
+package com.gyee.power.fitting.common.util;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
+import org.apache.commons.lang3.time.DateUtils;
|
|
|
+
|
|
|
+import java.lang.management.ManagementFactory;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.GregorianCalendar;
|
|
|
+import java.util.TimeZone;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class DateUtil extends DateUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getCurrentDate() {
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
|
|
|
+ String date = df.format(new Date());
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取前 N 小时的时间
|
|
|
+ *
|
|
|
+ * @param hour
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getPreviousDate(int hour) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour);
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String date = df.format(calendar.getTime());
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前时间后 N 小时的时间
|
|
|
+ *
|
|
|
+ * @param hour
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getNextDate(int hour) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String date = df.format(calendar.getTime());
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前时间后 N 小时的时间
|
|
|
+ *
|
|
|
+ * @param hour
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getNextDateTimestamp(int hour) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);
|
|
|
+ long time = calendar.getTime().getTime();
|
|
|
+ return time;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串时间转时间戳
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long covertDateTimestamp(String time){
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ long stamp = 0;
|
|
|
+ try {
|
|
|
+ stamp = sdf.parse(time).getTime();
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return stamp;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间格式(yyyy-MM-dd)
|
|
|
+ */
|
|
|
+ public final static String DATE_PATTERN = "yyyy-MM-dd";
|
|
|
+ /**
|
|
|
+ * 时间格式(yyyy-MM-dd HH:mm:ss)
|
|
|
+ */
|
|
|
+ public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
|
|
+
|
|
|
+ public static String format(Date date) {
|
|
|
+ return format(date, DATE_PATTERN);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatDateTime(Date date) {
|
|
|
+ return format(date, DATE_TIME_PATTERN);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 时间转换
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param pattern
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String format(Date date, String pattern) {
|
|
|
+ if (date != null) {
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat(pattern);
|
|
|
+ return df.format(date);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串格式化
|
|
|
+ * @param time
|
|
|
+ * @param pattern
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String format(String time,String pattern) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
+ Date dateTime = null;
|
|
|
+ try {
|
|
|
+ dateTime = sdf.parse(time);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+
|
|
|
+ }
|
|
|
+ return format(dateTime, pattern);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间转换
|
|
|
+ *
|
|
|
+ * @param time
|
|
|
+ * @param pattern
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String format(Long time, String pattern) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
+ Date date = new Date(time);
|
|
|
+ String format = sdf.format(date);
|
|
|
+ return format;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String YYYY = "yyyy";
|
|
|
+
|
|
|
+ public static String YYYY_MM = "yyyy-MM";
|
|
|
+
|
|
|
+ public static String YYYY_MM_DD_CHN = "yyyy年MM月dd日";
|
|
|
+
|
|
|
+ public static String YYYY_MM_DD = "yyyy-MM-dd";
|
|
|
+
|
|
|
+ public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
|
|
|
+
|
|
|
+ public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
|
|
|
+
|
|
|
+ private static String[] parsePatterns = {
|
|
|
+ "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
|
|
|
+ "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
|
|
|
+ "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前Date型日期
|
|
|
+ *
|
|
|
+ * @return Date() 当前日期
|
|
|
+ */
|
|
|
+ public static Date getNowDate() {
|
|
|
+ return new Date();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前日期, 默认格式为yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @return String
|
|
|
+ */
|
|
|
+ public static String getDate() {
|
|
|
+ return dateTimeNow(YYYY_MM_DD);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String getTimeDiff() {
|
|
|
+ return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String dateTimeNow() {
|
|
|
+ return dateTimeNow(YYYYMMDDHHMMSS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String dateTimeNow(final String format) {
|
|
|
+ return parseDateToStr(format, new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String dateTime(final Date date) {
|
|
|
+ return parseDateToStr(YYYY_MM_DD, date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String parseDateToStr(final String format, final Date date) {
|
|
|
+ return new SimpleDateFormat(format).format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final Date dateTime(final String format, final String ts) {
|
|
|
+ try {
|
|
|
+ return new SimpleDateFormat(format).parse(ts);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取0点0分0秒值
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static final Date dateZeroFormat(Date date){
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
|
|
|
+ 0, 0, 0);
|
|
|
+ Date time = cal.getTime();
|
|
|
+ return time;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期路径 即年/月/日 如2018/08/08
|
|
|
+ */
|
|
|
+ public static final String datePath() {
|
|
|
+ Date now = new Date();
|
|
|
+ return DateFormatUtils.format(now, "yyyy/MM/dd");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期路径 即年/月/日 如20180808
|
|
|
+ */
|
|
|
+ public static final String dateTime() {
|
|
|
+ Date now = new Date();
|
|
|
+ return DateFormatUtils.format(now, "yyyyMMdd");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期型字符串转化为日期 格式
|
|
|
+ */
|
|
|
+ public static Date parseDate(Object str) {
|
|
|
+ if (str == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return parseDate(str.toString(), parsePatterns);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取服务器启动时间
|
|
|
+ */
|
|
|
+ public static Date getServerStartDate() {
|
|
|
+ long time = ManagementFactory.getRuntimeMXBean().getStartTime();
|
|
|
+ return new Date(time);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算两个时间差
|
|
|
+ */
|
|
|
+ public static String getDatePoor(Date endDate, Date nowDate) {
|
|
|
+ long nd = 1000 * 24 * 60 * 60;
|
|
|
+ long nh = 1000 * 60 * 60;
|
|
|
+ long nm = 1000 * 60;
|
|
|
+ // long ns = 1000;
|
|
|
+ // 获得两个时间的毫秒时间差异
|
|
|
+ long diff = endDate.getTime() - nowDate.getTime();
|
|
|
+ // 计算差多少天
|
|
|
+ long day = diff / nd;
|
|
|
+ // 计算差多少小时
|
|
|
+ long hour = diff % nd / nh;
|
|
|
+ // 计算差多少分钟
|
|
|
+ long min = diff % nd % nh / nm;
|
|
|
+ // 计算差多少秒//输出结果
|
|
|
+ // long sec = diff % nd % nh % nm / ns;
|
|
|
+ return day + "天" + hour + "小时" + min + "分钟";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将时间的时分秒毫秒字段去掉
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date truncate(Date date) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算两个时间之间差的天数(取整后)
|
|
|
+ *
|
|
|
+ * @param d1
|
|
|
+ * @param d2
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int daysDiff(Date d1, Date d2) {
|
|
|
+ return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 60 * 24 * 1000));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将字符串日期转换成日期类型
|
|
|
+ *
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date parseStrtoDate(String time) {
|
|
|
+
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date dateTime = null;
|
|
|
+ try {
|
|
|
+ dateTime = sdf.parse(time);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+
|
|
|
+ }
|
|
|
+ return dateTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date parseStrtoDate(String time,String pattern) {
|
|
|
+
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
+ Date dateTime = null;
|
|
|
+ try {
|
|
|
+ dateTime = sdf.parse(time);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+
|
|
|
+ }
|
|
|
+ return dateTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date str2DateTime(String time,String pattern) {
|
|
|
+
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
+ Date dateTime = null;
|
|
|
+ try {
|
|
|
+ dateTime = sdf.parse(time);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+
|
|
|
+ }
|
|
|
+ return dateTime;
|
|
|
+ }
|
|
|
+ public static Date str2DateTime(String time) {
|
|
|
+ return str2DateTime(time,DATE_TIME_PATTERN);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加减年份
|
|
|
+ */
|
|
|
+ public static Date dateTimeAddYear(Date date,int k) {
|
|
|
+ Calendar instance = Calendar.getInstance();
|
|
|
+ instance.setTime(date);
|
|
|
+ instance.add(Calendar.YEAR,k);
|
|
|
+ return instance.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String datetime2Str(Date time,String pattern) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
+ return sdf.format(time);
|
|
|
+ }
|
|
|
+ public static String datetime2Str(Date time) {
|
|
|
+ return datetime2Str(time,DATE_TIME_PATTERN);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static double hoursDiff1(Date d1, Date d2) {
|
|
|
+ return Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (double) (60 * 60 * 1000));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static double hoursDiff2(Date d1, Date d2) {
|
|
|
+ return Math.abs((d1.getTime() - d2.getTime())) / (double) (60 * 60 * 1000);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算两个时间之间差的分钟数(取整后)
|
|
|
+ *
|
|
|
+ * @param d1
|
|
|
+ * @param d2
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int minutesDiff(Date d1, Date d2) {
|
|
|
+ return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 1000));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算两个时间之间差的分钟数(取整后)
|
|
|
+ *
|
|
|
+ * @param d1
|
|
|
+ * @param d2
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static double minutesDiff2(Date d1, Date d2) {
|
|
|
+ return Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 1000));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取系统时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date now() {
|
|
|
+ return new Date();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前月的第一天
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getCurrtenFirstDay() {
|
|
|
+
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ // c.add(Calendar.MONTH, 0);
|
|
|
+ c.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前月的最后一天
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getCurrtenLastDay() {
|
|
|
+
|
|
|
+ Calendar ca = Calendar.getInstance();
|
|
|
+ ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(ca.getTime());
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 计算两个时间之间差的小时数(取整后)
|
|
|
+ *
|
|
|
+ * @param d1
|
|
|
+ * @param d2
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int hoursDiff(Date d1, Date d2) {
|
|
|
+ return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 60 * 1000));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前整点数
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getCurrHourTime(Date date) {
|
|
|
+ Calendar ca = Calendar.getInstance();
|
|
|
+ int minute = ca.get(Calendar.MINUTE);
|
|
|
+ if (minute < 30) {
|
|
|
+ ca.set(Calendar.MINUTE, 30);
|
|
|
+ } else {
|
|
|
+ ca.add(Calendar.HOUR, 1);
|
|
|
+ ca.set(Calendar.MINUTE, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ ca.set(Calendar.SECOND, 5);
|
|
|
+ date = ca.getTime();
|
|
|
+
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ //普通时间转为UTC
|
|
|
+ public static String localToUTC(String localTimeStr) {
|
|
|
+ try {
|
|
|
+ Date localDate = getLocalSDF().parse(localTimeStr);
|
|
|
+ return getUTCSDF().format(localDate);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ //UTC转为普通时间
|
|
|
+ public static String utcToLocal(String utcTimeStr) {
|
|
|
+ try {
|
|
|
+ Date date = getUTCSDF().parse(utcTimeStr);
|
|
|
+ return getLocalSDF().format(date);
|
|
|
+
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static SimpleDateFormat getLocalSDF() {
|
|
|
+ return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static SimpleDateFormat getUTCSDF() {
|
|
|
+ SimpleDateFormat utcSDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
|
|
+ utcSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
+ return utcSDF;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取两个时间相差分钟数
|
|
|
+ public static int getTimeDiff(String oldTime, String newTime) {
|
|
|
+ int diff = 0;
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ long NTime = 0;
|
|
|
+ long OTime = 0;
|
|
|
+ try {
|
|
|
+ NTime = df.parse(newTime).getTime();
|
|
|
+ //从对象中拿到时间
|
|
|
+ OTime = df.parse(oldTime).getTime();
|
|
|
+ diff = (int) (Math.abs((NTime-OTime))/1000/60);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ }
|
|
|
+
|
|
|
+ return diff;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取去年同一时间
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+
|
|
|
+ public static String getLastYearCurrentDate(String date, String pattern){
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(parseStrtoDate(date, pattern));
|
|
|
+ cal.add(Calendar.YEAR, -1);
|
|
|
+ return sdf.format(cal.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return 当天零点
|
|
|
+ */
|
|
|
+ public static Date today0am() {
|
|
|
+ Calendar day = Calendar.getInstance();
|
|
|
+ day.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ day.set(Calendar.MINUTE, 0);
|
|
|
+ day.set(Calendar.SECOND, 0);
|
|
|
+ day.set(Calendar.MILLISECOND, 0);
|
|
|
+ return day.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在指定时间上加指定的天数
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param day
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date addDays(Date date, int day) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.add(Calendar.DAY_OF_MONTH, day);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在指定的时间上加指定的月数
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param month
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date addMonths(Date date, int month) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.add(Calendar.MONTH, month);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在指定的时间上加指定的月数
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param year
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date addYears(Date date, int year) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.add(Calendar.YEAR, year);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在指定时间上加指定的小时
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param hour
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date addHours(Date date, int hour) {
|
|
|
+ return new Date(date.getTime() + hour * 3600 * 1000);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在指定时间上加指定的分钟
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param m
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date addMinutes(Date date, int m) {
|
|
|
+ return new Date(date.getTime() + m * 60 * 1000);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在指定时间上加指定的秒
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param s
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date addSeconds(Date date, int s) {
|
|
|
+ return new Date(date.getTime() + s * 1000);
|
|
|
+ }
|
|
|
+}
|