|
@@ -0,0 +1,763 @@
|
|
|
+package com.cslg.ppa.common.utils;
|
|
|
+
|
|
|
+
|
|
|
+import com.cslg.ppa.entity.commom.Calculate;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.lang.management.ManagementFactory;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+public class DateUtils {
|
|
|
+ public static final String START_TIME = " 00:00:00";
|
|
|
+ public static final String END_TIME = " 23:59:59";
|
|
|
+ public final static String FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ public final static String[] REPLACE_STRING = new String[]{"GMT+0800", "GMT+08:00"};
|
|
|
+ public final static String SPLIT_STRING = "(中国标准时间)";
|
|
|
+ public static Logger log = LoggerFactory.getLogger(DateUtils.class);
|
|
|
+ public static String YYYY = "yyyy";
|
|
|
+ public static String YYYY_MM = "yyyy-MM";
|
|
|
+ public static String YYYY_MM_DD = "yyyy-MM-dd";
|
|
|
+ public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
|
|
|
+ public static String YYYYMMDD = "yyyyMMdd";
|
|
|
+ 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"};
|
|
|
+
|
|
|
+// public static String getDateSourceName(Date startTime, Date endTime, Integer offset, Integer index) {
|
|
|
+// String ret = null;
|
|
|
+// switch (offset) {
|
|
|
+// //月份
|
|
|
+// case -1:
|
|
|
+// ret = DateUtil.format(startTime, "yyyy-MM");
|
|
|
+// break;
|
|
|
+// //季度
|
|
|
+// case -3:
|
|
|
+// ret = String.format("%s-Q%s", DateUtil.format(startTime, "yyyy"), (index % 4) + 1);
|
|
|
+// break;
|
|
|
+// //半年
|
|
|
+// case -6:
|
|
|
+// ret = String.format("%s-%s", DateUtil.format(startTime, "yyyy"), index % 2 == 0 ? "H1" : "H2");
|
|
|
+// break;
|
|
|
+// //1年
|
|
|
+// case -12:
|
|
|
+// ret = DateUtil.format(startTime, "yyyy");
|
|
|
+// break;
|
|
|
+// //2年,3年,5年
|
|
|
+// case -24:
|
|
|
+// case -36:
|
|
|
+// case -60:
|
|
|
+// ret = String.format("%s-%s", DateUtil.format(DateUtil.offsetMonth(endTime, offset / 12 * -1), "yyyy"), DateUtil.format(startTime, "yyyy"));
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// return ret;
|
|
|
+// }
|
|
|
+//
|
|
|
+// private DateUtils() {
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
|
|
|
+ Calendar date = Calendar.getInstance();
|
|
|
+ date.setTime(nowTime);
|
|
|
+ Calendar begin = Calendar.getInstance();
|
|
|
+ begin.setTime(beginTime);
|
|
|
+ Calendar end = Calendar.getInstance();
|
|
|
+ end.setTime(endTime);
|
|
|
+ return date.after(begin) && date.before(end);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取现在的时间 yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static String getNowTime() {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date date = new Date(System.currentTimeMillis());
|
|
|
+ return format.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前Date型日期
|
|
|
+ *
|
|
|
+ * @return Date() 当前日期
|
|
|
+ */
|
|
|
+ public static Date getNowDate() {
|
|
|
+ return new Date();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Integer getDateTime() {
|
|
|
+ return (int) (new Date().getTime() / 1000);
|
|
|
+ }
|
|
|
+
|
|
|
+// public static Integer getDateTime(String date) {
|
|
|
+// int dateTime = 0;
|
|
|
+// if (date.contains("/")) {
|
|
|
+// dateTime = Math.toIntExact(strToDateTime(date, parsePatterns[4]).getTime() / 1000);
|
|
|
+// } else if (date.contains("-")) {
|
|
|
+// dateTime = Math.toIntExact(strToDateTime(date, YYYY_MM_DD).getTime() / 1000);
|
|
|
+// }
|
|
|
+// return dateTime;
|
|
|
+// }
|
|
|
+
|
|
|
+ public static Date getDateTime(String date) {
|
|
|
+ Date dateTime = null;
|
|
|
+ if (date.contains("/")) {
|
|
|
+ dateTime = strToDateTime(date, parsePatterns[4]);
|
|
|
+ } else if (date.contains("-")) {
|
|
|
+ dateTime = strToDateTime(date, YYYY_MM_DD);
|
|
|
+ }
|
|
|
+ return dateTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @author 陌溪
|
|
|
+ * @date 2018年6月14日
|
|
|
+ */
|
|
|
+ public static String getNowTimeFormat(String format) {
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
|
|
|
+ Date date = new Date(System.currentTimeMillis());
|
|
|
+ return simpleDateFormat.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static Date str2Date(String dateString) {
|
|
|
+ try {
|
|
|
+ dateString = dateString.split(Pattern.quote(SPLIT_STRING))[0].replace(REPLACE_STRING[0], REPLACE_STRING[1]);
|
|
|
+ SimpleDateFormat sf1 = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z", Locale.US);
|
|
|
+ return sf1.parse(dateString);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("时间转化格式错误" + "[dateString=" + dateString + "]" + "[FORMAT_STRING=" + FORMAT_STRING + "]");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date strToDate(String dateString) {
|
|
|
+ try {
|
|
|
+ SimpleDateFormat sf1;
|
|
|
+ if (dateString.contains("-")) {
|
|
|
+ sf1 = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ } else if (dateString.contains("/")) {
|
|
|
+ sf1 = new SimpleDateFormat("yyyy/MM/dd");
|
|
|
+ } else if (dateString.contains(":")) {
|
|
|
+ sf1 = new SimpleDateFormat("yyyy:MM:dd");
|
|
|
+ } else if (dateString.contains(".")) {
|
|
|
+ sf1 = new SimpleDateFormat("yyyy.MM.dd");
|
|
|
+ } else {
|
|
|
+ sf1 = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ }
|
|
|
+
|
|
|
+ return sf1.parse(dateString);
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println ("时间转化格式错误" + "[dateString=" + dateString + "]" + "[FORMAT_STRING=" + FORMAT_STRING + "]");
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取今天开始的时间
|
|
|
+ */
|
|
|
+ public static String getToDayStartTime() {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
|
|
+ Date date = new Date(System.currentTimeMillis());
|
|
|
+ return format.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取今天结束的时间
|
|
|
+ */
|
|
|
+ public static String getToDayEndTime() {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
|
|
|
+ Date date = new Date(System.currentTimeMillis());
|
|
|
+ return format.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取昨天开始的时间
|
|
|
+ */
|
|
|
+ public static String getYesterdayStartTime() {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
|
|
+ Date date = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000L);
|
|
|
+ return format.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取昨天结束的时间
|
|
|
+ */
|
|
|
+ public static String getYesterdayEndTime() {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
|
|
|
+ Date date = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000L);
|
|
|
+ return format.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某天开始的时间
|
|
|
+ */
|
|
|
+ public static String getOneDayStartTime(String oneDay) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
|
|
+ Date date = new Date(oneDay);
|
|
|
+ return format.format(oneDay);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某天开始的日期
|
|
|
+ */
|
|
|
+ public static String getOneDayStartTime(Date oneDay) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
|
|
+ return format.format(oneDay);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某天结束的时间
|
|
|
+ */
|
|
|
+ public static String getOneDayEndTime(String oneDay) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
|
|
+ Date date = new Date(oneDay);
|
|
|
+ return format.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某天结束的日期
|
|
|
+ */
|
|
|
+ public static String getOneDayEndTime(Date oneDay) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
|
|
+ return format.format(oneDay);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本周开始的时间
|
|
|
+ */
|
|
|
+ public static Date getWeekStartTime() {
|
|
|
+ //获得本周一0点时间
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将 String 转换成 Date
|
|
|
+ */
|
|
|
+ public static Date strToDateTime(String dateTime) {
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ date = format.parse(dateTime);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将 String 转换成 Date (转换格式可传入)
|
|
|
+ */
|
|
|
+ public static Date strToDateTime(String dateTime, String fmt) {
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(fmt);
|
|
|
+ date = format.parse(dateTime);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将 Date 转换成时间戳
|
|
|
+ */
|
|
|
+ public static Long dateToStamp(String s) throws ParseException {
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date date = simpleDateFormat.parse(s);
|
|
|
+ return date.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将 Date 转换成 String
|
|
|
+ */
|
|
|
+ public static String dateTimeToStr(Date dateTime) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ return format.format(dateTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateTimeToStr(Date dateTime, String fmt) {
|
|
|
+
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(fmt);
|
|
|
+ return format.format(dateTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本周开始的时间的字符串
|
|
|
+ */
|
|
|
+ public static String getWeekStartTimeStr() {
|
|
|
+ //获得本周一0点时间
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
|
|
+ return format.format(cal.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本周结束的时间
|
|
|
+ */
|
|
|
+ public static Date getWeekEndTime() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(getWeekStartTime());
|
|
|
+ cal.add(Calendar.DAY_OF_WEEK, 7);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本周结束的时间的字符串
|
|
|
+ */
|
|
|
+ public static String getWeekEndTimeStr() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(getWeekStartTime());
|
|
|
+ cal.add(Calendar.DAY_OF_WEEK, 7);
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
|
|
|
+ return format.format(cal.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取上周开始的时间的字符串
|
|
|
+ */
|
|
|
+ public static String getLastWeekStartTimeStr() {
|
|
|
+ int weeks = -1;
|
|
|
+ int mondayPlus = getMondayPlus();
|
|
|
+ GregorianCalendar currentDate = new GregorianCalendar();
|
|
|
+ currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);
|
|
|
+ Date monday = currentDate.getTime();
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
|
|
+ return format.format(monday);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本月开始的时间
|
|
|
+ */
|
|
|
+ public static Date getMonthStartTime() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本月开始的时间的字符串
|
|
|
+ */
|
|
|
+ public static String getMonthStartTimeStr() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
|
|
|
+ return format.format(cal.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本月结束的时间
|
|
|
+ */
|
|
|
+ public static Date getMonthEndTime() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 24);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本月结束的时间的字符串
|
|
|
+ */
|
|
|
+ public static String getMonthEndTimeStr() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 24);
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
|
|
|
+ return format.format(cal.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当月的 天数
|
|
|
+ */
|
|
|
+ public static int getCurrentMonthDay() {
|
|
|
+ Calendar a = Calendar.getInstance();
|
|
|
+ a.set(Calendar.DATE, 1);
|
|
|
+ a.roll(Calendar.DATE, -1);
|
|
|
+ return a.get(Calendar.DATE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到二个日期间的间隔天数
|
|
|
+ */
|
|
|
+ public static int getDayByTwoDay(String date1, String date2) {
|
|
|
+ SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ long day;
|
|
|
+ try {
|
|
|
+ Date date = myFormatter.parse(date1);
|
|
|
+ Date myDate = myFormatter.parse(date2);
|
|
|
+ day = (date.getTime() - myDate.getTime()) / (24 * 60 * 60 * 1000);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return (int) day;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到两个日期相差的秒数
|
|
|
+ */
|
|
|
+ public static int getSecondByTwoDay(Date lastDate, Date date) {
|
|
|
+ long second;
|
|
|
+ try {
|
|
|
+ second = (lastDate.getTime() - date.getTime()) / 1000;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return (int) second;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断某个日期属于本周的第几天 (星期一代表第一天)
|
|
|
+ */
|
|
|
+ public static int getDaysByWeek(String dateTime) throws ParseException {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date date = dateFormat.parse(dateTime);
|
|
|
+ cal.setTime(date);
|
|
|
+ int day = cal.get(Calendar.DAY_OF_WEEK);
|
|
|
+ day = day - 1;
|
|
|
+ if (day == 0) {
|
|
|
+ day = 7;
|
|
|
+ }
|
|
|
+ return day;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断某个日期属于本月的第几天
|
|
|
+ */
|
|
|
+ public static int getDaysByMonth(String dateTime) throws ParseException {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date date = dateFormat.parse(dateTime);
|
|
|
+ cal.setTime(date);
|
|
|
+ return cal.get(Calendar.DAY_OF_MONTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据年 月 获取对应的月份 天数
|
|
|
+ */
|
|
|
+ public static int getDaysByYearMonth(int year, int month) {
|
|
|
+ Calendar a = Calendar.getInstance();
|
|
|
+ a.set(Calendar.YEAR, year);
|
|
|
+ a.set(Calendar.MONTH, month - 1);
|
|
|
+ a.set(Calendar.DATE, 1);
|
|
|
+ a.roll(Calendar.DATE, -1);
|
|
|
+ return a.get(Calendar.DATE);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前的年
|
|
|
+ */
|
|
|
+ public static Integer getYears() {
|
|
|
+ Calendar calendar = new GregorianCalendar(TimeZone
|
|
|
+ .getDefault());
|
|
|
+ return calendar.get(Calendar.YEAR);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前的月
|
|
|
+ */
|
|
|
+ public static Integer getMonth() {
|
|
|
+ Calendar calendar = new GregorianCalendar(TimeZone
|
|
|
+ .getDefault());
|
|
|
+ return calendar.get(Calendar.MONTH) + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前天
|
|
|
+ */
|
|
|
+ public static Integer getDay() {
|
|
|
+ Calendar calendar = new GregorianCalendar(TimeZone
|
|
|
+ .getDefault());
|
|
|
+ return calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * wx支付的过期时间
|
|
|
+ */
|
|
|
+ public static String getTime(double hour) {
|
|
|
+ long time = (long) (System.currentTimeMillis() + hour * 60 * 60 * 1000L);
|
|
|
+ Date date = new Date(time);
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
+ return format.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前日期与本周日相差的天数
|
|
|
+ */
|
|
|
+ private static int getMondayPlus() {
|
|
|
+ Calendar cd = Calendar.getInstance();
|
|
|
+ // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
|
|
|
+ // 因为按中国礼拜一作为第一天所以这里减1
|
|
|
+ int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
+ if (dayOfWeek == 1) {
|
|
|
+ return 0;
|
|
|
+ } else {
|
|
|
+ return 1 - dayOfWeek;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取几天之后的日期
|
|
|
+ *
|
|
|
+ * @param date yyyy-MM-dd HH:mm:ss
|
|
|
+ * @param day 加减的天数
|
|
|
+ */
|
|
|
+ public static Date getDate(String date, int day) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ Date beforeDate;
|
|
|
+ try {
|
|
|
+ beforeDate = format.parse(date);
|
|
|
+ cal.setTime(beforeDate);
|
|
|
+ cal.add(Calendar.DAY_OF_MONTH, day);
|
|
|
+ return cal.getTime();
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取几天之后的日期
|
|
|
+ *
|
|
|
+ * @param date yyyy-MM-dd HH:mm:ss
|
|
|
+ * @param day 加减的天数
|
|
|
+ */
|
|
|
+ public static Date getDate(Date date, int day) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.add(Calendar.DAY_OF_MONTH, day);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某个日期 在加上 秒数的时间
|
|
|
+ *
|
|
|
+ * @param beforeDate yyyy-MM-dd HH:mm:ss
|
|
|
+ * @param timeSecond 加减的秒数
|
|
|
+ */
|
|
|
+ public static String getDateStr(Date beforeDate, Long timeSecond) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ try {
|
|
|
+ // 返回毫秒数 + 添加的毫秒数
|
|
|
+ Long time = beforeDate.getTime() + timeSecond * 1000;
|
|
|
+ return format.format(time);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把date转换成字符串
|
|
|
+ */
|
|
|
+ public static String formatDate(Date date, String code) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(code);
|
|
|
+ return format.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatDate(Integer timestamp, String code) {
|
|
|
+ if (timestamp == null || timestamp == 0) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return formatDate(new Date(timestamp * 1000L), code);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取过去N天内的日期数组
|
|
|
+ *
|
|
|
+ * @param intervals intervals天内
|
|
|
+ * @param formatStr 格式化字符串 yyyy-MM-dd
|
|
|
+ * @return 日期数组
|
|
|
+ */
|
|
|
+ public static ArrayList<String> getDaysByN(int intervals, String formatStr) {
|
|
|
+ ArrayList<String> pastDaysList = new ArrayList<>();
|
|
|
+ for (int i = intervals - 1; i >= 0; i--) {
|
|
|
+ pastDaysList.add(getPastDate(i, formatStr));
|
|
|
+ }
|
|
|
+ return pastDaysList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取过去第几天的日期
|
|
|
+ */
|
|
|
+ public static String getPastDate(int past, String formatStr) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
|
|
|
+ Date today = calendar.getTime();
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(formatStr);
|
|
|
+ return format.format(today);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某个时间段内所有日期
|
|
|
+ */
|
|
|
+ public static List<String> getDayBetweenDates(String begin, String end) {
|
|
|
+ Date dBegin = strToDateTime(begin);
|
|
|
+ Date dEnd = strToDateTime(end);
|
|
|
+ List<String> lDate = new ArrayList<>();
|
|
|
+ SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ lDate.add(sd.format(dBegin));
|
|
|
+ Calendar calBegin = Calendar.getInstance();
|
|
|
+ // 使用给定的 Date 设置此 Calendar 的时间
|
|
|
+ calBegin.setTime(dBegin);
|
|
|
+ Calendar calEnd = Calendar.getInstance();
|
|
|
+ // 使用给定的 Date 设置此 Calendar 的时间
|
|
|
+ calEnd.setTime(dEnd);
|
|
|
+ // 测试此日期是否在指定日期之后
|
|
|
+ while (dEnd.after(calBegin.getTime())) {
|
|
|
+ // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
|
|
|
+ calBegin.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ lDate.add(sd.format(calBegin.getTime()));
|
|
|
+ }
|
|
|
+ return lDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取服务器启动时间
|
|
|
+ */
|
|
|
+ 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 diff = endDate.getTime() - nowDate.getTime();
|
|
|
+ // 计算差多少天
|
|
|
+ long day = diff / nd;
|
|
|
+ // 计算差多少小时
|
|
|
+ long hour = diff % nd / nh;
|
|
|
+ // 计算差多少分钟
|
|
|
+ long min = diff % nd % nh / nm;
|
|
|
+ return day + "天" + hour + "小时" + min + "分钟";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static long getTimeDiff(Date date) {
|
|
|
+ long NTime = date.getTime();
|
|
|
+ long OTime = getNowDate().getTime();
|
|
|
+ return (NTime - OTime) / 1000 / 60;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date setDateHourAndMinute(Date date, int hour, int minute) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, hour);
|
|
|
+ calendar.set(Calendar.MINUTE, minute);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据起止条数计算开始页数、开始页数的开始位置、结束页数、结束页数的结束位置
|
|
|
+ *
|
|
|
+ * @param startNumber 起始条数
|
|
|
+ * @param endNumber 终止条数
|
|
|
+ * @return 返回计算结果对象(开始页数、开始页数的开始位置、结束页数、结束页数的结束位置)
|
|
|
+ */
|
|
|
+ public static Calculate calculateFromStartAndEndNumber(Integer startNumber, Integer endNumber, Integer pageSize) {
|
|
|
+ int startPage; //检索开始页数
|
|
|
+ int startNum; //检索开始页数的开始专利位置
|
|
|
+ int endPage; //检索结束页数
|
|
|
+ int endNum; //检索结束页数的结束专利位置
|
|
|
+ if (startNumber % pageSize != 0) {
|
|
|
+ startPage = startNumber / pageSize;
|
|
|
+ startNum = startNumber % pageSize;
|
|
|
+ } else {
|
|
|
+ startPage = startNumber / pageSize;
|
|
|
+ startNum = pageSize;
|
|
|
+ }
|
|
|
+ if (endNumber % pageSize != 0) {
|
|
|
+ endPage = endNumber / pageSize + 1;
|
|
|
+ endNum = endNumber % pageSize;
|
|
|
+ } else {
|
|
|
+ endPage = endNumber / pageSize;
|
|
|
+ endNum = pageSize;
|
|
|
+ }
|
|
|
+
|
|
|
+ Calculate calculate = new Calculate()
|
|
|
+ .setStartPage(startPage)
|
|
|
+ .setStartNum(startNum)
|
|
|
+ .setEndPage(endPage)
|
|
|
+ .setEndNum(endNum);
|
|
|
+
|
|
|
+ return calculate;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 专利之星返回日期格式为字符串 yyyyMMdd,如 "20230713",本方法将其转成10位数字时间戳
|
|
|
+ *
|
|
|
+ * @param dateStr yyyyMMdd格式字符串日期
|
|
|
+ * @return 返回10位数字时间戳
|
|
|
+ */
|
|
|
+ public static int stringDateToTimeStamp(String dateStr) {
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ Date date;
|
|
|
+ try {
|
|
|
+ date = dateFormat.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ //日期格式转换异常
|
|
|
+ e.printStackTrace();
|
|
|
+ return Integer.parseInt(dateStr);
|
|
|
+ }
|
|
|
+ long timeStamp = date.getTime() / 1000;
|
|
|
+ return (int) timeStamp;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String strToStr(String dateString,String dateForm) {
|
|
|
+ try {
|
|
|
+ SimpleDateFormat sf1;
|
|
|
+ if (dateString.contains("-")) {
|
|
|
+ sf1 = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ } else if (dateString.contains("/")) {
|
|
|
+ sf1 = new SimpleDateFormat("yyyy/MM/dd");
|
|
|
+ } else if (dateString.contains(":")) {
|
|
|
+ sf1 = new SimpleDateFormat("yyyy:MM:dd");
|
|
|
+ } else if (dateString.contains(".")) {
|
|
|
+ sf1 = new SimpleDateFormat("yyyy.MM.dd");
|
|
|
+ } else {
|
|
|
+ sf1 = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(dateForm);
|
|
|
+ Date a= sf1.parse(dateString);
|
|
|
+ String reDate= format.format(a);
|
|
|
+ return reDate;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 获取时间
|
|
|
+ */
|
|
|
+ public static String toGMTString(Date date) {
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
|
|
|
+ df.setTimeZone(new SimpleTimeZone(0, "GMT"));
|
|
|
+ return df.format(date);
|
|
|
+ }
|
|
|
+}
|