Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
yangxiujun
/
paidan_demo
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit dff89591
authored
Oct 08, 2023
by
刘鑫
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
CI: 容量查询接口限制日期范围在一个月内
1 parent
33803aab
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
508 additions
and
2 deletions
project-interface/src/main/java/com/dituhui/pea/util/DateUtil.java
project-order/src/main/java/com/dituhui/pea/order/controller/PeaApiController.java
project-interface/src/main/java/com/dituhui/pea/util/DateUtil.java
0 → 100644
View file @
dff8959
package
com
.
dituhui
.
pea
.
util
;
import
lombok.experimental.UtilityClass
;
import
org.springframework.util.Assert
;
import
java.text.MessageFormat
;
import
java.time.Duration
;
import
java.time.Instant
;
import
java.time.LocalDate
;
import
java.time.LocalDateTime
;
import
java.time.Period
;
import
java.time.ZoneId
;
import
java.time.ZonedDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.time.temporal.Temporal
;
import
java.time.temporal.TemporalAmount
;
import
java.time.temporal.TemporalQuery
;
import
java.util.Calendar
;
import
java.util.Date
;
import
java.util.GregorianCalendar
;
import
java.util.TimeZone
;
/**
* 日期工具类
*
* @author liuxin
*/
@UtilityClass
public
class
DateUtil
{
/**
* yyyy-MM-dd HH:mm:ss
*/
public
static
final
String
PATTERN_DATETIME
=
"yyyy-MM-dd HH:mm:ss"
;
/**
* 日期 yyyy-MM-dd格式
*/
public
static
final
String
PATTERN_DATE
=
"yyyy-MM-dd"
;
/**
* 时间格式 HH:mm:ss
*/
public
static
final
String
PATTERN_TIME
=
"HH:mm:ss"
;
/**
* java 8 时间格式化
*/
public
static
final
DateTimeFormatter
DATETIME_FORMATTER
=
DateTimeFormatter
.
ofPattern
(
DateUtil
.
PATTERN_DATETIME
);
/**
* 日期 yyyy-MM-dd格式
*/
public
static
final
DateTimeFormatter
DATE_FORMATTER
=
DateTimeFormatter
.
ofPattern
(
DateUtil
.
PATTERN_DATE
);
/**
* 时间格式 HH:mm:ss
*/
public
static
final
DateTimeFormatter
TIME_FORMATTER
=
DateTimeFormatter
.
ofPattern
(
DateUtil
.
PATTERN_TIME
);
/**
* 获取当前日期
*
* @return 当前日期
*/
public
static
Date
now
()
{
return
new
Date
();
}
/**
* 添加年
*
* @param date 时间
* @param yearsToAdd 添加的年数
* @return 设置后的时间
*/
public
static
Date
plusYears
(
Date
date
,
int
yearsToAdd
)
{
return
DateUtil
.
set
(
date
,
Calendar
.
YEAR
,
yearsToAdd
);
}
/**
* 添加月
*
* @param date 时间
* @param monthsToAdd 添加的月数
* @return 设置后的时间
*/
public
static
Date
plusMonths
(
Date
date
,
int
monthsToAdd
)
{
return
DateUtil
.
set
(
date
,
Calendar
.
MONTH
,
monthsToAdd
);
}
/**
* 添加周
*
* @param date 时间
* @param weeksToAdd 添加的周数
* @return 设置后的时间
*/
public
static
Date
plusWeeks
(
Date
date
,
int
weeksToAdd
)
{
return
DateUtil
.
plus
(
date
,
Period
.
ofWeeks
(
weeksToAdd
));
}
/**
* 添加天
*
* @param date 时间
* @param daysToAdd 添加的天数
* @return 设置后的时间
*/
public
static
Date
plusDays
(
Date
date
,
long
daysToAdd
)
{
return
DateUtil
.
plus
(
date
,
Duration
.
ofDays
(
daysToAdd
));
}
/**
* 添加小时
*
* @param date 时间
* @param hoursToAdd 添加的小时数
* @return 设置后的时间
*/
public
static
Date
plusHours
(
Date
date
,
long
hoursToAdd
)
{
return
DateUtil
.
plus
(
date
,
Duration
.
ofHours
(
hoursToAdd
));
}
/**
* 添加分钟
*
* @param date 时间
* @param minutesToAdd 添加的分钟数
* @return 设置后的时间
*/
public
static
Date
plusMinutes
(
Date
date
,
long
minutesToAdd
)
{
return
DateUtil
.
plus
(
date
,
Duration
.
ofMinutes
(
minutesToAdd
));
}
/**
* 添加秒
*
* @param date 时间
* @param secondsToAdd 添加的秒数
* @return 设置后的时间
*/
public
static
Date
plusSeconds
(
Date
date
,
long
secondsToAdd
)
{
return
DateUtil
.
plus
(
date
,
Duration
.
ofSeconds
(
secondsToAdd
));
}
/**
* 添加毫秒
*
* @param date 时间
* @param millisToAdd 添加的毫秒数
* @return 设置后的时间
*/
public
static
Date
plusMillis
(
Date
date
,
long
millisToAdd
)
{
return
DateUtil
.
plus
(
date
,
Duration
.
ofMillis
(
millisToAdd
));
}
/**
* 添加纳秒
*
* @param date 时间
* @param nanosToAdd 添加的纳秒数
* @return 设置后的时间
*/
public
static
Date
plusNanos
(
Date
date
,
long
nanosToAdd
)
{
return
DateUtil
.
plus
(
date
,
Duration
.
ofNanos
(
nanosToAdd
));
}
/**
* 日期添加时间量
*
* @param date 时间
* @param amount 时间量
* @return 设置后的时间
*/
public
static
Date
plus
(
Date
date
,
TemporalAmount
amount
)
{
Instant
instant
=
date
.
toInstant
();
return
Date
.
from
(
instant
.
plus
(
amount
));
}
/**
* 减少年
*
* @param date 时间
* @param years 减少的年数
* @return 设置后的时间
*/
public
static
Date
minusYears
(
Date
date
,
int
years
)
{
return
DateUtil
.
set
(
date
,
Calendar
.
YEAR
,
-
years
);
}
/**
* 减少月
*
* @param date 时间
* @param months 减少的月数
* @return 设置后的时间
*/
public
static
Date
minusMonths
(
Date
date
,
int
months
)
{
return
DateUtil
.
set
(
date
,
Calendar
.
MONTH
,
-
months
);
}
/**
* 减少周
*
* @param date 时间
* @param weeks 减少的周数
* @return 设置后的时间
*/
public
static
Date
minusWeeks
(
Date
date
,
int
weeks
)
{
return
DateUtil
.
minus
(
date
,
Period
.
ofWeeks
(
weeks
));
}
/**
* 减少天
*
* @param date 时间
* @param days 减少的天数
* @return 设置后的时间
*/
public
static
Date
minusDays
(
Date
date
,
long
days
)
{
return
DateUtil
.
minus
(
date
,
Duration
.
ofDays
(
days
));
}
/**
* 减少小时
*
* @param date 时间
* @param hours 减少的小时数
* @return 设置后的时间
*/
public
static
Date
minusHours
(
Date
date
,
long
hours
)
{
return
DateUtil
.
minus
(
date
,
Duration
.
ofHours
(
hours
));
}
/**
* 减少分钟
*
* @param date 时间
* @param minutes 减少的分钟数
* @return 设置后的时间
*/
public
static
Date
minusMinutes
(
Date
date
,
long
minutes
)
{
return
DateUtil
.
minus
(
date
,
Duration
.
ofMinutes
(
minutes
));
}
/**
* 减少秒
*
* @param date 时间
* @param seconds 减少的秒数
* @return 设置后的时间
*/
public
static
Date
minusSeconds
(
Date
date
,
long
seconds
)
{
return
DateUtil
.
minus
(
date
,
Duration
.
ofSeconds
(
seconds
));
}
/**
* 减少毫秒
*
* @param date 时间
* @param millis 减少的毫秒数
* @return 设置后的时间
*/
public
static
Date
minusMillis
(
Date
date
,
long
millis
)
{
return
DateUtil
.
minus
(
date
,
Duration
.
ofMillis
(
millis
));
}
/**
* 减少纳秒
*
* @param date 时间
* @param nanos 减少的纳秒数
* @return 设置后的时间
*/
public
static
Date
minusNanos
(
Date
date
,
long
nanos
)
{
return
DateUtil
.
minus
(
date
,
Duration
.
ofNanos
(
nanos
));
}
/**
* 日期减少时间量
*
* @param date 时间
* @param amount 时间量
* @return 设置后的时间
*/
public
static
Date
minus
(
Date
date
,
TemporalAmount
amount
)
{
Instant
instant
=
date
.
toInstant
();
return
Date
.
from
(
instant
.
minus
(
amount
));
}
/**
* 设置日期属性
*
* @param date 时间
* @param calendarField 更改的属性
* @param amount 更改数,-1表示减少
* @return 设置后的时间
*/
private
static
Date
set
(
Date
date
,
int
calendarField
,
int
amount
)
{
Assert
.
notNull
(
date
,
"The date must not be null"
);
Calendar
c
=
Calendar
.
getInstance
();
c
.
setLenient
(
false
);
c
.
setTime
(
date
);
c
.
add
(
calendarField
,
amount
);
return
c
.
getTime
();
}
/**
* 将字符串转换为时间
*
* @param dateStr 时间字符串
* @param pattern 表达式
* @param query TemporalQuery
* @param <T> 转换目标类型
* @return 时间
*/
public
static
<
T
>
T
parse
(
String
dateStr
,
String
pattern
,
TemporalQuery
<
T
>
query
)
{
return
DateTimeFormatter
.
ofPattern
(
pattern
).
parse
(
dateStr
,
query
);
}
/**
* 时间转 Instant
*
* @param dateTime 时间
* @return Instant
*/
public
static
Instant
toInstant
(
LocalDateTime
dateTime
)
{
return
dateTime
.
atZone
(
ZoneId
.
of
(
"+8"
)).
toInstant
();
}
/**
* Instant 转 时间
*
* @param instant Instant
* @return Instant
*/
public
static
LocalDateTime
toDateTime
(
Instant
instant
)
{
return
LocalDateTime
.
ofInstant
(
instant
,
ZoneId
.
of
(
"+8"
));
}
/**
* 转换成 date
*
* @param dateTime LocalDateTime
* @return Date
*/
public
static
Date
toDate
(
LocalDateTime
dateTime
)
{
return
Date
.
from
(
DateUtil
.
toInstant
(
dateTime
));
}
/**
* 转换成 date
*
* @param localDate LocalDate
* @return Date
*/
public
static
Date
toDate
(
final
LocalDate
localDate
)
{
return
Date
.
from
(
localDate
.
atStartOfDay
(
ZoneId
.
of
(
"+8"
)).
toInstant
());
}
/**
* Converts local date time to Calendar.
*
* @param localDateTime 需要转换掉时间
* @return Calendar对象
*/
public
static
Calendar
toCalendar
(
final
LocalDateTime
localDateTime
)
{
return
GregorianCalendar
.
from
(
ZonedDateTime
.
of
(
localDateTime
,
ZoneId
.
of
(
"+8"
)));
}
/**
* localDateTime 转换成毫秒数
*
* @param localDateTime LocalDateTime
* @return long
*/
public
static
long
toMilliseconds
(
final
LocalDateTime
localDateTime
)
{
return
localDateTime
.
atZone
(
ZoneId
.
of
(
"+8"
)).
toInstant
().
toEpochMilli
();
}
/**
* localDate 转换成毫秒数
*
* @param localDate LocalDate
* @return long
*/
public
static
long
toMilliseconds
(
LocalDate
localDate
)
{
return
toMilliseconds
(
localDate
.
atStartOfDay
());
}
/**
* 转换成java8 时间
*
* @param calendar 日历
* @return LocalDateTime
*/
public
static
LocalDateTime
fromCalendar
(
final
Calendar
calendar
)
{
TimeZone
tz
=
calendar
.
getTimeZone
();
ZoneId
zid
=
tz
==
null
?
ZoneId
.
of
(
"+8"
)
:
tz
.
toZoneId
();
return
LocalDateTime
.
ofInstant
(
calendar
.
toInstant
(),
zid
);
}
/**
* 转换成java8 时间
*
* @param instant Instant
* @return LocalDateTime
*/
public
static
LocalDateTime
fromInstant
(
final
Instant
instant
)
{
return
LocalDateTime
.
ofInstant
(
instant
,
ZoneId
.
of
(
"+8"
));
}
/**
* 转换成java8 时间
*
* @param date Date
* @return LocalDateTime
*/
public
static
LocalDateTime
fromDate
(
final
Date
date
)
{
return
LocalDateTime
.
ofInstant
(
date
.
toInstant
(),
ZoneId
.
of
(
"+8"
));
}
/**
* 转换成java8 时间
*
* @param milliseconds 毫秒数
* @return LocalDateTime
*/
public
static
LocalDateTime
fromMilliseconds
(
final
long
milliseconds
)
{
return
LocalDateTime
.
ofInstant
(
Instant
.
ofEpochMilli
(
milliseconds
),
ZoneId
.
of
(
"+8"
));
}
/**
* 比较2个时间差,跨度比较小
*
* @param startInclusive 开始时间
* @param endExclusive 结束时间
* @return 时间间隔
*/
public
static
Duration
between
(
Temporal
startInclusive
,
Temporal
endExclusive
)
{
return
Duration
.
between
(
startInclusive
,
endExclusive
);
}
/**
* 比较2个时间差,跨度比较大,年月日为单位
*
* @param startDate 开始时间
* @param endDate 结束时间
* @return 时间间隔
*/
public
static
Period
between
(
LocalDate
startDate
,
LocalDate
endDate
)
{
return
Period
.
between
(
startDate
,
endDate
);
}
/**
* 比较2个 时间差
*
* @param startDate 开始时间
* @param endDate 结束时间
* @return 时间间隔
*/
public
static
Duration
between
(
Date
startDate
,
Date
endDate
)
{
return
Duration
.
between
(
startDate
.
toInstant
(),
endDate
.
toInstant
());
}
/**
* 将秒数转换为日时分秒
*
* @param second 秒数
* @return 时间
*/
//CHECKSTYLE:OFF
@SuppressWarnings
(
"checkstyle:MagicNumber"
)
public
static
String
secondToTime
(
Long
second
)
{
// 判断是否为空
if
(
second
==
null
||
second
==
0L
)
{
return
""
;
}
//转换天数
long
days
=
second
/
86400
;
//剩余秒数
second
=
second
%
86400
;
//转换小时
long
hours
=
second
/
3600
;
//剩余秒数
second
=
second
%
3600
;
//转换分钟
long
minutes
=
second
/
60
;
//剩余秒数
second
=
second
%
60
;
if
(
days
>
0
)
{
return
MessageFormat
.
format
(
"{}天{}小时{}分{}秒"
,
days
,
hours
,
minutes
,
second
);
}
else
{
return
MessageFormat
.
format
(
"{}小时{}分{}秒"
,
hours
,
minutes
,
second
);
}
}
//CHECKSTYLE:ON
}
project-order/src/main/java/com/dituhui/pea/order/controller/PeaApiController.java
View file @
dff8959
package
com
.
dituhui
.
pea
.
order
.
controller
;
import
com.dituhui.pea.common.BusinessException
;
import
com.dituhui.pea.common.Result
;
import
com.dituhui.pea.order.dto.OrganizationTreeDTO
;
import
com.dituhui.pea.order.dto.param.BaseDistance
;
...
...
@@ -13,6 +14,7 @@ import com.dituhui.pea.order.dto.param.OrderConfirmParam;
import
com.dituhui.pea.order.dto.param.OrderConfirmResult
;
import
com.dituhui.pea.order.dto.param.OrderDTO
;
import
com.dituhui.pea.order.dto.param.ValidGroup
;
import
com.dituhui.pea.util.DateUtil
;
import
com.fasterxml.jackson.annotation.JsonFormat
;
import
lombok.RequiredArgsConstructor
;
import
org.springframework.format.annotation.DateTimeFormat
;
...
...
@@ -26,10 +28,11 @@ import org.springframework.web.bind.annotation.RestController;
import
javax.validation.constraints.NotBlank
;
import
javax.validation.constraints.NotNull
;
import
java.time.Period
;
import
java.util.Date
;
/**
* PEA
调用API
* PEA
对外接口
*/
@RestController
@RequestMapping
(
"/pea-order/pea"
)
...
...
@@ -108,7 +111,12 @@ public class PeaApiController {
*/
@PostMapping
(
"/capacity/query"
)
public
Result
<
CapacityQueryDTO
.
Result
>
capacityQuery
(
@Validated
@RequestBody
CapacityQueryDTO
.
Request
reqDTO
)
{
//TODO 查询日期起止参数限制为一月
//查询日期起止参数限制为一月
Period
between
=
Period
.
between
(
DateUtil
.
fromDate
(
reqDTO
.
getBeginDate
()).
toLocalDate
(),
DateUtil
.
fromDate
(
reqDTO
.
getEndDate
()).
toLocalDate
());
if
(
between
.
getMonths
()
>
1
)
{
throw
new
BusinessException
(
"查询日期起止参数限制为一月"
);
}
CapacityQueryDTO
.
Result
result
=
new
CapacityQueryDTO
.
Result
();
return
Result
.
success
(
result
);
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment