Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
邬吉朋
rx
Commits
4b86b646
Commit
4b86b646
authored
Aug 26, 2024
by
wujipeng
Browse files
查询七天内历史数据和当天历史数据
parent
b35c3bac
Changes
9
Hide whitespace changes
Inline
Side-by-side
pom.xml
View file @
4b86b646
...
@@ -130,9 +130,14 @@
...
@@ -130,9 +130,14 @@
<version>
1.4.2
</version>
<version>
1.4.2
</version>
</dependency>
</dependency>
<!-- mongodb -->
<!-- mongodb -->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-mongodb</artifactId>-->
<!-- </dependency>-->
<dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<groupId>
com.ruzhoukj
</groupId>
<artifactId>
spring-boot-starter-data-mongodb
</artifactId>
<artifactId>
common-mongo
</artifactId>
<version>
1.0.0
</version>
</dependency>
</dependency>
</dependencies>
</dependencies>
...
...
src/main/java/com/ruzhoukj/rx/controller/EquipmentController.java
View file @
4b86b646
...
@@ -2,6 +2,7 @@ package com.ruzhoukj.rx.controller;
...
@@ -2,6 +2,7 @@ package com.ruzhoukj.rx.controller;
import
com.ruzhoukj.rx.common.Result
;
import
com.ruzhoukj.rx.common.Result
;
import
com.ruzhoukj.rx.pojo.dto.EquipmentDTO
;
import
com.ruzhoukj.rx.pojo.dto.EquipmentDTO
;
import
com.ruzhoukj.rx.pojo.vo.DeviceVO
;
import
com.ruzhoukj.rx.service.EquipmentService
;
import
com.ruzhoukj.rx.service.EquipmentService
;
import
io.swagger.annotations.ApiOperation
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
...
@@ -37,9 +38,16 @@ public class EquipmentController {
...
@@ -37,9 +38,16 @@ public class EquipmentController {
return
Result
.
success
(
equipmentService
.
getDeviceInfoList
(
equipmentDTO
));
return
Result
.
success
(
equipmentService
.
getDeviceInfoList
(
equipmentDTO
));
}
}
// @ApiOperation(value = "获取所有设备的最新一条数据")
// @PostMapping("/report/list")
@ApiOperation
(
value
=
"根据设备id查询今天的信息"
)
// public Result getDeviceDataList() {
@GetMapping
(
"/report/today/{deviceId}"
)
// return Result.success(equipmentService.getDeviceDataList());
public
Result
getTodayByDeviceId
(
@PathVariable
String
deviceId
)
{
// }
return
Result
.
success
(
equipmentService
.
getTodayByDeviceId
(
deviceId
));
}
@ApiOperation
(
value
=
"根据设备id查询今天的信息"
)
@PostMapping
(
"/report/former/page"
)
public
Result
getTodayByDeviceId
(
@RequestBody
EquipmentDTO
equipmentDTO
)
{
return
Result
.
success
(
equipmentService
.
getFormerByDeviceId
(
equipmentDTO
));
}
}
}
src/main/java/com/ruzhoukj/rx/dao/DeviceDao.java
View file @
4b86b646
...
@@ -5,6 +5,8 @@ import com.ruzhoukj.rx.pojo.vo.DeviceVO;
...
@@ -5,6 +5,8 @@ import com.ruzhoukj.rx.pojo.vo.DeviceVO;
import
org.springframework.data.mongodb.repository.MongoRepository
;
import
org.springframework.data.mongodb.repository.MongoRepository
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
java.time.LocalDateTime
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.List
;
/**
/**
...
@@ -15,5 +17,24 @@ public interface DeviceDao extends MongoRepository<DeviceEntity, String> {
...
@@ -15,5 +17,24 @@ public interface DeviceDao extends MongoRepository<DeviceEntity, String> {
DeviceVO
findTopByDeviceIdOrderByTimestampDesc
(
String
deviceId
);
DeviceVO
findTopByDeviceIdOrderByTimestampDesc
(
String
deviceId
);
// List<DeviceVO> findtopOrderByTimestampDescgroupByDeviceId();
/**
* 查询近七天内所有 deviceId 相同的数据
*
* @param deviceId 设备 ID
* @return List<DeviceVO>
*/
List
<
DeviceVO
>
findAllByDeviceIdAndTimestampGreaterThanEqualOrderByTimestamp
(
String
deviceId
,
Date
sevenDaysAgo
);
/**
* 查询今天内所有 deviceId 相同的数据
*
* @param deviceId 设备 ID
* @param start 今天开始的时间戳
* @param end 今天结束的时间戳
* @return List<DeviceVO>
*/
List
<
DeviceVO
>
findByDeviceIdAndTimestampBetweenOrderByTimestamp
(
String
deviceId
,
LocalDateTime
start
,
LocalDateTime
end
);
}
}
src/main/java/com/ruzhoukj/rx/pojo/dto/EquipmentDTO.java
View file @
4b86b646
package
com.ruzhoukj.rx.pojo.dto
;
package
com.ruzhoukj.rx.pojo.dto
;
import
com.ruzhoukj.rx.pojo.entity.EquipmentEntity
;
import
com.ruzhoukj.rx.pojo.entity.EquipmentEntity
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiModelProperty
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
import
lombok.Data
;
import
lombok.EqualsAndHashCode
;
import
lombok.EqualsAndHashCode
;
import
java.util.Date
;
/**
/**
* @author wujipeng
* @author wujipeng
* @version 1.0
* @version 1.0
...
@@ -17,4 +18,17 @@ public class EquipmentDTO extends EquipmentEntity {
...
@@ -17,4 +18,17 @@ public class EquipmentDTO extends EquipmentEntity {
@ApiModelProperty
(
value
=
"设备名称或路名"
)
@ApiModelProperty
(
value
=
"设备名称或路名"
)
private
String
name
;
private
String
name
;
@ApiModelProperty
(
value
=
"页码"
)
private
Integer
pageNum
=
1
;
@ApiModelProperty
(
value
=
"条数"
)
private
Integer
pageSize
=
10
;
@ApiModelProperty
(
value
=
"开始时间"
)
private
Date
startTime
;
@ApiModelProperty
(
value
=
"结束时间"
)
private
Date
endTime
;
}
}
src/main/java/com/ruzhoukj/rx/service/EquipmentService.java
View file @
4b86b646
...
@@ -2,6 +2,7 @@ package com.ruzhoukj.rx.service;
...
@@ -2,6 +2,7 @@ package com.ruzhoukj.rx.service;
import
com.baomidou.mybatisplus.extension.service.IService
;
import
com.baomidou.mybatisplus.extension.service.IService
;
import
com.ruzhoukj.rx.pojo.dto.EquipmentDTO
;
import
com.ruzhoukj.rx.pojo.dto.EquipmentDTO
;
import
com.ruzhoukj.rx.pojo.entity.DeviceEntity
;
import
com.ruzhoukj.rx.pojo.entity.EquipmentEntity
;
import
com.ruzhoukj.rx.pojo.entity.EquipmentEntity
;
import
com.ruzhoukj.rx.pojo.vo.DeviceVO
;
import
com.ruzhoukj.rx.pojo.vo.DeviceVO
;
import
com.ruzhoukj.rx.pojo.vo.EquipmentVO
;
import
com.ruzhoukj.rx.pojo.vo.EquipmentVO
;
...
@@ -32,9 +33,18 @@ public interface EquipmentService extends IService<EquipmentEntity> {
...
@@ -32,9 +33,18 @@ public interface EquipmentService extends IService<EquipmentEntity> {
*/
*/
List
<
EquipmentVO
>
getDeviceInfoList
(
EquipmentDTO
equipmentDTO
);
List
<
EquipmentVO
>
getDeviceInfoList
(
EquipmentDTO
equipmentDTO
);
// /**
/**
// * 获取所有设备的最新一条数据
* 根据设备id查询今天的信息
// * @return List<DeviceVO>
*
// */
* @return List<DeviceVO>
// List<DeviceVO> getDeviceDataList();
*/
List
<
DeviceVO
>
getTodayByDeviceId
(
String
deviceId
);
/**
* 根据设备id查询之前的信息
*
* @return List<DeviceVO>
*/
List
<
DeviceVO
>
getFormerByDeviceId
(
EquipmentDTO
equipmentDTO
);
}
}
src/main/java/com/ruzhoukj/rx/service/impl/EquipmentServiceImpl.java
View file @
4b86b646
package
com.ruzhoukj.rx.service.impl
;
package
com.ruzhoukj.rx.service.impl
;
import
ch.qos.logback.classic.spi.EventArgUtil
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.ruzhoukj.rx.common.NumberConstant
;
import
com.ruzhoukj.rx.common.NumberConstant
;
import
com.ruzhoukj.rx.dao.DeviceDao
;
import
com.ruzhoukj.rx.dao.DeviceDao
;
...
@@ -13,6 +12,9 @@ import com.ruzhoukj.rx.service.EquipmentService;
...
@@ -13,6 +12,9 @@ import com.ruzhoukj.rx.service.EquipmentService;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
import
java.time.*
;
import
java.time.temporal.ChronoUnit
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.List
;
/**
/**
...
@@ -26,6 +28,7 @@ public class EquipmentServiceImpl
...
@@ -26,6 +28,7 @@ public class EquipmentServiceImpl
@Resource
@Resource
DeviceDao
deviceDao
;
DeviceDao
deviceDao
;
@Override
@Override
public
DeviceVO
getDeviceDataByDeviceId
(
String
deviceId
)
{
public
DeviceVO
getDeviceDataByDeviceId
(
String
deviceId
)
{
return
deviceDao
.
findTopByDeviceIdOrderByTimestampDesc
(
deviceId
);
return
deviceDao
.
findTopByDeviceIdOrderByTimestampDesc
(
deviceId
);
...
@@ -35,7 +38,7 @@ public class EquipmentServiceImpl
...
@@ -35,7 +38,7 @@ public class EquipmentServiceImpl
public
EquipmentVO
getDeviceInfoByDeviceId
(
String
deviceId
)
{
public
EquipmentVO
getDeviceInfoByDeviceId
(
String
deviceId
)
{
EquipmentVO
equipmentVO
=
baseMapper
.
getDeviceInfoByDeviceId
(
deviceId
);
EquipmentVO
equipmentVO
=
baseMapper
.
getDeviceInfoByDeviceId
(
deviceId
);
DeviceVO
deviceVO
=
deviceDao
.
findTopByDeviceIdOrderByTimestampDesc
(
deviceId
);
DeviceVO
deviceVO
=
deviceDao
.
findTopByDeviceIdOrderByTimestampDesc
(
deviceId
);
if
(
equipmentVO
!=
null
&&
deviceVO
!=
null
){
if
(
equipmentVO
!=
null
&&
deviceVO
!=
null
)
{
equipmentVO
.
setDeviceVO
(
deviceVO
);
equipmentVO
.
setDeviceVO
(
deviceVO
);
}
}
return
equipmentVO
;
return
equipmentVO
;
...
@@ -70,8 +73,23 @@ public class EquipmentServiceImpl
...
@@ -70,8 +73,23 @@ public class EquipmentServiceImpl
return
deviceInfoList
;
return
deviceInfoList
;
}
}
// @Override
@Override
// public List<DeviceVO> getDeviceDataList() {
public
List
<
DeviceVO
>
getTodayByDeviceId
(
String
deviceId
)
{
// return deviceDao.findtopOrderByTimestampDescgroupByDeviceId();
// LocalDate today = LocalDate.now(ZoneId.systemDefault());
// }
// LocalDateTime startOfDay = today.atStartOfDay();
// LocalDateTime endOfDay = today.plusDays(1).atStartOfDay().minusNanos(1);
// return deviceDao.findByDeviceIdAndTimestampBetween(deviceId, startOfDay, endOfDay);
LocalDate
today
=
LocalDate
.
now
(
ZoneId
.
systemDefault
());
LocalDateTime
startOfDay
=
today
.
atStartOfDay
().
plusHours
(
8
);
LocalDateTime
endOfDay
=
today
.
plusDays
(
1
).
atStartOfDay
().
plusHours
(
8
).
minusNanos
(
1
);
return
deviceDao
.
findByDeviceIdAndTimestampBetweenOrderByTimestamp
(
deviceId
,
startOfDay
,
endOfDay
);
}
@Override
public
List
<
DeviceVO
>
getFormerByDeviceId
(
EquipmentDTO
equipmentDTO
)
{
Date
sevenDaysAgo
=
Date
.
from
(
Instant
.
now
().
minus
(
7
,
ChronoUnit
.
DAYS
));
return
deviceDao
.
findAllByDeviceIdAndTimestampGreaterThanEqualOrderByTimestamp
(
equipmentDTO
.
getDeviceId
(),
sevenDaysAgo
);
}
}
}
src/main/java/com/ruzhoukj/rx/util/DateUtil.java
0 → 100644
View file @
4b86b646
package
com.ruzhoukj.rx.util
;
import
com.google.common.collect.Lists
;
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.time.*
;
import
java.time.format.DateTimeFormatter
;
import
java.util.*
;
public
class
DateUtil
extends
DateUtils
{
public
static
String
YYYY
=
"yyyy"
;
public
static
String
HH
=
"HH"
;
public
static
String
YYYY_MM_DD_SPRIT
=
"yyyy/MM/dd"
;
public
static
String
YYYY_MM
=
"yyyy-MM"
;
public
static
String
YYYY_POINT_MM
=
"yyyy.MM"
;
public
static
String
YYYY_MM_DD
=
"yyyy-MM-dd"
;
public
static
String
YYYY_POINT_MM_POINT_DD
=
"yyyy.MM.dd"
;
public
static
String
YYYY_POINT_MM_POINT_DD_POINT_HH_POINT_MM
=
"yyyy.MM.dd HH.MM"
;
public
static
String
MM_DD
=
"MM-dd"
;
public
static
String
MM_POINT_DD
=
"MM.dd"
;
public
static
final
String
FORMATTER_MM_POINT_DD
=
"MM.dd"
;
public
static
final
String
FORMATTER_HH_POINT_MM00
=
"HH.00"
;
public
static
final
String
HH_POINT_MM00
=
"HH:00"
;
public
static
final
String
FORMATTER_YYYY_POINT_MM_POINT_DD
=
"yyyy.MM.dd"
;
public
static
String
YYYY_MM_DD_HH
=
"yyyy-MM-dd HH"
;
public
static
String
YYYY_MM_DD_HH_MM
=
"yyyy.MM.dd HH:mm"
;
public
static
String
YYYYMMDDHHMMSS
=
"yyyyMMddHHmmss"
;
public
static
String
YYYY_MM_DD_HH_MM_SS
=
"yyyy-MM-dd HH:mm:ss"
;
public
static
String
YYYY_MM_DD_HH_MM_SS_BIAS
=
"yyyy/MM/dd HH:mm:ss"
;
public
static
String
YYYY_MM_DD_HH_MM_BIAS
=
"yyyy/MM/dd HH:mm"
;
public
static
String
YYYY_MM_DD_HH_MM_LINE
=
"yyyy-MM-dd HH:mm"
;
private
static
String
[]
parsePatterns
=
new
String
[]{
"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"
};
private
static
final
String
DEFAULT_INPUT_FORMAT
=
"yyyy/MM/dd"
;
private
static
final
String
DEFAULT_OUTPUT_FORMAT
=
"yyyy-MM-dd"
;
/**
* 将 Date 对象从一种格式转换为另一种格式.
*
* @param date 要转换的 Date 对象
* @param inputFormat 输入日期格式
* @param outputFormat 输出日期格式
* @return 转换后的 Date 对象
*/
public
static
Date
convertDateFormat
(
Date
date
,
String
inputFormat
,
String
outputFormat
)
{
try
{
SimpleDateFormat
inputFormatter
=
new
SimpleDateFormat
(
inputFormat
);
String
dateString
=
inputFormatter
.
format
(
date
);
SimpleDateFormat
outputFormatter
=
new
SimpleDateFormat
(
outputFormat
);
return
outputFormatter
.
parse
(
dateString
);
}
catch
(
ParseException
e
)
{
throw
new
RuntimeException
(
"Error converting date format"
,
e
);
}
}
/**
* 将 Date 对象从 yyyy/MM/dd 格式转换为 yyyy-MM-dd 格式.
*
* @param date 要转换的 Date 对象
* @return 转换后的 Date 对象
*/
public
static
Date
convertToStandardFormat
(
Date
date
)
{
return
convertDateFormat
(
date
,
DEFAULT_INPUT_FORMAT
,
DEFAULT_OUTPUT_FORMAT
);
}
private
static
final
String
INPUT_FORMAT
=
"yyyy/MM/dd HH:mm"
;
private
static
final
String
OUTPUT_FORMAT
=
"yyyy-MM-dd HH:mm"
;
/**
* 将给定的日期对象从一种格式转换为另一种格式.
*
* @param date 要转换的日期对象
* @return 转换后的日期对象
*/
public
static
Date
convertDateFormat
(
Date
date
)
{
SimpleDateFormat
inputFormatter
=
new
SimpleDateFormat
(
INPUT_FORMAT
);
SimpleDateFormat
outputFormatter
=
new
SimpleDateFormat
(
OUTPUT_FORMAT
);
String
formattedDateString
=
outputFormatter
.
format
(
date
);
try
{
return
outputFormatter
.
parse
(
formattedDateString
);
}
catch
(
ParseException
e
)
{
throw
new
IllegalArgumentException
(
"Error converting date format"
,
e
);
}
}
public
DateUtil
()
{
}
public
static
Date
getNowDate
()
{
return
new
Date
();
}
public
static
String
getDate
()
{
return
dateTimeNow
(
YYYY_MM_DD
);
}
public
static
final
String
getTime
()
{
return
dateTimeNow
(
YYYY_MM_DD_HH_MM_SS
);
}
public
static
final
String
dateTimeNow
()
{
return
dateTimeNow
(
YYYYMMDDHHMMSS
);
}
public
static
final
String
dateTimeNow
(
String
format
)
{
return
parseDateToStr
(
format
,
new
Date
());
}
public
static
final
String
dateTime
(
Date
date
)
{
return
parseDateToStr
(
YYYY_MM_DD
,
date
);
}
public
static
final
String
parseDateToStr
(
String
format
,
Date
date
)
{
return
(
new
SimpleDateFormat
(
format
)).
format
(
date
);
}
public
static
final
Date
parseStrToDate
(
String
format
,
String
dateStr
)
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
format
);
try
{
return
sdf
.
parse
(
dateStr
);
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
public
static
final
Date
dateTime
(
String
format
,
String
ts
)
{
try
{
return
(
new
SimpleDateFormat
(
format
)).
parse
(
ts
);
}
catch
(
ParseException
var3
)
{
throw
new
RuntimeException
(
var3
);
}
}
public
static
final
String
datePath
()
{
Date
now
=
new
Date
();
return
DateFormatUtils
.
format
(
now
,
"yyyy/MM/dd"
);
}
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
;
}
else
{
try
{
return
parseDate
(
str
.
toString
(),
parsePatterns
);
}
catch
(
ParseException
var2
)
{
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
=
86400000L
;
long
nh
=
3600000L
;
long
nm
=
60000L
;
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
Date
getPreviousMonthLastDay
(
Date
currentDate
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
currentDate
);
calendar
.
add
(
Calendar
.
MONTH
,
-
1
);
calendar
.
set
(
Calendar
.
DAY_OF_MONTH
,
calendar
.
getActualMaximum
(
Calendar
.
DAY_OF_MONTH
));
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
0
);
calendar
.
set
(
Calendar
.
MINUTE
,
0
);
calendar
.
set
(
Calendar
.
SECOND
,
0
);
calendar
.
set
(
Calendar
.
MILLISECOND
,
0
);
return
calendar
.
getTime
();
}
/**
* 获取今天的零时零分零秒的date类对象
*
* @return
*/
public
static
Date
getTodayZeroTime
()
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
0
);
calendar
.
set
(
Calendar
.
MINUTE
,
0
);
calendar
.
set
(
Calendar
.
SECOND
,
0
);
calendar
.
set
(
Calendar
.
MILLISECOND
,
0
);
return
calendar
.
getTime
();
}
/**
* 获取今天 23 时 59 分 59 秒的 Date 对象
*
* @return 今天 23 时 59 分 59 秒的 Date 对象
*/
public
static
Date
getTodayEndOfDay
()
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
23
);
calendar
.
set
(
Calendar
.
MINUTE
,
59
);
calendar
.
set
(
Calendar
.
SECOND
,
59
);
calendar
.
set
(
Calendar
.
MILLISECOND
,
0
);
return
calendar
.
getTime
();
}
/**
* @description: 获取当前年份
* @author: zhuxiang
* @date: 2023/7/4 16:45
* @param:
* @return:
**/
public
static
String
getNowYear
()
{
// 获取当前时间
Date
currentDate
=
new
Date
();
// 创建日期格式化对象
SimpleDateFormat
dateFormat
=
new
SimpleDateFormat
(
"yyyy"
);
// 格式化当前时间为年份
return
dateFormat
.
format
(
currentDate
);
}
/**
* 获取当前日期起前n天的日期
*/
public
static
Date
getBeforeNDay
(
Integer
n
)
{
// 获取当前日期
Calendar
calendar
=
Calendar
.
getInstance
();
// 减去n天
calendar
.
add
(
Calendar
.
DAY_OF_MONTH
,
-
n
);
return
calendar
.
getTime
();
}
/**
* 获取当前日期起前n天的起始日期
*/
public
static
Date
getBeforeNDayStart
(
Integer
n
)
{
// 获取当前日期
Calendar
calendar
=
Calendar
.
getInstance
();
// 减去n天
calendar
.
add
(
Calendar
.
DAY_OF_MONTH
,
-
n
);
//设置零点时间
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
0
);
calendar
.
set
(
Calendar
.
MINUTE
,
0
);
calendar
.
set
(
Calendar
.
SECOND
,
0
);
calendar
.
set
(
Calendar
.
MILLISECOND
,
0
);
return
calendar
.
getTime
();
}
/**
* 计算两个时间相差的描述
*
* @param beforeDate 前时间
* @param AfterDate 后时间
* @return double 秒
*/
public
static
int
getDiffHour
(
Date
beforeDate
,
Date
AfterDate
)
{
// 获取两个 Date 对象的毫秒数
long
time1
=
beforeDate
.
getTime
();
long
time2
=
AfterDate
.
getTime
();
// 计算秒数差
return
(
int
)
((
time2
-
time1
)
/
1000
/
3600
);
}
/**
* 计算两个时间相差的描述
*
* @param beforeDate 前时间
* @param AfterDate 后时间
* @return double 秒
*/
public
static
int
getDiffDay
(
Date
beforeDate
,
Date
AfterDate
)
{
// 获取两个 Date 对象的毫秒数
long
time1
=
beforeDate
.
getTime
();
long
time2
=
AfterDate
.
getTime
();
// 计算秒数差
return
(
int
)
((
time2
-
time1
)
/
1000
/
3600
/
24
);
}
/**
* 计算两个时间相差的描述
*
* @param beforeDate 前时间
* @param AfterDate 后时间
* @return double 秒
*/
public
static
Long
getDiffSecond
(
Date
beforeDate
,
Date
AfterDate
)
{
// 获取两个 Date 对象的毫秒数
long
time1
=
beforeDate
.
getTime
();
long
time2
=
AfterDate
.
getTime
();
// 计算秒数差
return
(
time2
-
time1
)
/
1000
;
}
/**
* 查看当前时间前小时时间
*
* @param n 要计算的小时数
*/
public
static
Date
getBeforeNHour
(
int
n
)
{
// 获取当前日期
Calendar
calendar
=
Calendar
.
getInstance
();
// 减去n天
calendar
.
add
(
Calendar
.
HOUR_OF_DAY
,
-
n
);
return
calendar
.
getTime
();
}
/**
* 获取n天前日期数组
*
* @param day 天数
* @return List<String>日期数组
*/
public
static
List
<
String
>
getLastNDays
(
int
day
)
{
//初始化日期数组
List
<
String
>
lastSevenDays
=
Lists
.
newArrayList
();
// 使用循环获取最近七天的日期
// 获取当前日期
Date
currentDate
=
new
Date
();
for
(
int
i
=
0
;
i
<
day
;
i
++)
{
lastSevenDays
.
add
(
parseDateToStr
(
MM_POINT_DD
,
currentDate
));
// 减去一天以获取前一天的日期
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
currentDate
);
calendar
.
add
(
Calendar
.
DAY_OF_MONTH
,
-
1
);
currentDate
=
calendar
.
getTime
();
}
return
lastSevenDays
;
}
/**
* 获取最近五小时数组
*
* @param hour 小时数
* @return List<String> 小时数组
*/
public
static
List
<
String
>
getLastFiveHours
(
int
hour
)
{
//初始化小时数组
List
<
String
>
lastFiveHours
=
Lists
.
newArrayList
();
// 使用循环获取最近七天的日期
// 获取当前日期
Date
currentDate
=
new
Date
();
for
(
int
i
=
0
;
i
<
hour
;
i
++)
{
lastFiveHours
.
add
(
parseDateToStr
(
YYYY_MM_DD_HH
,
currentDate
));
// 减去一天以获取前一天的日期
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
currentDate
);
calendar
.
add
(
Calendar
.
HOUR_OF_DAY
,
-
1
);
currentDate
=
calendar
.
getTime
();
}
return
lastFiveHours
;
}
/**
* @param date 初始日期
* @param hour 累加的小时
* @return Date 累加后的日期
*/
public
static
Date
DateAddHour
(
Date
date
,
Double
hour
)
{
// 将小时转换为毫秒(1小时=3600秒=3600000毫秒)
long
millisecondsToAdd
=
(
long
)
(
hour
*
3600000
);
// 计算新的时间
return
new
Date
(
date
.
getTime
()
+
millisecondsToAdd
);
}
/**
* @param date 初始日期
* @param hour 减少的小时
* @return Date 减少后的日期
*/
public
static
Date
DateReduceHour
(
Date
date
,
Double
hour
)
{
// 将小时转换为毫秒(1小时=3600秒=3600000毫秒)
long
millisecondsToAdd
=
(
long
)
(
hour
*
3600000
);
// 计算新的时间
return
new
Date
(
date
.
getTime
()
-
millisecondsToAdd
);
}
/**
* 判断月份是否存在对应日期
*/
public
static
Boolean
DateComparison
()
{
//获取当前年月日
LocalDate
currentDate
=
LocalDate
.
now
();
//获取上月同期日期
LocalDate
lastMonth
=
currentDate
.
minusMonths
(
1
);
//获取当前日
int
dayToCheck
=
currentDate
.
getDayOfMonth
();
//获取上月最后一天日
int
lastMonthLength
=
lastMonth
.
lengthOfMonth
();
//比较
return
dayToCheck
<=
lastMonthLength
;
}
/**
* @param startDate 开始日期
* @param endDate 结束日期
* @return 时间数组
*/
public
static
List
<
String
>
dateRange
(
String
startDate
,
String
endDate
)
{
List
<
String
>
dateRange
=
generateDateRange
(
startDate
,
endDate
);
// 对日期数组进行排序
Collections
.
sort
(
dateRange
);
// 输出生成的日期数组
return
dateRange
;
}
private
static
List
<
String
>
generateDateRange
(
String
startDateString
,
String
endDateString
)
{
List
<
String
>
dateRange
=
new
ArrayList
<>();
// 尝试解析为日期格式
LocalDate
startDate
=
parseDate
(
startDateString
);
LocalDate
endDate
=
parseDate
(
endDateString
);
// 尝试解析为年月格式
YearMonth
startYearMonth
=
parseYearMonth
(
startDateString
);
YearMonth
endYearMonth
=
parseYearMonth
(
endDateString
);
if
(
startDate
!=
null
&&
endDate
!=
null
)
{
// 处理日期范围
while
(!
startDate
.
isAfter
(
endDate
))
{
DateTimeFormatter
outputFormatter
=
DateTimeFormatter
.
ofPattern
(
"yyyy.MM.dd"
);
dateRange
.
add
(
startDate
.
format
(
outputFormatter
));
startDate
=
startDate
.
plusDays
(
1
);
}
}
else
if
(
startYearMonth
!=
null
&&
endYearMonth
!=
null
)
{
// 处理年月范围
while
(!
startYearMonth
.
isAfter
(
endYearMonth
))
{
DateTimeFormatter
outputFormatter
=
DateTimeFormatter
.
ofPattern
(
"yyyy.MM"
);
dateRange
.
add
(
startYearMonth
.
format
(
outputFormatter
));
startYearMonth
=
startYearMonth
.
plusMonths
(
1
);
}
}
else
{
// 处理年份范围
Integer
startYear
=
parseYear
(
startDateString
);
Integer
endYear
=
parseYear
(
endDateString
);
if
(
startYear
!=
null
&&
endYear
!=
null
)
{
while
(
startYear
<=
endYear
)
{
dateRange
.
add
(
startYear
.
toString
());
startYear
++;
}
}
}
return
dateRange
;
}
private
static
LocalDate
parseDate
(
String
dateString
)
{
try
{
return
LocalDate
.
parse
(
dateString
,
DateTimeFormatter
.
ISO_LOCAL_DATE
);
}
catch
(
Exception
e
)
{
// 处理解析失败的情况
return
null
;
}
}
private
static
YearMonth
parseYearMonth
(
String
yearMonthString
)
{
try
{
return
YearMonth
.
parse
(
yearMonthString
,
DateTimeFormatter
.
ofPattern
(
"yyyy-MM"
));
}
catch
(
Exception
e
)
{
// 处理解析失败的情况
return
null
;
}
}
private
static
Integer
parseYear
(
String
yearString
)
{
try
{
return
Integer
.
parseInt
(
yearString
);
}
catch
(
NumberFormatException
e
)
{
// 处理解析失败的情况
return
null
;
}
}
private
static
String
formatDate
(
LocalDate
date
)
{
return
date
.
format
(
DateTimeFormatter
.
ISO_LOCAL_DATE
);
}
private
static
String
formatYearMonth
(
YearMonth
yearMonth
)
{
return
yearMonth
.
format
(
DateTimeFormatter
.
ofPattern
(
"yyyy-MM"
));
}
/**
* 根据开始时间和间隔时间,计算下一个时间
*
* @param startTime 开始时间
* @param period 间隔时间 分钟
* @return 下一个时间
*/
public
static
Date
getNextTime
(
Date
startTime
,
String
period
)
{
Instant
instant
=
startTime
.
toInstant
();
Date
nowDate
=
DateUtil
.
getNowDate
();
if
(
nowDate
.
before
(
Date
.
from
(
instant
)))
{
return
Date
.
from
(
instant
);
}
while
(!
nowDate
.
before
(
Date
.
from
(
instant
)))
{
instant
=
instant
.
plusSeconds
(
60L
*
Integer
.
parseInt
(
period
));
}
return
Date
.
from
(
instant
);
}
public
static
List
<
String
>
getRecent5Days
()
{
List
<
String
>
dates
=
new
ArrayList
<>();
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
"MM-dd"
);
LocalDate
today
=
LocalDate
.
now
();
for
(
int
i
=
0
;
i
<
5
;
i
++)
{
String
formattedDate
=
today
.
minusDays
(
i
).
format
(
formatter
);
dates
.
add
(
formattedDate
);
}
return
Lists
.
reverse
(
dates
);
}
/**
* 获取n天前日期数组
*
* @param day 天数
* @return List<String>日期数组
*/
public
static
List
<
String
>
getLastNDays
(
int
day
,
String
format
)
{
// 获取当前日期
LocalDate
currentDate
=
LocalDate
.
now
();
// 定义日期格式化器
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
format
);
// 创建存储日期的列表
List
<
String
>
dateList
=
Lists
.
newArrayList
();
// 循环获取前七天的日期,并添加到列表中
for
(
int
i
=
0
;
i
<
day
;
i
++)
{
// 减去 i+1 天
LocalDate
previousDate
=
currentDate
.
minusDays
(
i
+
1
);
// 格式化日期
String
formattedDate
=
previousDate
.
format
(
formatter
);
// 添加到列表中
dateList
.
add
(
formattedDate
);
}
Collections
.
sort
(
dateList
);
return
dateList
;
}
public
static
List
<
String
>
getLastNDayIncludingToday
(
int
day
,
String
format
)
{
// Get the current date
LocalDate
currentDate
=
LocalDate
.
now
();
// Define the date formatter
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
format
);
// Create a list to store the dates
List
<
String
>
dateList
=
new
ArrayList
<>();
// Loop to get the last N days including today
for
(
int
i
=
0
;
i
<
day
;
i
++)
{
// Get the date for the current iteration
LocalDate
dateToAdd
=
currentDate
.
minusDays
(
day
-
1
-
i
);
// Format the date and add it to the list
String
formattedDate
=
dateToAdd
.
format
(
formatter
);
dateList
.
add
(
formattedDate
);
}
return
dateList
;
}
/**
* 日期格式转字符串
*/
public
static
String
date2Str
(
Date
date
,
String
format
)
{
// 创建日期时间格式化器
return
(
new
SimpleDateFormat
(
format
)).
format
(
date
);
}
/**
* 获取N小时前数组
*
* @param hour 小时数
* @param format 转换格式
* @return List<String>
*/
public
static
List
<
String
>
getLastNHours
(
int
hour
,
String
format
)
{
// 获取当前时间
LocalDateTime
now
=
LocalDateTime
.
now
();
// 创建存储最近六小时时间的数组
List
<
String
>
lastSixHours
=
new
ArrayList
<>();
// 创建时间格式化器
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
format
);
// 将当前时间往前推六个小时,依次格式化并添加到数组中
for
(
int
i
=
0
;
i
<
hour
;
i
++)
{
LocalDateTime
time
=
now
.
minusHours
(
i
);
String
formattedTime
=
time
.
format
(
formatter
);
lastSixHours
.
add
(
formattedTime
);
}
Collections
.
sort
(
lastSixHours
);
return
lastSixHours
;
}
/**
* 获取今天二十四小时的日期数据
*
* @param format 格式化
* @return List<String>
*/
public
static
List
<
String
>
getTodayHours
(
String
format
)
{
// 获取当前日期
LocalDate
today
=
LocalDate
.
now
();
// 创建存储今天 24 小时时间的数组
List
<
String
>
todayHours
=
Lists
.
newArrayList
();
// 创建时间格式化器
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
format
);
// 循环添加今天 24 小时的时间
for
(
int
i
=
0
;
i
<
24
;
i
++)
{
LocalDateTime
time
=
LocalDateTime
.
of
(
today
,
LocalTime
.
of
(
i
,
0
));
String
formattedTime
=
time
.
format
(
formatter
);
todayHours
.
add
(
formattedTime
);
}
return
todayHours
;
}
/**
* 将 yyyy/MM/dd 格式的日期字符串转换为 yyyy-MM-dd 格式的 Date 对象
*
* @param dateString 输入的日期字符串
* @return 转换后的 Date 对象,如果输入格式不正确则返回 null
*/
public
static
Date
convertToDateNew
(
String
dateString
)
{
try
{
SimpleDateFormat
inputFormatter
=
new
SimpleDateFormat
(
"yyyy/MM/dd"
);
SimpleDateFormat
outputFormatter
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
Date
date
=
inputFormatter
.
parse
(
dateString
);
String
formattedDateString
=
outputFormatter
.
format
(
date
);
return
outputFormatter
.
parse
(
formattedDateString
);
}
catch
(
ParseException
e
)
{
// 如果输入的日期字符串格式不正确,返回 null
return
null
;
}
}
private
static
final
String
DEFAULT_PATTERN
=
"yyyy-MM-dd HH:mm:ss"
;
private
static
final
String
DEFAULT_TIMEZONE
=
"Asia/Shanghai"
;
// 东八区时区
/**
* 获取当前时间的日期字符串
*
* @return 当前时间的日期字符串
*/
public
static
String
getCurrentDateTime
()
{
return
getDateTime
(
new
Date
(),
DEFAULT_TIMEZONE
);
}
/**
* 获取指定日期时间的日期字符串
*
* @param date 指定的日期时间
* @return 指定日期时间的日期字符串
*/
public
static
String
getDateTime
(
Date
date
)
{
return
getDateTime
(
date
,
DEFAULT_TIMEZONE
);
}
/**
* 获取指定日期时间的日期字符串
*
* @param date 指定的日期时间
* @param timeZone 时区
* @return 指定日期时间的日期字符串
*/
public
static
String
getDateTime
(
Date
date
,
String
timeZone
)
{
return
getDateTime
(
date
,
DEFAULT_PATTERN
,
timeZone
);
}
/**
* 获取指定日期时间的日期字符串
*
* @param date 指定的日期时间
* @param pattern 日期格式字符串
* @param timeZone 时区
* @return 指定日期时间的日期字符串
*/
public
static
String
getDateTime
(
Date
date
,
String
pattern
,
String
timeZone
)
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
pattern
);
sdf
.
setTimeZone
(
TimeZone
.
getTimeZone
(
timeZone
));
return
sdf
.
format
(
date
);
}
}
src/main/resources/application.yml
View file @
4b86b646
...
@@ -39,4 +39,7 @@ jwt:
...
@@ -39,4 +39,7 @@ jwt:
key
:
test
key
:
test
ttl
:
5400000
ttl
:
5400000
logging
:
level
:
org.springframework.data.mongodb.core.MongoTemplate
:
DEBUG
src/main/resources/com/ruzhoukj/rx/dao/DeviceDao.xml
View file @
4b86b646
...
@@ -2,7 +2,4 @@
...
@@ -2,7 +2,4 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper
namespace=
"com.ruzhoukj.rx.dao.DeviceDao"
>
<mapper
namespace=
"com.ruzhoukj.rx.dao.DeviceDao"
>
<select
id=
"findTopByDeviceIdOrderByTimestampDesc"
resultType=
"com.ruzhoukj.rx.pojo.vo.DeviceVO"
>
</select>
</mapper>
</mapper>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment