From 27885ecfa181a3cbf3679daa657f4f6ce47faf72 Mon Sep 17 00:00:00 2001
From: sunshihuan <13733918655@163.com>
Date: Wed, 13 Oct 2021 10:49:33 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ehutool=E4=BE=9D=E8=B5=96?=
=?UTF-8?q?=EF=BC=8Ctd=E7=A4=BA=E4=BE=8B=E4=BF=9D=E5=AD=98=E6=95=B0?=
=?UTF-8?q?=E6=8D=AEbug=E4=BF=AE=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
mqtts-common/mqtts-common-core/pom.xml | 7 +
mqtts-modules/mqtts-modules-tdengine/pom.xml | 8 +
.../tdengine/service/WeatherService.java | 6 +-
.../net/mqtts/tdengine/util/DateUntils.java | 240 ++++++++++++++++++
4 files changed, 260 insertions(+), 1 deletion(-)
create mode 100644 mqtts-modules/mqtts-modules-tdengine/src/main/java/net/mqtts/tdengine/util/DateUntils.java
diff --git a/mqtts-common/mqtts-common-core/pom.xml b/mqtts-common/mqtts-common-core/pom.xml
index b6e9f59..7d38f6a 100644
--- a/mqtts-common/mqtts-common-core/pom.xml
+++ b/mqtts-common/mqtts-common-core/pom.xml
@@ -107,6 +107,13 @@
swagger-annotations
+
+
+ cn.hutool
+ hutool-all
+ 5.7.14
+
+
diff --git a/mqtts-modules/mqtts-modules-tdengine/pom.xml b/mqtts-modules/mqtts-modules-tdengine/pom.xml
index f8826bf..a7270c2 100644
--- a/mqtts-modules/mqtts-modules-tdengine/pom.xml
+++ b/mqtts-modules/mqtts-modules-tdengine/pom.xml
@@ -97,6 +97,14 @@
-->
+
+
+ src/main/java
+
+ **/*.xml
+
+
+
diff --git a/mqtts-modules/mqtts-modules-tdengine/src/main/java/net/mqtts/tdengine/service/WeatherService.java b/mqtts-modules/mqtts-modules-tdengine/src/main/java/net/mqtts/tdengine/service/WeatherService.java
index 0511e3a..6cf85e7 100644
--- a/mqtts-modules/mqtts-modules-tdengine/src/main/java/net/mqtts/tdengine/service/WeatherService.java
+++ b/mqtts-modules/mqtts-modules-tdengine/src/main/java/net/mqtts/tdengine/service/WeatherService.java
@@ -2,6 +2,7 @@ package net.mqtts.tdengine.service;
import net.mqtts.tdengine.dao.WeatherMapper;
import net.mqtts.tdengine.domain.Weather;
+import net.mqtts.tdengine.util.DateUntils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -41,10 +42,13 @@ public class WeatherService {
}
public int save(float temperature, float humidity) {
+ long ts = System.currentTimeMillis();
+ long thirtySec = 1000 * 30;
Weather weather = new Weather();
+ weather.setTs(new Timestamp(ts + (thirtySec)));
weather.setTemperature(temperature);
weather.setHumidity(humidity);
-
+ weather.setNote("1");
return weatherMapper.insert(weather);
}
diff --git a/mqtts-modules/mqtts-modules-tdengine/src/main/java/net/mqtts/tdengine/util/DateUntils.java b/mqtts-modules/mqtts-modules-tdengine/src/main/java/net/mqtts/tdengine/util/DateUntils.java
new file mode 100644
index 0000000..47ebf3f
--- /dev/null
+++ b/mqtts-modules/mqtts-modules-tdengine/src/main/java/net/mqtts/tdengine/util/DateUntils.java
@@ -0,0 +1,240 @@
+package net.mqtts.tdengine.util;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @Description 时间工具类
+ * @author mqtts.net
+ * @Email 13733918655@163.com
+ * @Date 2019/12/9 20:45
+ * @Version 1.0
+ */
+public class DateUntils {
+
+ public static Date getCurrentDate() {
+ return new Date();
+ }
+
+ public static String getDateString(Date date) {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
+ String previousDate = dateFormat.format(date);
+ return previousDate;
+ }
+
+ public static String getDateStrings(Date date) {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ String previousDate = dateFormat.format(date);
+ return previousDate;
+ }
+
+ public static String getDateStampString(Date date) {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
+ String previousDate = dateFormat.format(date);
+ return previousDate;
+ }
+
+ public static Date getDateString(String date) {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
+ Date date1 = null;
+ try {
+ date1 = dateFormat.parse(date);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ return date1;
+ }
+
+ public static String strToDateFormat(String date) throws ParseException {
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
+ formatter.setLenient(false);
+ Date newDate = formatter.parse(date);
+ formatter = new SimpleDateFormat("yyyy-MM-dd");
+ return formatter.format(newDate);
+ }
+
+ public static String getDateFormatString(Date date) {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
+ String previousDate = dateFormat.format(date);
+ return previousDate;
+ }
+
+ public static String getDateFormatTostr(Date date) {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
+ String previousDate = dateFormat.format(date);
+ return previousDate;
+ }
+
+ public static String getCurrentDateWithZero(Date date) {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
+ String previousDate = dateFormat.format(date);
+ return previousDate;
+ }
+
+ public static String getCurrentDateWithNight(Date date) {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
+ String previousDate = dateFormat.format(date);
+ return previousDate;
+ }
+
+ public static String getPreviousDateWithZero(Date date) {
+ Date prevDate = new Date(date.getTime() - (24 * 3600000));
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
+ String previousDate = dateFormat.format(prevDate);
+ return previousDate;
+ }
+
+ public static Date getFomatDate(String str) {
+ //创建SimpleDateFormat对象实例并定义好转换格式
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ Date date = null;
+ try {
+ // 注意格式需要与上面一致,不然会出现异常
+ date = sdf.parse(str);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ return date;
+ }
+
+ /**
+ * 将时间戳转换为日期
+ *
+ * @param stamp 时间戳
+ * @return 时间,返回格式为 yyyy-MM-dd-HH-mm-ss
+ */
+ public static String Stamp2Date(Long stamp) {
+ String result = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(stamp);
+ return result;
+ }
+
+ /**
+ * 取得当前时间戳(精确到秒)
+ *
+ * @return
+ */
+ public static String timeStamp() {
+ long time = System.currentTimeMillis();
+ String t = String.valueOf(time / 1000);
+ return t;
+ }
+
+ /**
+ * 取得当前时间戳(精确到秒)Long 类型
+ *
+ * @return
+ */
+ public static Long timeStampL() {
+ long time = System.currentTimeMillis();
+ return time / 1000;
+ }
+
+ /**
+ * 获取指定url中的某个参数
+ *
+ * @param url
+ * @param name
+ * @return
+ */
+ public static String getParamByUrl(String url, String name) {
+ url += "&";
+ String pattern = "(\\?|&){1}#{0,1}" + name + "=[a-zA-Z0-9]*(&{1})";
+
+ Pattern r = Pattern.compile(pattern);
+
+ Matcher m = r.matcher(url);
+ if (m.find()) {
+ System.out.println(m.group(0));
+ return m.group(0).split("=")[1].replace("&", "");
+ } else {
+ return null;
+ }
+ }
+ public static void main(String[] args) throws ParseException {
+
+ String sTime = "2019-05-17 00:00:00";
+ String eTime = "2019-05-19 16:00:00";
+
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ Date d1 = getFomatDate(sTime);
+ Date d2 = getFomatDate(eTime);
+ List times = getTimes(d1,d2,3600000L);
+ for (String str:times) {
+ System.out.println("得到的时间集合=:"+str);
+ }
+ List times1 = getTimes(getFomatDate("2021-09-22 00:00:00"), getFomatDate("2021-09-30 00:00:00"), 86400000);
+ System.out.println(times1);
+ }
+
+
+ //获取时间段内,时间间隔的所以时间点集合
+ public static List getTimes(Date startTime, Date endTime,long ll) throws ParseException {
+
+ List times = new ArrayList<>();
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(startTime);
+ long startTimeStamp = calendar.getTimeInMillis();//起始的毫秒数
+ calendar.setTime(endTime);
+ long endTimeStamp = calendar.getTimeInMillis();//截止时间毫秒数
+
+ while (true){
+ long temp = startTimeStamp + ll;
+ if(temp <= endTimeStamp){
+ startTimeStamp = temp;
+ Calendar _calendar = Calendar.getInstance();
+ _calendar.setTimeInMillis(temp);
+ int year = _calendar.get(Calendar.YEAR);
+ int month = _calendar.get(Calendar.MONTH);
+ int day = _calendar.get(Calendar.DAY_OF_MONTH);
+ int hour = _calendar.get(Calendar.HOUR_OF_DAY);//24小时制
+ //int hour = calendar.get(Calendar.HOUR);//12小时制
+ int minute = _calendar.get(Calendar.MINUTE);
+ int second = _calendar.get(Calendar.SECOND);
+ String time = year + "-" + (month + 1) + "-" + day + " " + hour + ":" + minute + ":" + second;
+ //时间格式处理
+ String res;
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ Date date = simpleDateFormat.parse(time);
+ long ts = date.getTime();
+ long lt = new Long(String.valueOf(ts));
+ Date date1 = new Date(lt);
+ res = simpleDateFormat.format(date1);
+ times.add(res);
+ }else {
+ break;
+ }
+ }
+ return times;
+ }
+ /*
+ * 将时间转换为时间戳
+ */
+ public static String dateToStamp(String s) throws ParseException {
+ String res;
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ Date date = simpleDateFormat.parse(s);
+ long ts = date.getTime();
+ res = String.valueOf(ts);
+ return res;
+ }
+
+ /*
+ * 将时间戳转换为时间
+ */
+ public static String stampToDate(String s){
+ String res;
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ long lt = new Long(s);
+ Date date = new Date(lt);
+ res = simpleDateFormat.format(date);
+ return res;
+ }
+
+}
--
GitLab