|
@@ -0,0 +1,321 @@
|
|
|
+package com.huimv.business.utils;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Project : huimv.shiwan
|
|
|
+ * @Package : com.huimv.biosafety.uface.controller
|
|
|
+ * @Description : TODO
|
|
|
+ * @Version : 1.0
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Create : 2020-12-25
|
|
|
+ **/
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class DateUtil {
|
|
|
+ /**
|
|
|
+ * 获取今天
|
|
|
+ *
|
|
|
+ * @return String
|
|
|
+ */
|
|
|
+ public String getToday() {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取昨天
|
|
|
+ *
|
|
|
+ * @return String
|
|
|
+ */
|
|
|
+ public String getYestoday() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.add(Calendar.DATE, -1);
|
|
|
+ Date time = cal.getTime();
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(time);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本月开始日期
|
|
|
+ *
|
|
|
+ * @return String
|
|
|
+ **/
|
|
|
+ public String getMonthStart() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.add(Calendar.MONTH, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ Date time = cal.getTime();
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(time);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本月最后一天
|
|
|
+ *
|
|
|
+ * @return String
|
|
|
+ **/
|
|
|
+ public String getMonthEnd() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ Date time = cal.getTime();
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(time);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本周的第一天
|
|
|
+ *
|
|
|
+ * @return String
|
|
|
+ **/
|
|
|
+ public String getWeekStart() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.add(Calendar.WEEK_OF_MONTH, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_WEEK, 2);
|
|
|
+ Date time = cal.getTime();
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(time);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本周的最后一天
|
|
|
+ *
|
|
|
+ * @return String
|
|
|
+ **/
|
|
|
+ public String getWeekEnd() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
|
|
|
+ cal.add(Calendar.DAY_OF_WEEK, 1);
|
|
|
+ Date time = cal.getTime();
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(time);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本年的第一天
|
|
|
+ *
|
|
|
+ * @return String
|
|
|
+ **/
|
|
|
+ public String getYearStart() {
|
|
|
+ return new SimpleDateFormat("yyyy").format(new Date()) + "-01-01";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本年的最后一天
|
|
|
+ *
|
|
|
+ * @return String
|
|
|
+ **/
|
|
|
+ public String getYearEnd() {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ Date currYearLast = calendar.getTime();
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast);
|
|
|
+ }
|
|
|
+
|
|
|
+ //Long转换为Date格式
|
|
|
+ public String fromLongToDate(Long time, String format) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ Date date = new Date(time);
|
|
|
+ return sdf.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String formatTimestampToDatetime(Timestamp timestamp) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ return sdf.format(timestamp);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String formatTimestampToDate(Timestamp timestamp) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ return sdf.format(timestamp);
|
|
|
+ }
|
|
|
+
|
|
|
+ //格式化本年
|
|
|
+ public String getThisYear() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ int year = cal.get(Calendar.YEAR);
|
|
|
+ return String.valueOf(year);
|
|
|
+ }
|
|
|
+
|
|
|
+ //格式化本月
|
|
|
+ public String getThisMonth() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ int month = cal.get(Calendar.MONTH) + 1;
|
|
|
+ if (String.valueOf(month).length() == 1) {
|
|
|
+ return "0" + String.valueOf(month);
|
|
|
+ } else {
|
|
|
+ return String.valueOf(month);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //格式化日期时间
|
|
|
+ public String formatDateTime(String dateText) throws ParseException {
|
|
|
+ if (dateText.indexOf("T") != -1) {
|
|
|
+ dateText = dateText.replace("T", " ");
|
|
|
+ }
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date date = sdf.parse(dateText);
|
|
|
+ return sdf.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String formatDateText(String dateText) throws ParseException {
|
|
|
+ if (dateText.indexOf("T") != -1) {
|
|
|
+ dateText = dateText.replace("T", " ");
|
|
|
+ }
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date date = sdf.parse(dateText);
|
|
|
+ return sdf.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String formatDateText(Date date) throws ParseException {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ return sdf.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Date parseDateTime(String dateText) throws ParseException {
|
|
|
+ if (dateText.indexOf("T") != -1) {
|
|
|
+ dateText = dateText.replace("T", " ");
|
|
|
+ }
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ return sdf.parse(dateText);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Date parseDate(String dateText) {
|
|
|
+ if (dateText.indexOf("T") != -1) {
|
|
|
+ dateText = dateText.replace("T", " ");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ return sdf.parse(dateText);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Date parseDatePlus(String dateText) {
|
|
|
+ try {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Calendar cd = Calendar.getInstance();
|
|
|
+ cd.setTime(sdf.parse(dateText));
|
|
|
+ cd.add(Calendar.DATE, 1);//增加一天
|
|
|
+ //cd.add(Calendar.MONTH, n);//增加一个月
|
|
|
+ return sdf.parse(sdf.format(cd.getTime()));
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public Date parseDate2(String dateText) throws ParseException {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ return sdf.parse(dateText);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Long parseDateTimeLong(String dateText) throws ParseException {
|
|
|
+ if (dateText.indexOf("T") != -1) {
|
|
|
+ dateText = dateText.replace("T", " ");
|
|
|
+ }
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date date = sdf.parse(dateText);
|
|
|
+ return date.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //
|
|
|
+ public Date getTodayDate() throws ParseException {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ return sdf.parse(sdf.format(new Date()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getTodayDateText() throws ParseException {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ return sdf.format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getTodayText() throws ParseException {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ return sdf.format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getTodayMissionText() throws ParseException {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
|
|
+ return sdf.format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getStartDateInThisMonth() {
|
|
|
+ DateTime date = cn.hutool.core.date.DateUtil.date();
|
|
|
+ return (cn.hutool.core.date.DateUtil.beginOfMonth(date) + "").substring(0, 10);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getEndDateInThisMonth() {
|
|
|
+ DateTime date = cn.hutool.core.date.DateUtil.date();
|
|
|
+ return (date + "").substring(0, 10);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取过去或者未来 任意天内的日期数组
|
|
|
+ *
|
|
|
+ * @param intervals intervals天内
|
|
|
+ * @return 日期数组
|
|
|
+ */
|
|
|
+ public ArrayList<String> test(int intervals) {
|
|
|
+ ArrayList<String> pastDaysList = new ArrayList<>();
|
|
|
+ ArrayList<String> fetureDaysList = new ArrayList<>();
|
|
|
+ for (int i = 0; i < intervals; i++) {
|
|
|
+ pastDaysList.add(getPastDate(i));
|
|
|
+ fetureDaysList.add(getFetureDate(i));
|
|
|
+ }
|
|
|
+ return pastDaysList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取过去第几天的日期
|
|
|
+ *
|
|
|
+ * @param past
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getPastDate(int past) {
|
|
|
+ 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("yyyy-MM-dd");
|
|
|
+ String result = format.format(today);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取未来 第 past 天的日期
|
|
|
+ *
|
|
|
+ * @param past
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getFetureDate(int past) {
|
|
|
+ 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("yyyy-MM-dd");
|
|
|
+ String result = format.format(today);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ //重新构建日期
|
|
|
+ public String rebuildDateTime(String text) {
|
|
|
+ return text.substring(0, 4) + "-" + text.substring(4, 6) + "-" + text.substring(6, 8) + " " + text.substring(8, 10) + ":" + text.substring(10, 12) + ":" + text.substring(12, 14);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ DateUtil du = new DateUtil();
|
|
|
+ //
|
|
|
+ du.test1();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void test1() {
|
|
|
+ String text = "20211201104300";
|
|
|
+// String text = "1234567890abcd";
|
|
|
+ String date = text.substring(0, 4) + "-" + text.substring(4, 6) + "-" + text.substring(6, 8) + " " + text.substring(8, 10) + ":" + text.substring(10, 12) + ":" + text.substring(12, 14);
|
|
|
+ System.out.println("date=" + date);
|
|
|
+ }
|
|
|
+}
|