|
@@ -0,0 +1,1293 @@
|
|
|
+package com.ruoyi.common.utils;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
+
|
|
|
+import java.sql.Time;
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.ParsePosition;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.*;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 日期工具类
|
|
|
+ *
|
|
|
+ * @author zhujt
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class DateUtil {
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 2011-01-01 01:01:00
|
|
|
+ */
|
|
|
+ private static SimpleDateFormat fullSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ private static SimpleDateFormat fullSD = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ /**
|
|
|
+ * Get full time string, like 2011-01-01 01:01:00
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getCurrentTime() {
|
|
|
+ return fullSDF.format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurrentDate() {
|
|
|
+ return fullSD.format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ static long now;
|
|
|
+ public static final String FORMAT_TIME = "yyyy-MM-dd HH:mm:ss"; // 默认时间格式
|
|
|
+
|
|
|
+ public static final String FORMAT_TIME_MINUTE = "yyyy-MM-dd HH:mm"; // 默认时间格式
|
|
|
+
|
|
|
+ public static final String FORMAT_DATE = "yyyy-MM-dd"; // 默认日期格式
|
|
|
+
|
|
|
+ public static final String FORMAT_DTIME = "yyyyMMdd"; // 数据库日期格式
|
|
|
+
|
|
|
+ public static final String FORMAT_MONTH = "yyyy-MM"; // 默认月份格式
|
|
|
+
|
|
|
+ public static final String FORMAT_YEAR = "yyyy"; // 默认年格式
|
|
|
+
|
|
|
+ public static final String FORMAT_DATE2 = "yyyy/MM/dd"; //
|
|
|
+
|
|
|
+ public static Date CurrTime;
|
|
|
+ public final static DateFormat FullFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ public final static DateFormat MinuteFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
+ public final static DateFormat HourFormat = new SimpleDateFormat("yyyy-MM-dd HH");
|
|
|
+ public final static DateFormat DayFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ public final static DateFormat MonthFormat = new SimpleDateFormat("yyyy-MM");
|
|
|
+ public final static DateFormat YearFormat = new SimpleDateFormat("yyyy");
|
|
|
+ public final static DateFormat HourNumFormat = new SimpleDateFormat("yyyyMMddHH");
|
|
|
+
|
|
|
+ public final static String FULL_FORMAT_REGEX = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$";
|
|
|
+ public final static String HOUR_FORMAT_REGEX = "^\\d{4}-\\d{2}-\\d{2} \\d{2}$";
|
|
|
+ public final static String DAY_FORMAT_REGEX = "^\\d{4}-\\d{2}-\\d{2}$";
|
|
|
+ public final static String MONTH_FORMAT_REGEX = "^\\d{4}-\\d{2}";
|
|
|
+ public final static String YEAR_FORMAT_REGEX = "^\\d{4}$";
|
|
|
+
|
|
|
+ public final static String YEARTOHOUR_FORMAT_REGEX = "^\\d{4}-\\d{2}-\\d{2}\\d{2}$";
|
|
|
+ public final static String YEARTOMONTH_FORMAT_REGEX = "^\\d{4}\\d{2}$";
|
|
|
+ public final static String QUARTER_FORMAT_REGEX = "^\\d{4}Q\\d{1}$";
|
|
|
+ public final static int FULL_FORMAT = 0;
|
|
|
+ public final static int HOUR_FORMAT = 1;
|
|
|
+ public final static int DAY_FORMAT = 2;
|
|
|
+ public final static int MONTH_FORMAT = 3;
|
|
|
+ public final static int YEAR_FORMAT = 4;
|
|
|
+
|
|
|
+ public static String getLongToString(Long longtime) {
|
|
|
+ if (longtime == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return fullSDF.format(new Date(longtime));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getTimestampToString(Timestamp time) {
|
|
|
+ return fullSDF.format(time);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurrTime() {
|
|
|
+ Date date_time = new Date();
|
|
|
+ return FormatDate(date_time, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurrDetailTime() {
|
|
|
+ Date date_time = new Date();
|
|
|
+ return FormatDate(date_time, "yyyy-MM-dd HH-mm-ss");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurrYY() {
|
|
|
+ Date date_time = new Date();
|
|
|
+ String date = FormatDate(date_time, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ return date.substring(0, 4);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurrMM() {
|
|
|
+ Date date_time = new Date();
|
|
|
+ String date = FormatDate(date_time, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ return date.substring(5, 7);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurrDD() {
|
|
|
+ Date date_time = new Date();
|
|
|
+ String date = FormatDate(date_time, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ return date.substring(8, 10);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurrHH() {
|
|
|
+ Date date_time = new Date();
|
|
|
+ String date = FormatDate(date_time, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ return date.substring(11, 13);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getYear(Date date) {
|
|
|
+ if (date == null)
|
|
|
+ return "";
|
|
|
+ else
|
|
|
+ return FormatDate(date, "yyyy");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getMonth(Date date) {
|
|
|
+ if (date == null)
|
|
|
+ return "";
|
|
|
+ else
|
|
|
+ return FormatDate(date, "MM");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateToday_year(Date date) {
|
|
|
+ if (date == null)
|
|
|
+ return "";
|
|
|
+ else
|
|
|
+ return FormatDate(date, "yyyy");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateToday(Date date) {
|
|
|
+ if (date == null)
|
|
|
+ return "";
|
|
|
+ else
|
|
|
+ return FormatDate(date, "yyyy-MM-dd");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getDay(Date date) {
|
|
|
+ if (date == null)
|
|
|
+ return "";
|
|
|
+ else
|
|
|
+ return FormatDate(date, "dd");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getHour(Date date) {
|
|
|
+ if (date == null)
|
|
|
+ return "";
|
|
|
+ else
|
|
|
+ return FormatDate(date, "HH");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getMinute(Date date) {
|
|
|
+ if (date == null)
|
|
|
+ return "";
|
|
|
+ else
|
|
|
+ return FormatDate(date, "mm");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getSecond(Date date) {
|
|
|
+ if (date == null)
|
|
|
+ return "";
|
|
|
+ else
|
|
|
+ return FormatDate(date, "ss");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateToString(Date date) {
|
|
|
+ if (date == null)
|
|
|
+ return "";
|
|
|
+ else
|
|
|
+ return FormatDate(date, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateToStringShort(Date date) {
|
|
|
+ if (date == null)
|
|
|
+ return "";
|
|
|
+ else
|
|
|
+ return FormatDate(date, "yyyy-MM-dd");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String ToString(Date date) {
|
|
|
+ if (date == null)
|
|
|
+ return "";
|
|
|
+ else
|
|
|
+ return FormatDate(date, "yyyy-MM-dd") + " 23:59:59";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date addTime(Date date, int field, int amount) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(field, amount);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析日期yyyy-MM-dd HH:mm:ss
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date parse(String date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date stringToDate(String dateString) {
|
|
|
+ String sf = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ Date dt = new Date();
|
|
|
+ try {
|
|
|
+ dt = stringToDate(dateString, sf);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date stringToDateShort(String dateString) {
|
|
|
+ if (dateString == null || dateString.trim().equals("")) {
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ String sf = "yyyy-MM-dd";
|
|
|
+ Date dt = stringToDate(dateString, sf);
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String FormatDate(Date date, String sf) {
|
|
|
+ if (date == null) {
|
|
|
+ return "";
|
|
|
+ } else {
|
|
|
+ SimpleDateFormat dateformat = new SimpleDateFormat(sf);
|
|
|
+ return dateformat.format(date);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getDateFormatStr(String formart) {
|
|
|
+ return new SimpleDateFormat(formart).format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date stringToDate(String dateString, String sf) {
|
|
|
+ // ParsePosition pos = new ParsePosition(0);
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(sf);
|
|
|
+ try {
|
|
|
+ Date dt = sdf.parse(dateString);
|
|
|
+ sdf.format(dt);
|
|
|
+ return dt;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date stringToDateAddOneDay(String dateString, String sf) {
|
|
|
+ ParsePosition pos = new ParsePosition(0);
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(sf);
|
|
|
+ Date dt = sdf.parse(dateString, pos);
|
|
|
+
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(dt);
|
|
|
+ cal.add(Calendar.DATE, 1);
|
|
|
+
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date addAnyDay(Date date, int dayCount) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.add(Calendar.DATE, dayCount);
|
|
|
+
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date addAnyMonth(Date date, int monthCount) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.add(Calendar.MONTH, monthCount);
|
|
|
+
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static long diffTwoDate(Date date1, Date date2) {
|
|
|
+ long l1 = date1.getTime();
|
|
|
+ long l2 = date2.getTime();
|
|
|
+ return l1 - l2;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getDateChangeTime(String currentTime, String type, int iQuantity) {
|
|
|
+ Date curr = stringToDate(currentTime);
|
|
|
+ curr = getDateChangeTime(curr, type, iQuantity);
|
|
|
+ return dateToString(curr);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int whichFormat(String time) {
|
|
|
+ if (time.matches(HOUR_FORMAT_REGEX)) {
|
|
|
+ return HOUR_FORMAT;
|
|
|
+ } else if (time.matches(DAY_FORMAT_REGEX)) {
|
|
|
+ return DAY_FORMAT;
|
|
|
+ } else if (time.matches(MONTH_FORMAT_REGEX)) {
|
|
|
+ return MONTH_FORMAT;
|
|
|
+ } else if (time.matches(YEAR_FORMAT_REGEX)) {
|
|
|
+ return YEAR_FORMAT;
|
|
|
+ } else {
|
|
|
+ return FULL_FORMAT;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getFirstTimeOfMonth(Date time) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(time);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getFirstTimeOfDay(Date time) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(time);
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+// public static void main(String[] args) {
|
|
|
+// System.out.println(getFirstTimeOfDay(new Date()));
|
|
|
+// System.out.println(getLastTimeOfDay(new Date()));
|
|
|
+// System.out.println(HourNumFormat.format(getFirstTimeOfDay(new Date())));
|
|
|
+// System.out.println(HourNumFormat.format(getLastTimeOfDay(new Date())));
|
|
|
+// String format = HourNumFormat.format(getFirstTimeOfDay(new Date()));
|
|
|
+// String format2 = HourNumFormat.format(getLastTimeOfDay(new Date()));
|
|
|
+// int a = Integer.valueOf(format);
|
|
|
+// int b = Integer.valueOf(format2);
|
|
|
+// System.out.println(b - a);
|
|
|
+// }
|
|
|
+
|
|
|
+ public static Date getNextHour(Date startDate) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(startDate);
|
|
|
+ cal.add(Calendar.HOUR_OF_DAY, 1);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据指定日期,延后addHour小时
|
|
|
+ * @param startDate
|
|
|
+ * @param addHour
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getNextHour(Date startDate,int addHour) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(startDate);
|
|
|
+ cal.add(Calendar.HOUR_OF_DAY, addHour);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean compareDateByHour(Date endDate, Date nextDate) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(endDate);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ Calendar nextCalendar = Calendar.getInstance();
|
|
|
+ nextCalendar.setTime(nextDate);
|
|
|
+ nextCalendar.set(Calendar.SECOND, 0);
|
|
|
+ nextCalendar.set(Calendar.MINUTE, 0);
|
|
|
+ if (calendar.compareTo(nextCalendar) >= 0) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getLastTimeOfDay(Date time) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(getFirstTimeOfDay(time));
|
|
|
+ cal.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
+ cal.add(Calendar.SECOND, -1);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getFirstTimeOfHour(Date time) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(time);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getLastTimeOfHour(Date time) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(getFirstTimeOfHour(time));
|
|
|
+ cal.add(Calendar.HOUR_OF_DAY, 1);
|
|
|
+ cal.add(Calendar.SECOND, -1);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getLastTimeOfMonth(Date time) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(getFirstTimeOfMonth(time));
|
|
|
+ cal.add(Calendar.MONTH, 1);
|
|
|
+ cal.add(Calendar.SECOND, -1);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getDateChangeTime(Date currentTime, String type, int iQuantity) {
|
|
|
+ int year = Integer.parseInt(FormatDate(currentTime, "yyyy"));
|
|
|
+ int month = Integer.parseInt(FormatDate(currentTime, "MM"));
|
|
|
+ int day = Integer.parseInt(FormatDate(currentTime, "dd"));
|
|
|
+ int hour = Integer.parseInt(FormatDate(currentTime, "HH"));
|
|
|
+ int mi = Integer.parseInt(FormatDate(currentTime, "mm"));
|
|
|
+ int ss = Integer.parseInt(FormatDate(currentTime, "ss"));
|
|
|
+ GregorianCalendar gc = new GregorianCalendar(year, month, day, hour, mi, ss);
|
|
|
+ if (type.equalsIgnoreCase("y"))
|
|
|
+ gc.add(1, iQuantity);
|
|
|
+ else if (type.equalsIgnoreCase("m"))
|
|
|
+ gc.add(2, iQuantity);
|
|
|
+ else if (type.equalsIgnoreCase("d"))
|
|
|
+ gc.add(5, iQuantity);
|
|
|
+ else if (type.equalsIgnoreCase("h"))
|
|
|
+ gc.add(10, iQuantity);
|
|
|
+ else if (type.equalsIgnoreCase("mi"))
|
|
|
+ gc.add(12, iQuantity);
|
|
|
+ else if (type.equalsIgnoreCase("s"))
|
|
|
+ gc.add(13, iQuantity);
|
|
|
+ gc.add(2, -1);
|
|
|
+ return gc.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date convertStringToDate(String str, String pattern) {
|
|
|
+ Date date = null;
|
|
|
+
|
|
|
+ if (str.equals("") || str == null)
|
|
|
+ return date;
|
|
|
+
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(pattern);
|
|
|
+ try {
|
|
|
+ date = formatter.parse(str);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ static {
|
|
|
+ now = System.currentTimeMillis();
|
|
|
+ CurrTime = new Date(now);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回日期字符串("yyyy-mm")的年
|
|
|
+ public static int getYear(String s) {
|
|
|
+ if (s == null || s.length() < 4) {
|
|
|
+ return 1970;
|
|
|
+ }
|
|
|
+ return Integer.parseInt(s.substring(0, 4));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回日期字符串("yyyy-mm")的月
|
|
|
+ public static int getMonth(String s) {
|
|
|
+ if (s == null || s.length() < 7) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ return Integer.parseInt(s.substring(5, 7));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 指定年月的最后一天的日期 yyyy-mm
|
|
|
+ public static String getDateOfLastDay(String date, String format) {
|
|
|
+ int year = getYear(date);
|
|
|
+ int month = getMonth(date);
|
|
|
+ int day = 1;
|
|
|
+ Calendar calendar = new GregorianCalendar();
|
|
|
+ calendar.set(year, month, day);
|
|
|
+ calendar.roll(Calendar.MONDAY, -1);
|
|
|
+ calendar.roll(Calendar.DATE, 0 - calendar.get(Calendar.DATE));
|
|
|
+ calendar.set(Calendar.HOUR, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ return sdf.format(calendar.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 指定年月的第一天的日期 yyyy-mm
|
|
|
+ public static String getDateOfFirstDay(String date) {
|
|
|
+ if (date == null || date.trim().length() <= 0) {
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+ String[] dateStrs = date.split("-");
|
|
|
+ if (dateStrs == null || dateStrs.length != 2) {
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+ return date + "-" + "01";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Time getSqlTime(String str) {
|
|
|
+ try {
|
|
|
+ return new Time(fullSDF.parse(str).getTime());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Timestamp getSqlTimeStamp(String str) {
|
|
|
+ try {
|
|
|
+ return new Timestamp(fullSDF.parse(str).getTime());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static long getDWMStartTime(String type) {
|
|
|
+ Long startTime = null;
|
|
|
+ if (type.equals("D")) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);
|
|
|
+ startTime = cal.getTimeInMillis();
|
|
|
+ } else if (type.equals("W")) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
|
|
|
+ cal.add(Calendar.WEEK_OF_YEAR, -1);
|
|
|
+ }
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
|
|
+ startTime = cal.getTimeInMillis();
|
|
|
+ } else if (type.equals("M")) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
|
|
|
+ startTime = cal.getTimeInMillis();
|
|
|
+ }
|
|
|
+ // System.out.println(new Timestamp(startTime));
|
|
|
+ return startTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getDWMStartTimeToString(String type) {
|
|
|
+ return DayFormat.format(new Date(getDWMStartTime(type)));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String[] getDateStringInWorM(String type) {
|
|
|
+ List<String> dates = new ArrayList<String>();
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 0,
|
|
|
+ 0, 0);
|
|
|
+ if (type.equals("M")) {
|
|
|
+ calendar.set(Calendar.DATE, 1);
|
|
|
+ int month = calendar.get(Calendar.MONTH);
|
|
|
+ while (calendar.get(Calendar.MONTH) == month) {
|
|
|
+ dates.add(DayFormat.format(calendar.getTime()));
|
|
|
+ calendar.add(Calendar.DATE, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (type.equals("W")) {
|
|
|
+ if (calendar.get(Calendar.DAY_OF_WEEK) == 1) {
|
|
|
+ calendar.add(Calendar.WEEK_OF_YEAR, -1);
|
|
|
+ }
|
|
|
+ int week = calendar.get(Calendar.WEEK_OF_YEAR);
|
|
|
+ // System.out.println("week="+week);
|
|
|
+ calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
|
|
+ // 取完整的一周
|
|
|
+ while (calendar.get(Calendar.WEEK_OF_YEAR) == week
|
|
|
+ || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
|
|
|
+ dates.add(DayFormat.format(calendar.getTime()));
|
|
|
+ calendar.add(Calendar.DATE, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (type.equals("D")) {
|
|
|
+ dates.add(DayFormat.format(calendar.getTime()));
|
|
|
+ }
|
|
|
+ return dates.toArray(new String[dates.size()]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String[] getDateStringForMon(int year, int mon) {
|
|
|
+ List<String> dates = new ArrayList<String>();
|
|
|
+ try {
|
|
|
+ int i = 1;
|
|
|
+ Calendar a = Calendar.getInstance();
|
|
|
+ a.set(Calendar.YEAR, year);
|
|
|
+ a.set(Calendar.MONTH, mon - 1);
|
|
|
+ a.set(Calendar.DATE, 1);
|
|
|
+ a.roll(Calendar.DATE, -1);
|
|
|
+ int maxDate = a.get(Calendar.DATE);
|
|
|
+
|
|
|
+ while (i <= maxDate) {
|
|
|
+ dates.add(DayFormat.format(stringToDate(year + "-" + mon + "-" + i, "yyyy-MM-dd")));
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return dates.toArray(new String[dates.size()]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 两个时间之间相差距离多少天
|
|
|
+ *
|
|
|
+ * @param str1
|
|
|
+ * 时间参数 1:
|
|
|
+ * @param str2
|
|
|
+ * 时间参数 2:
|
|
|
+ * @return 相差天数
|
|
|
+ */
|
|
|
+ public static long getDistanceDays(String str1, String str2) {
|
|
|
+ DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date one;
|
|
|
+ Date two;
|
|
|
+ long days = 0;
|
|
|
+ try {
|
|
|
+ one = df.parse(str1);
|
|
|
+ two = df.parse(str2);
|
|
|
+ long time1 = one.getTime();
|
|
|
+ long time2 = two.getTime();
|
|
|
+ long diff;
|
|
|
+ if (time1 < time2) {
|
|
|
+ diff = time2 - time1;
|
|
|
+ } else {
|
|
|
+ diff = time1 - time2;
|
|
|
+ }
|
|
|
+ days = diff / (1000 * 60 * 60 * 24);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return days;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 两个时间相差距离多少天多少小时多少分多少秒
|
|
|
+ *
|
|
|
+ * @param str1
|
|
|
+ * 时间参数 1 格式:1990-01-01 12:00:00
|
|
|
+ * @param str2
|
|
|
+ * 时间参数 2 格式:2009-01-01 12:00:00
|
|
|
+ * @return long[] 返回值为:{天, 时, 分, 秒}
|
|
|
+ */
|
|
|
+ public static long[] getDistanceTimes(String str1, String str2) {
|
|
|
+ DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date one;
|
|
|
+ Date two;
|
|
|
+ long day = 0;
|
|
|
+ long hour = 0;
|
|
|
+ long min = 0;
|
|
|
+ long sec = 0;
|
|
|
+ try {
|
|
|
+ one = df.parse(str1);
|
|
|
+ two = df.parse(str2);
|
|
|
+ long time1 = one.getTime();
|
|
|
+ long time2 = two.getTime();
|
|
|
+ long diff;
|
|
|
+ if (time1 < time2) {
|
|
|
+ diff = time2 - time1;
|
|
|
+ } else {
|
|
|
+ diff = time1 - time2;
|
|
|
+ }
|
|
|
+ day = diff / (24 * 60 * 60 * 1000);
|
|
|
+ hour = (diff / (60 * 60 * 1000) - day * 24);
|
|
|
+ min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
|
|
|
+ sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ long[] times = { day, hour, min, sec };
|
|
|
+ return times;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 两个时间相差距离多少天多少小时多少分多少秒
|
|
|
+ *
|
|
|
+ * @param str1
|
|
|
+ * 时间参数 1 格式:1990-01-01 12:00:00
|
|
|
+ * @param str2
|
|
|
+ * 时间参数 2 格式:2009-01-01 12:00:00
|
|
|
+ * @return String 返回值为:xx天xx小时xx分xx秒
|
|
|
+ */
|
|
|
+ public static String getDistanceTime(String str1, String str2) {
|
|
|
+ DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date one;
|
|
|
+ Date two;
|
|
|
+ long day = 0;
|
|
|
+ long hour = 0;
|
|
|
+ long min = 0;
|
|
|
+ long sec = 0;
|
|
|
+ try {
|
|
|
+ one = df.parse(str1);
|
|
|
+ two = df.parse(str2);
|
|
|
+ long time1 = one.getTime();
|
|
|
+ long time2 = two.getTime();
|
|
|
+ long diff;
|
|
|
+ if (time1 < time2) {
|
|
|
+ diff = time2 - time1;
|
|
|
+ } else {
|
|
|
+ diff = time1 - time2;
|
|
|
+ }
|
|
|
+ day = diff / (24 * 60 * 60 * 1000);
|
|
|
+ hour = (diff / (60 * 60 * 1000) - day * 24);
|
|
|
+ min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
|
|
|
+ sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return day + "天" + hour + "小时" + min + "分" + sec + "秒";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getDistanceTime(long time1, long time2) {
|
|
|
+ long day = 0;
|
|
|
+ long hour = 0;
|
|
|
+ long min = 0;
|
|
|
+ long sec = 0;
|
|
|
+ long diff = 0;
|
|
|
+ if (time1 < time2) {
|
|
|
+ diff = time2 - time1;
|
|
|
+ } else {
|
|
|
+ diff = time1 - time2;
|
|
|
+ }
|
|
|
+ day = diff / (24 * 60 * 60 * 1000);
|
|
|
+ hour = (diff / (60 * 60 * 1000) - day * 24);
|
|
|
+ min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
|
|
|
+ sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
|
|
|
+ return day + "天" + hour + "小时" + min + "分" + sec + "秒";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static long getDistanceTime(String str1) throws Exception {
|
|
|
+ DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date one;
|
|
|
+ long time = 0;
|
|
|
+ try {
|
|
|
+ one = df.parse(str1);
|
|
|
+ time = one.getTime();
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return time;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取得当前日期所在周的第一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getFirstDayOfWeek(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setFirstDayOfWeek(Calendar.SUNDAY);
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek()); // Sunday
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取得当前日期所在周的最后一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getLastDayOfWeek(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setFirstDayOfWeek(Calendar.SUNDAY);
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6); // Saturday
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *Nov 14, 2019 4:13:31 PM 格式转换方法
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String parseDate(String dates) throws ParseException {
|
|
|
+ SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String newDate = "MMM dd, yyyy hh:mm:ss aa";
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat(newDate, Locale.ENGLISH);
|
|
|
+ Date formatDate= dateFormat.parse(dates);
|
|
|
+ String hdate = date.format(formatDate);
|
|
|
+ return hdate;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断传入的时间与type需对应的时间是否匹配
|
|
|
+ * @param time
|
|
|
+ * @param type
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Boolean timeMatchType(String time, Integer type){
|
|
|
+ Boolean b = true;
|
|
|
+ if (StringUtils.isNotBlank(time)) {
|
|
|
+ //1.H 2.D 3.W 4.M 5.Q 6.Y 7.A
|
|
|
+ switch (type) {
|
|
|
+ case 1:
|
|
|
+ b = time.matches(YEARTOHOUR_FORMAT_REGEX);
|
|
|
+ break;
|
|
|
+ case 2: case 3: case 7:
|
|
|
+ b = time.matches(DAY_FORMAT_REGEX);
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ b = time.matches(YEARTOMONTH_FORMAT_REGEX);
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ b = time.matches(QUARTER_FORMAT_REGEX);
|
|
|
+ break;
|
|
|
+ case 6:
|
|
|
+ b = time.matches(YEAR_FORMAT_REGEX);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析日期
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @param pattern 日期格式
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date parse(String date, String pattern) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Date resultDate = null;
|
|
|
+ try {
|
|
|
+ resultDate = new SimpleDateFormat(pattern).parse(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ return resultDate;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 根据日期类型type得到开始时间
|
|
|
+ * @param starttime
|
|
|
+ * @param type
|
|
|
+ * @return
|
|
|
+ * @author wanghw
|
|
|
+ */
|
|
|
+ public static String setStarttime(String starttime, int type) {
|
|
|
+ if(StringUtils.isNotBlank(starttime)) {
|
|
|
+ starttime = starttime.replace("-","");
|
|
|
+ if(type==7) {
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date=formatter.parse(starttime);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ c.setTime(date);
|
|
|
+ //得到前一天日期
|
|
|
+ int day = c.get(Calendar.DATE);
|
|
|
+ c.set(Calendar.DATE, day - 1);
|
|
|
+ starttime = formatter.format(c.getTime());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return starttime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据日期类型type得到结束时间
|
|
|
+ * @param endtime
|
|
|
+ * @param type
|
|
|
+ * @return
|
|
|
+ * @author wanghw
|
|
|
+ */
|
|
|
+ public static String setEndtime(String endtime, int type) {
|
|
|
+ SimpleDateFormat sdf;
|
|
|
+ Date date=new Date();
|
|
|
+ String time = null ;
|
|
|
+ if(StringUtils.isBlank(endtime)) {
|
|
|
+ //1.H 2.D 3.W 4.M 5.Q 6.Y 7.A
|
|
|
+ switch (type){
|
|
|
+ case 1:
|
|
|
+ sdf = new SimpleDateFormat("yyyyMMddHH");
|
|
|
+ time = sdf.format(date);
|
|
|
+ break;
|
|
|
+ case 2: case 3: case 7:
|
|
|
+ sdf = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ time = sdf.format(date);
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ sdf = new SimpleDateFormat("yyyyMM");
|
|
|
+ time = sdf.format(date);
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ time = getQn();
|
|
|
+ break;
|
|
|
+ case 6:
|
|
|
+ sdf = new SimpleDateFormat("yyyy");
|
|
|
+ time = sdf.format(date);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ time = endtime.replace("-","");
|
|
|
+ SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ if(type==7 && Integer.parseInt(time) > Integer.parseInt(sdf1.format(date))) {
|
|
|
+ time = sdf1.format(date);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return time;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 得到当前日期所在季度
|
|
|
+ * @return YYYYQn
|
|
|
+ * @author wanghw
|
|
|
+ */
|
|
|
+ /**
|
|
|
+ * 得到当前日期所在季度
|
|
|
+ * @return YYYYQn
|
|
|
+ * @author wanghw
|
|
|
+ */
|
|
|
+ public static String getQn() {
|
|
|
+ Date dt = new Date();
|
|
|
+ SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
|
|
|
+ SimpleDateFormat sdf2 = new SimpleDateFormat("M");
|
|
|
+ String year = sdf1.format(dt);
|
|
|
+ String m = sdf2.format(dt);
|
|
|
+ int Qn = Integer.parseInt(m);
|
|
|
+ String endtime = null;
|
|
|
+ if(1<=Qn && Qn<=3) {
|
|
|
+ endtime = year + "Q1";
|
|
|
+ }else if(4<=Qn && Qn<=6) {
|
|
|
+ endtime = year + "Q2";
|
|
|
+ }else if(7<=Qn && Qn<=9) {
|
|
|
+ endtime = year + "Q3";
|
|
|
+ }else{
|
|
|
+ endtime = year + "Q4";
|
|
|
+ }
|
|
|
+ return endtime;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取给定日期yyyyMMdd所在周的周一的日期
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ * @author wanghw
|
|
|
+ */
|
|
|
+ public static String getMonday(String str) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = format.parse(str);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ cal.setTime(date);
|
|
|
+ //cal.setFirstDayOfWeek(Calendar.MONDAY);
|
|
|
+ cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
|
|
|
+ return format.format(cal.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定类型时间点时间
|
|
|
+ * @param dtime
|
|
|
+ * @param type
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String setDtime(String dtime, int type) {
|
|
|
+ SimpleDateFormat sdf;
|
|
|
+ Date dt = new Date();//yyyy-MM-dd HH:mm:ss
|
|
|
+ if(StringUtils.isBlank(dtime)) {
|
|
|
+ //1.H 2.D 3.W 4.M 5.Q 6.Y 7.A
|
|
|
+ switch (type){
|
|
|
+ case 1:
|
|
|
+ sdf = new SimpleDateFormat("yyyyMMddHH");
|
|
|
+ dtime = sdf.format(dt);
|
|
|
+ break;
|
|
|
+ case 2: case 7:
|
|
|
+ sdf = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ dtime = sdf.format(dt);
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ sdf = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ dtime = getMonday(sdf.format(dt));
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ sdf = new SimpleDateFormat("yyyyMM");
|
|
|
+ dtime = sdf.format(dt);
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ dtime = getQn();
|
|
|
+ break;
|
|
|
+ case 6:
|
|
|
+ sdf = new SimpleDateFormat("yyyy");
|
|
|
+ dtime = sdf.format(dt);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ dtime = dtime.replace("-","");
|
|
|
+ }
|
|
|
+ return dtime;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 解析日期 yyyy-MM-dd HH:mm:ss 为 Long
|
|
|
+ *
|
|
|
+ * @param dateStr
|
|
|
+ * @return
|
|
|
+ * @author wanghw
|
|
|
+ */
|
|
|
+ public static Long format(String dateStr) {
|
|
|
+ Long timestamp = 0L;
|
|
|
+ if (null == dateStr || dateStr.length() < 0) {
|
|
|
+ return timestamp;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME);
|
|
|
+ Date date = format.parse(dateStr);
|
|
|
+ timestamp = date.getTime();
|
|
|
+ } catch (Exception e) {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
+ return timestamp;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return
|
|
|
+ * @Author:wangming
|
|
|
+ * @Description:TODO(字符串时间转时间戳格式)
|
|
|
+ * @Date 13:59 2019/12/7
|
|
|
+ * @Param
|
|
|
+ */
|
|
|
+ public static Long stringDataConvertLong(String stringData) {
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
|
|
|
+ try {
|
|
|
+ Date parse = simpleDateFormat.parse(stringData);
|
|
|
+ return parse.getTime();
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析日期 yyyy-MM-dd HH:mm:ss
|
|
|
+ *
|
|
|
+ * @param timestamp
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String format(Long timestamp) {
|
|
|
+ String dateStr = "";
|
|
|
+ if (null == timestamp || timestamp.longValue() < 0) {
|
|
|
+ return dateStr;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Date date = new Date(timestamp);
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME);
|
|
|
+ dateStr = format.format(date);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
+
|
|
|
+ return dateStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析日期 yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @param timestamp
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String formatYMD(Long timestamp) {
|
|
|
+ String dateStr = "";
|
|
|
+ if (null == timestamp || timestamp.longValue() <= 0) {
|
|
|
+ return dateStr;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Date date = new Date(timestamp);
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(FORMAT_DATE);
|
|
|
+ dateStr = format.format(date);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
+
|
|
|
+ return dateStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析日期 yyyy-MM-dd HH:mm:ss
|
|
|
+ *
|
|
|
+ * @param timestamp
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String format(Long timestamp, String pattern) {
|
|
|
+ String dateStr = "";
|
|
|
+ if (null == timestamp || timestamp.longValue() < 0) {
|
|
|
+ return dateStr;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Date date = new Date(timestamp);
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(pattern);
|
|
|
+ dateStr = format.format(date);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
+
|
|
|
+ return dateStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前时间前几天时间,按指定格式返回
|
|
|
+ *
|
|
|
+ * @param days
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String forwardDay(Integer days, String format) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ Date date = new Date();
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, -days);
|
|
|
+ date = calendar.getTime();
|
|
|
+ return sdf.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定时间前几天时间,按指定格式返回
|
|
|
+ *
|
|
|
+ * @param days
|
|
|
+ * @return
|
|
|
+ * @author wanghw
|
|
|
+ */
|
|
|
+ public static String forwardDay(Integer days, String dates, String format) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dates);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ //e.printStackTrace();
|
|
|
+ }
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, -days);
|
|
|
+ date = calendar.getTime();
|
|
|
+ return sdf.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据开始日期和结束日期,得到之间的每一天,并倒序
|
|
|
+ * @param startTime
|
|
|
+ * @param endTime
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<String> getDescDays(String startTime, String endTime) {
|
|
|
+ // 返回的日期集合
|
|
|
+ List<String> days = new ArrayList<String>();
|
|
|
+ DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ try {
|
|
|
+ Date start = dateFormat.parse(startTime);
|
|
|
+ Date end = dateFormat.parse(endTime);
|
|
|
+
|
|
|
+ Calendar tempStart = Calendar.getInstance();
|
|
|
+ tempStart.setTime(start);
|
|
|
+
|
|
|
+ Calendar tempEnd = Calendar.getInstance();
|
|
|
+ tempEnd.setTime(end);
|
|
|
+ tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
|
|
|
+ while (tempStart.before(tempEnd)) {
|
|
|
+ days.add(dateFormat.format(tempStart.getTime()));
|
|
|
+ tempStart.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ Collections.reverse(days);
|
|
|
+ return days;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间字符串转为时间戳
|
|
|
+ *
|
|
|
+ * @param time 时间字符串
|
|
|
+ * @param pattern 时间格式
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static long timeStringToTimestamp(String time, String pattern) {
|
|
|
+ LocalDateTime localDateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern(pattern));
|
|
|
+ return localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期字符串转为时间戳
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @param pattern 日期格式
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static long dateStringToTimestamp(String date, String pattern) {
|
|
|
+ LocalDateTime localDateTime = LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern)).atStartOfDay();
|
|
|
+ return localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间戳转字符串
|
|
|
+ *
|
|
|
+ * @param timestamp 时间戳
|
|
|
+ * @param pattern 时间格式
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String timestampToTimeString(long timestamp, String pattern) {
|
|
|
+ DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
|
|
|
+ return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()).format(dateTimeFormatter);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Author:wangming
|
|
|
+ * @Description:TODO(获取某个月份第一天开始时间戳)
|
|
|
+ * @Date 16:44 2020/10/26
|
|
|
+ * @Param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getStartTime(String time){
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = df.parse(time);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ c.add(Calendar.MONTH, 0); //获取当前月第一天
|
|
|
+ c.set(Calendar.DAY_OF_MONTH, 1); //设置为1号,当前日期既为本月第一天
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0); //将小时至0
|
|
|
+ c.set(Calendar.MINUTE, 0); //将分钟至0
|
|
|
+ c.set(Calendar.SECOND,0); //将秒至0
|
|
|
+ c.set(Calendar.MILLISECOND, 0); //将毫秒至0
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String startDate = sdf.format(c.getTime()); //月份开始时间
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Author:wangming
|
|
|
+ * @Description:TODO(获取某月最后一天的结束时间戳)
|
|
|
+ * @Date 16:47 2020/10/26
|
|
|
+ * @Param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getEndTime(String time){
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = df.parse(time);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ Calendar c2 = Calendar.getInstance();
|
|
|
+ c2.setTime(date);
|
|
|
+ c2.set(Calendar.DAY_OF_MONTH, c2.getActualMaximum(Calendar.DAY_OF_MONTH)); //获取当前月最后一天
|
|
|
+ c2.set(Calendar.HOUR_OF_DAY, 23); //将小时至23
|
|
|
+ c2.set(Calendar.MINUTE, 59); //将分钟至59
|
|
|
+ c2.set(Calendar.SECOND,59); //将秒至59
|
|
|
+ c2.set(Calendar.MILLISECOND, 999); //将毫秒至999
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String endDate = sdf.format(c2.getTime()); //结束时间字符串
|
|
|
+ return c2.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|