Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
谢明辉
/
recv_data_longhua
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 2b7277a0
authored
Aug 25, 2020
by
xmh
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
通了
1 parent
f0b6b57f
Hide whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
2266 additions
and
430 deletions
pom.xml
src/main/java/com/viontech/Application.java
src/main/java/com/viontech/controller/DataController.java
src/main/java/com/viontech/model/BaseModel.java
src/main/java/com/viontech/model/BehaviorModel.java
src/main/java/com/viontech/model/FlowModel.java
src/main/java/com/viontech/model/KeepAlive.java
src/main/java/com/viontech/model/LicensePlateRecord.java
src/main/java/com/viontech/model/LoginData.java
src/main/java/com/viontech/model/TrafficModel.java
src/main/java/com/viontech/netty/ChannelGroup.java
src/main/java/com/viontech/netty/NettyReceiverHandler.java
src/main/java/com/viontech/netty/NettyServer.java
src/main/java/com/viontech/process/BehaviorProcess.java
src/main/java/com/viontech/process/Process.java
src/main/java/com/viontech/process/TrafficProcess.java
src/main/java/com/viontech/utils/DateUtil.java
src/main/resources/fxlb.properties
pom.xml
View file @
2b7277a
...
...
@@ -25,17 +25,6 @@
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
com.viontech.keliu
</groupId>
<artifactId>
keliu-util
</artifactId>
<version>
6.0.7-SNAPSHOT
</version>
<exclusions>
<exclusion>
<artifactId>
maven-scm-provider-integrity
</artifactId>
<groupId>
org.apache.maven.scm
</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>
io.netty
</groupId>
<artifactId>
netty-all
</artifactId>
<version>
4.1.32.Final
</version>
...
...
@@ -60,6 +49,16 @@
<artifactId>
commons-pool2
</artifactId>
<version>
2.5.0
</version>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
1.2.73
</version>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
<version>
3.5
</version>
</dependency>
</dependencies>
<build>
...
...
src/main/java/com/viontech/Application.java
View file @
2b7277a
...
...
@@ -7,6 +7,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
(
exclude
=
org
.
springframework
.
boot
.
autoconfigure
.
jdbc
.
DataSourceAutoConfiguration
.
class
)
@Slf4j
public
class
Application
{
private
final
static
long
REAL_TIME
=
0x00000001
L
;
private
final
static
long
NOT_REAL_TIME
=
0x00000002
L
;
public
static
void
main
(
String
[]
args
)
{
try
{
...
...
@@ -15,4 +17,5 @@ public class Application {
log
.
error
(
"error"
,
e
);
}
}
}
src/main/java/com/viontech/controller/DataController.java
View file @
2b7277a
package
com
.
viontech
.
controller
;
import
com.viontech.model.BaseModel
;
import
com.viontech.netty.ChannelGroup
;
import
com.viontech.process.Process
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.sound.sampled.Line
;
/**
* .
*
...
...
@@ -13,16 +17,18 @@ import org.springframework.web.bind.annotation.RestController;
* @date 2020/8/18
*/
@RestController
@Slf4j
public
class
DataController
{
@Autowired
Process
process
;
@PostMapping
(
"/data"
)
public
Object
receiveData
(
@RequestBody
String
dataStr
)
throws
Exception
{
process
.
process
(
dataStr
);
return
null
;
public
Object
receiveData
(
@RequestBody
String
dataStr
)
throws
Exception
{
log
.
info
(
"收到一条繁星发来的消息"
);
BaseModel
data
=
Process
.
process
(
dataStr
);
if
(
data
!=
null
)
{
data
.
encodeData
();
ChannelGroup
.
broadcast
(
data
);
}
return
"success"
;
}
...
...
src/main/java/com/viontech/model/BaseModel.java
View file @
2b7277a
...
...
@@ -15,10 +15,10 @@ import lombok.experimental.Accessors;
@Setter
@Accessors
(
chain
=
true
)
public
class
BaseModel
{
protected
L
ong
deviceId
;
protected
L
ong
length
;
protected
Long
firstInt
;
protected
Long
secondInt
;
protected
l
ong
deviceId
;
protected
l
ong
length
;
protected
long
protocol
;
protected
long
flag
;
protected
byte
[]
data
;
...
...
@@ -51,8 +51,8 @@ public class BaseModel {
model
.
setDeviceId
(
deviceId
);
model
.
setLength
(
length
);
model
.
set
FirstInt
(
firstInt
);
model
.
set
SecondInt
(
secondInt
);
model
.
set
Protocol
(
firstInt
);
model
.
set
Flag
(
secondInt
);
if
(
length
>
16
)
{
model
.
data
=
new
byte
[(
int
)
(
length
-
16
)];
byteBuf
.
readBytes
(
model
.
data
);
...
...
@@ -61,8 +61,17 @@ public class BaseModel {
return
model
;
}
public
static
int
castLong2Int
(
long
data
)
{
return
(
int
)
(
data
&
0xFF
);
public
static
int
toInt
(
long
data
)
{
return
(
int
)
data
;
}
public
static
byte
[]
toBytes
(
int
n
)
{
byte
[]
b
=
new
byte
[
4
];
b
[
3
]
=
(
byte
)
(
n
&
0xff
);
b
[
2
]
=
(
byte
)
(
n
>>
8
&
0xff
);
b
[
1
]
=
(
byte
)
(
n
>>
16
&
0xff
);
b
[
0
]
=
(
byte
)
(
n
>>
24
&
0xff
);
return
b
;
}
/**
...
...
@@ -73,18 +82,24 @@ public class BaseModel {
public
void
to
(
ByteBuf
byteBuf
)
{
encodeData
();
byteBuf
.
writeInt
(
castLong2
Int
(
deviceId
));
byteBuf
.
writeInt
(
castLong2
Int
(
length
));
byteBuf
.
writeInt
(
castLong2Int
(
firstInt
));
byteBuf
.
writeInt
(
castLong2Int
(
secondInt
));
byteBuf
.
writeInt
(
to
Int
(
deviceId
));
byteBuf
.
writeInt
(
to
Int
(
length
));
byteBuf
.
writeInt
(
toInt
(
protocol
));
byteBuf
.
writeInt
(
toInt
(
flag
));
if
(
data
!=
null
&&
data
.
length
>
0
)
{
byteBuf
.
writeBytes
(
data
);
}
}
protected
void
encodeData
()
{
/**
* 重写此方法,将相关的字段组装成 byte[] 并赋值给 data
*/
public
void
encodeData
()
{
}
protected
void
decodeData
()
{
/**
* 重写此方法,将 byte[] 解析到子类的字段中
*/
public
void
decodeData
()
{
}
}
src/main/java/com/viontech/model/BehaviorModel.java
0 → 100644
View file @
2b7277a
package
com
.
viontech
.
model
;
import
com.fasterxml.jackson.core.util.ByteArrayBuilder
;
import
lombok.Getter
;
import
lombok.Setter
;
/**
* .
*
* @author 谢明辉
* @date 2020/8/24
*/
@Getter
@Setter
public
class
BehaviorModel
extends
BaseModel
{
private
final
byte
[]
licensePlateText
=
new
byte
[
16
];
private
final
byte
[]
recordingPath
=
new
byte
[
128
];
private
final
byte
[]
eventLocation
=
new
byte
[
32
];
private
final
byte
[]
eventSnapshotPath
=
new
byte
[
128
];
private
int
serialNum
;
private
int
recordingStartSecond
;
private
int
recordingStartMillisecond
;
private
int
recordingEndSecond
;
private
int
recordingEndMillisecond
;
private
int
eventStartSecond
;
private
int
eventStartMillisecond
;
private
int
eventEndSecond
;
private
int
eventEndMillisecond
;
private
int
eventCode
;
private
int
laneNo
;
private
int
pictureSize
;
private
int
pictureWidth
;
private
int
pictureHeight
;
private
int
x
;
private
int
y
;
private
int
vehicleType
;
private
int
color1
;
private
int
speed
;
private
int
direction
;
private
int
color2
;
private
int
color3
;
private
int
color1Weights
;
private
int
color2Weights
;
private
int
color3Weights
;
private
int
vehicleCategory
;
private
int
reservedField
;
private
int
recordingDuration
;
private
int
faceWidth
;
private
int
faceHeight
;
private
byte
[]
reservedField2
=
new
byte
[
24
];
private
byte
[]
picture
;
@Override
public
void
encodeData
()
{
this
.
length
=
16L
+
448L
+
picture
.
length
;
this
.
protocol
=
0x00040006
L
;
ByteArrayBuilder
builder
=
new
ByteArrayBuilder
(
Math
.
toIntExact
(
this
.
length
-
16
));
builder
.
appendFourBytes
(
serialNum
);
builder
.
appendFourBytes
(
recordingStartSecond
);
builder
.
appendFourBytes
(
recordingStartMillisecond
);
builder
.
appendFourBytes
(
recordingEndSecond
);
builder
.
appendFourBytes
(
recordingEndMillisecond
);
builder
.
appendFourBytes
(
eventStartSecond
);
builder
.
appendFourBytes
(
eventStartMillisecond
);
builder
.
appendFourBytes
(
eventEndSecond
);
builder
.
appendFourBytes
(
eventEndMillisecond
);
builder
.
appendFourBytes
(
laneNo
);
builder
.
appendFourBytes
(
pictureSize
);
builder
.
appendFourBytes
(
pictureWidth
);
builder
.
appendFourBytes
(
pictureHeight
);
builder
.
appendFourBytes
(
x
);
builder
.
appendFourBytes
(
y
);
builder
.
write
(
licensePlateText
);
builder
.
write
(
recordingPath
);
builder
.
appendFourBytes
(
vehicleType
);
builder
.
appendFourBytes
(
color1
);
builder
.
appendFourBytes
(
speed
);
builder
.
appendFourBytes
(
direction
);
builder
.
write
(
eventLocation
);
builder
.
write
(
eventSnapshotPath
);
builder
.
appendFourBytes
(
color2
);
builder
.
appendFourBytes
(
color3
);
builder
.
appendFourBytes
(
color1Weights
);
builder
.
appendFourBytes
(
color2Weights
);
builder
.
appendFourBytes
(
color3Weights
);
builder
.
appendFourBytes
(
vehicleCategory
);
builder
.
appendFourBytes
(
reservedField
);
builder
.
appendFourBytes
(
recordingDuration
);
builder
.
appendFourBytes
(
faceWidth
);
builder
.
appendFourBytes
(
faceHeight
);
builder
.
write
(
reservedField2
);
builder
.
write
(
picture
);
// todo 待crc校验
this
.
data
=
builder
.
resetAndGetFirstSegment
();
}
}
src/main/java/com/viontech/model/FlowModel.java
0 → 100644
View file @
2b7277a
package
com
.
viontech
.
model
;
import
com.fasterxml.jackson.core.util.ByteArrayBuilder
;
import
lombok.Getter
;
import
lombok.Setter
;
/**
* .
*
* @author 谢明辉
* @date 2020/8/24
*/
@Getter
@Setter
public
class
FlowModel
extends
BaseModel
{
private
final
int
[]
laneNoAndType
=
new
int
[
16
];
private
final
int
[]
traffic
=
new
int
[
16
];
private
final
int
[]
avgSpeed
=
new
int
[
16
];
private
final
int
[]
maxQueueLen
=
new
int
[
16
];
private
final
int
[]
avgDistanceBetweenVehicle
=
new
int
[
16
];
private
final
int
[]
trafficSupplement
=
new
int
[
16
];
private
int
serialNum
;
private
int
time
;
private
int
timeDuring
;
private
byte
[]
reservedField
=
new
byte
[
64
];
@Override
public
void
encodeData
()
{
this
.
length
=
540L
;
this
.
protocol
=
0x00040005
L
;
ByteArrayBuilder
builder
=
new
ByteArrayBuilder
(
Math
.
toIntExact
(
this
.
length
-
16
));
builder
.
appendFourBytes
(
toInt
(
deviceId
));
builder
.
appendFourBytes
(
toInt
(
length
));
builder
.
appendFourBytes
(
toInt
(
protocol
));
builder
.
appendFourBytes
(
toInt
(
flag
));
builder
.
appendFourBytes
(
serialNum
);
builder
.
appendFourBytes
(
time
);
builder
.
appendFourBytes
(
timeDuring
);
for
(
int
value
:
laneNoAndType
)
{
builder
.
appendFourBytes
(
value
);
}
for
(
int
value
:
traffic
)
{
builder
.
appendFourBytes
(
value
);
}
for
(
int
value
:
avgSpeed
)
{
builder
.
appendFourBytes
(
value
);
}
for
(
int
value
:
maxQueueLen
)
{
builder
.
appendFourBytes
(
value
);
}
for
(
int
value
:
avgDistanceBetweenVehicle
)
{
builder
.
appendFourBytes
(
value
);
}
for
(
int
value
:
trafficSupplement
)
{
builder
.
appendFourBytes
(
value
);
}
builder
.
write
(
reservedField
);
this
.
data
=
builder
.
resetAndGetFirstSegment
();
}
}
src/main/java/com/viontech/model/KeepAlive.java
View file @
2b7277a
...
...
@@ -11,12 +11,12 @@ public class KeepAlive extends BaseModel{
@Override
p
rotected
void
encodeData
()
{
p
ublic
void
encodeData
()
{
}
@Override
p
rotected
void
decodeData
()
{
p
ublic
void
decodeData
()
{
}
}
src/main/java/com/viontech/model/LicensePlateRecord.java
deleted
100644 → 0
View file @
f0b6b57
package
com
.
viontech
.
model
;
/**
* .
*
* @author 谢明辉
* @date 2020/8/21
*/
public
class
LicensePlateRecord
extends
BaseModel
{
/** 序列号 */
private
Long
serialNum
;
/** 识别车牌时间(秒) */
private
Long
discernSecondTime
;
/** 识别车牌时间(毫秒) */
private
Long
discernMillisecondTime
;
/** 车牌文字 */
private
String
LicensePlateText
;
/** 车牌类型 */
private
Long
LicensePlateType
;
/** 识别可靠度 */
private
Long
confidence
;
/** 车道 */
private
Long
lane
;
/** 车辆类型 */
private
Long
vehicleType
;
/** 图片大小宽高 */
private
Long
secondPictureSize
;
private
Long
secondPictureWidth
;
private
Long
secondPictureHeight
;
private
Long
firstPictureSize
;
private
Long
firstPictureWidth
;
private
Long
firstPictureHeight
;
/** 车牌在第一张图片两种 左上右下 位置 */
private
Long
patternLeft
;
private
Long
patternTop
;
private
Long
patternRight
;
private
Long
patternBottom
;
@Override
protected
void
encodeData
()
{
this
.
firstInt
=
0x00020004
L
;
}
}
src/main/java/com/viontech/model/LoginData.java
View file @
2b7277a
...
...
@@ -19,13 +19,13 @@ public class LoginData extends BaseModel {
private
String
password
;
@Override
p
rotected
void
decodeData
()
{
this
.
username
=
new
String
(
data
,
0
,
32
,
StandardCharsets
.
UTF_8
);
this
.
password
=
new
String
(
data
,
32
,
32
,
StandardCharsets
.
UTF_8
);
p
ublic
void
decodeData
()
{
this
.
username
=
new
String
(
data
,
0
,
32
,
StandardCharsets
.
UTF_8
)
.
trim
()
;
this
.
password
=
new
String
(
data
,
32
,
32
,
StandardCharsets
.
UTF_8
)
.
trim
()
;
}
@Override
p
rotected
void
encodeData
()
{
p
ublic
void
encodeData
()
{
}
}
src/main/java/com/viontech/model/TrafficModel.java
0 → 100644
View file @
2b7277a
package
com
.
viontech
.
model
;
import
com.fasterxml.jackson.core.util.ByteArrayBuilder
;
import
lombok.Getter
;
import
lombok.Setter
;
import
lombok.experimental.Accessors
;
import
java.util.zip.CRC32
;
/**
* 发送车牌记录,包含电警记录
*
* @author 谢明辉
* @date 2020/8/21
*/
@Getter
@Setter
@Accessors
(
chain
=
true
)
public
class
TrafficModel
extends
BaseModel
{
/** 车牌文字 */
private
final
byte
[]
plateText
=
new
byte
[
16
];
/** 经过地点 */
private
final
byte
[]
routeLocation
=
new
byte
[
32
];
/** 录像路径 */
private
final
byte
[]
videoPath
=
new
byte
[
128
];
/** 第一张照片路径 */
private
final
byte
[]
picturePath
=
new
byte
[
128
];
/** 序列号 */
private
int
serialNum
;
/** 识别车牌时间(秒) */
private
int
discernTime
;
/** 识别车牌时间(毫秒) */
private
int
discernMillisecondTime
;
/** 车牌类型 */
private
int
plateType
;
/** 车牌识别可靠度 */
private
int
confidence
;
/** 车道 */
private
int
lane
;
/** 车辆类型 */
private
int
vehicleType
;
/** 图片大小宽高 */
private
int
secondPictureSize
;
private
int
secondPictureWidth
;
private
int
secondPictureHeight
;
private
int
firstPictureSize
;
private
int
firstPictureWidth
;
private
int
firstPictureHeight
;
/** 车牌在第一张图片两种 左上右下 位置 */
private
int
patternLeft
;
private
int
patternTop
;
private
int
patternRight
;
private
int
patternBottom
;
/** 车身颜色 */
private
int
color
;
/** 车速 */
private
int
speed
;
/** 方向 */
private
int
direction
;
/** 厂商标志 */
private
int
manufacturerLogo
;
/** 三个预留位 */
private
int
reservedField
;
private
int
reservedField2
;
private
int
reservedField3
;
/** 车型细分 */
private
int
vehicleCategory
;
/** 车牌结构 1单行2双行3其他 */
private
int
plateStructure
;
/** 违章类型 */
private
int
illegalType
;
/** 预留位 */
private
int
reservedField4
;
/** 第二张图片抓拍时间 */
private
int
secondPictureTime
;
private
int
secondPictureMillisecondTime
;
/** 红灯开始时间 */
private
int
redLightBeginTime
;
private
int
redLightBeginMillisecondTime
;
/** 红灯结束时间 */
private
int
redLightEndTime
;
private
int
redLightEndMillisecondTime
;
/** 限速值 */
private
int
speedLimit
;
/** 超速抓拍值 */
private
int
overSpeedThreshold
;
/** 预留字段 */
private
byte
[]
reservedField5
=
new
byte
[
4
];
/** picture1 */
private
byte
[]
picture1
=
new
byte
[
0
];
/** picture2 */
private
byte
[]
picture2
=
new
byte
[
0
];
@Override
public
void
encodeData
()
{
this
.
protocol
=
0x00020004
L
;
this
.
length
=
16L
+
452L
+
picture1
.
length
+
picture2
.
length
+
4
;
ByteArrayBuilder
builder
=
new
ByteArrayBuilder
(
Math
.
toIntExact
(
this
.
length
-
16
-
4
));
builder
.
appendFourBytes
(
serialNum
);
builder
.
appendFourBytes
(
discernTime
);
builder
.
appendFourBytes
(
discernMillisecondTime
);
builder
.
write
(
plateText
);
builder
.
appendFourBytes
(
plateType
);
builder
.
appendFourBytes
(
confidence
);
builder
.
appendFourBytes
(
lane
);
builder
.
appendFourBytes
(
vehicleType
);
builder
.
appendFourBytes
(
secondPictureSize
);
builder
.
appendFourBytes
(
secondPictureWidth
);
builder
.
appendFourBytes
(
secondPictureHeight
);
builder
.
appendFourBytes
(
firstPictureSize
);
builder
.
appendFourBytes
(
firstPictureWidth
);
builder
.
appendFourBytes
(
firstPictureHeight
);
builder
.
appendFourBytes
(
patternLeft
);
builder
.
appendFourBytes
(
patternTop
);
builder
.
appendFourBytes
(
patternRight
);
builder
.
appendFourBytes
(
patternBottom
);
builder
.
appendFourBytes
(
color
);
builder
.
appendFourBytes
(
speed
);
builder
.
appendFourBytes
(
direction
);
builder
.
appendFourBytes
(
manufacturerLogo
);
builder
.
write
(
routeLocation
);
builder
.
write
(
videoPath
);
builder
.
write
(
picturePath
);
builder
.
appendFourBytes
(
reservedField
);
builder
.
appendFourBytes
(
reservedField2
);
builder
.
appendFourBytes
(
reservedField3
);
builder
.
appendFourBytes
(
vehicleCategory
);
builder
.
appendFourBytes
(
plateStructure
);
builder
.
appendFourBytes
(
illegalType
);
builder
.
appendFourBytes
(
reservedField4
);
builder
.
appendFourBytes
(
secondPictureTime
);
builder
.
appendFourBytes
(
secondPictureMillisecondTime
);
builder
.
appendFourBytes
(
redLightBeginTime
);
builder
.
appendFourBytes
(
redLightBeginMillisecondTime
);
builder
.
appendFourBytes
(
redLightEndTime
);
builder
.
appendFourBytes
(
redLightEndMillisecondTime
);
builder
.
appendFourBytes
(
speedLimit
);
builder
.
appendFourBytes
(
overSpeedThreshold
);
builder
.
write
(
reservedField5
);
builder
.
write
(
picture1
);
builder
.
write
(
picture2
);
byte
[]
bytes
=
builder
.
resetAndGetFirstSegment
();
int
count
=
bytes
.
length
;
// crc32
CRC32
crc32
=
new
CRC32
();
crc32
.
update
(
bytes
);
int
value
=
(
int
)
crc32
.
getValue
();
for
(
int
i
=
0
;
i
<
count
;
i
++)
{
bytes
[
i
]
=
(
byte
)
(
bytes
[
i
]
^
0x94
);
}
this
.
data
=
new
byte
[
count
+
4
];
System
.
arraycopy
(
bytes
,
0
,
data
,
0
,
count
);
this
.
data
[
count
++]
=
(
byte
)
(
value
>>
24
);
this
.
data
[
count
++]
=
(
byte
)
(
value
>>
16
);
this
.
data
[
count
++]
=
(
byte
)
(
value
>>
8
);
this
.
data
[
count
]
=
(
byte
)
value
;
}
}
src/main/java/com/viontech/netty/ChannelGroup.java
View file @
2b7277a
package
com
.
viontech
.
netty
;
import
com.viontech.model.BaseModel
;
import
io.netty.channel.Channel
;
import
lombok.extern.slf4j.Slf4j
;
...
...
@@ -56,7 +57,7 @@ public class ChannelGroup {
*
* @param msg 消息
*/
public
static
void
broadcast
(
Object
msg
)
{
public
static
void
broadcast
(
BaseModel
msg
)
{
for
(
Channel
channel
:
DEVICE_CHANNEL_MAP
.
values
())
{
try
{
channel
.
writeAndFlush
(
msg
).
await
();
...
...
src/main/java/com/viontech/netty/NettyReceiverHandler.java
View file @
2b7277a
...
...
@@ -29,15 +29,19 @@ public class NettyReceiverHandler extends ChannelInboundHandlerAdapter {
//注册命令
if
(
message
instanceof
LoginData
)
{
BaseModel
result
=
new
BaseModel
().
setDeviceId
(
0L
).
setLength
(
16L
).
set
FirstInt
(
0x00020100
L
);
BaseModel
result
=
new
BaseModel
().
setDeviceId
(
0L
).
setLength
(
16L
).
set
Protocol
(
0x00020100
L
);
LoginData
logindata
=
(
LoginData
)
message
;
Long
deviceId
=
logindata
.
getDeviceId
();
System
.
out
.
println
(
logindata
.
getUsername
());
System
.
out
.
println
(
logindata
.
getPassword
());
if
(
"admin"
.
equals
(
logindata
.
getUsername
())
&&
"admin"
.
equals
(
logindata
.
getPassword
()))
{
ChannelGroup
.
registered
(
deviceId
,
ctx
.
channel
());
result
.
set
SecondInt
(
0L
);
result
.
set
Flag
(
0L
);
}
else
{
result
.
set
SecondInt
(
1L
);
result
.
set
Flag
(
1L
);
}
ctx
.
writeAndFlush
(
result
).
await
();
}
else
if
(
message
instanceof
KeepAlive
)
{
...
...
@@ -51,6 +55,7 @@ public class NettyReceiverHandler extends ChannelInboundHandlerAdapter {
@Override
public
void
exceptionCaught
(
ChannelHandlerContext
ctx
,
Throwable
cause
)
throws
Exception
{
super
.
exceptionCaught
(
ctx
,
cause
);
cause
.
printStackTrace
();
}
@Override
...
...
src/main/java/com/viontech/netty/NettyServer.java
View file @
2b7277a
...
...
@@ -41,7 +41,7 @@ public class NettyServer implements CommandLineRunner {
@Override
protected
void
initChannel
(
SocketChannel
ch
)
{
ch
.
pipeline
().
addLast
(
new
LoggingHandler
(
LogLevel
.
INFO
));
ch
.
pipeline
().
addLast
(
new
LengthFieldBasedFrameDecoder
(
1024
*
1024
*
1000
,
4
,
4
,
-
16
,
0
));
ch
.
pipeline
().
addLast
(
new
LengthFieldBasedFrameDecoder
(
1024
*
1024
*
1000
,
4
,
4
,
-
8
,
0
));
ch
.
pipeline
().
addLast
(
new
ByteToMessageCodecHandler
());
ch
.
pipeline
().
addLast
(
new
NettyReceiverHandler
());
}
...
...
src/main/java/com/viontech/process/BehaviorProcess.java
View file @
2b7277a
package
com
.
viontech
.
process
;
import
io.netty.buffer.ByteBuf
;
import
com.alibaba.fastjson.JSONObject
;
import
com.sun.corba.se.impl.orb.ParserTable
;
import
com.viontech.model.BaseModel
;
import
java.text.DateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Map
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Arrays
;
import
java.util.zip.CRC32
;
public
class
BehaviorProcess
extends
Process
{
public
class
BehaviorProcess
implements
Process
{
@Override
public
void
getData
(
Map
jsonMap
,
ByteBuf
byteBuf
)
throws
Exception
{
if
(!
jsonMap
.
get
(
"event_cate"
).
equals
(
"behavior"
)){
return
;
}
Integer
event_refid
=(
Integer
)
jsonMap
.
get
(
"event_refid"
);
byteBuf
.
writeInt
(
event_refid
);
DateFormat
dateFormat
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
if
(
jsonMap
.
get
(
"video"
)!=
null
){
List
<
Map
>
videoList
=
(
List
<
Map
>)
jsonMap
.
get
(
"video"
);
for
(
Map
map
:
videoList
)
{
Date
start_dt
=
(
Date
)
map
.
get
(
"start_dt"
);
Date
end_dt
=
(
Date
)
map
.
get
(
"end_dt"
);
Date
startDateSecond
=
dateFormat
.
parse
(
dateFormat
.
format
(
start_dt
));
Date
endDateSecond
=
dateFormat
.
parse
(
dateFormat
.
format
(
end_dt
));
byteBuf
.
writeInt
((
int
)
startDateSecond
.
getTime
());
byteBuf
.
writeInt
((
int
)
start_dt
.
getTime
());
byteBuf
.
writeInt
((
int
)
endDateSecond
.
getTime
());
byteBuf
.
writeInt
((
int
)
end_dt
.
getTime
());
}
}
else
{
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
}
Date
event_dt
=
(
Date
)
jsonMap
.
get
(
"event_dt"
);
Date
eventDateSecond
=
dateFormat
.
parse
(
dateFormat
.
format
(
event_dt
));
byteBuf
.
writeInt
((
int
)
eventDateSecond
.
getTime
());
byteBuf
.
writeInt
((
int
)
event_dt
.
getTime
());
String
event_type
=
(
String
)
jsonMap
.
get
(
"event_type"
);
//事件类型需要转换
convertBehaviorCode
(
event_type
,
byteBuf
);
Integer
laneNumber
=
0
;
String
plate
=
""
;
String
carType
=
""
;
String
carColor
=
""
;
Integer
speed
=
0
;
String
direction
=
""
;
String
locationName
=
""
;
if
(
jsonMap
.
get
(
"event_data"
)!=
null
){
Map
event_data
=
(
Map
)
jsonMap
.
get
(
"event_data"
);
if
(
event_data
.
get
(
"lane"
)!=
null
){
Map
laneMap
=
(
Map
)
event_data
.
get
(
"lane"
);
if
(
laneMap
.
get
(
"number"
)!=
null
){
laneNumber
=(
Integer
)
laneMap
.
get
(
"number"
);
}
}
if
(
event_data
.
get
(
"vehicle"
)!=
null
){
Map
vehicle
=
(
Map
)
event_data
.
get
(
"vehicle"
);
if
(
vehicle
.
get
(
"plate"
)!=
null
){
Map
plateMap
=
(
Map
)
vehicle
.
get
(
"plate"
);
plate
=
(
String
)
plateMap
.
get
(
"text"
);
}
}
if
(
event_data
.
get
(
"uservehicle"
)!=
null
){
Map
userVehicle
=
(
Map
)
event_data
.
get
(
"uservehicle"
);
carType
=
(
String
)
userVehicle
.
get
(
"vehicletype"
);
carColor
=
(
String
)
userVehicle
.
get
(
"vehiclecolor"
);
}
speed
=
Integer
.
valueOf
(
String
.
valueOf
((
Double
)
event_data
.
get
(
"speed"
)));
if
(
event_data
.
get
(
"location"
)!=
null
){
Map
locationMap
=
(
Map
)
event_data
.
get
(
"location"
);
if
(
locationMap
.
get
(
"drive_direction"
)!=
null
){
Map
directionMap
=
(
Map
)
locationMap
.
get
(
"drive_direction"
);
direction
=
(
String
)
directionMap
.
get
(
"code"
);
}
locationName
=
(
String
)
locationMap
.
get
(
"name"
);
}
}
byteBuf
.
writeInt
(
laneNumber
);
if
(
jsonMap
.
get
(
"pics"
)!=
null
){
List
<
Map
>
pics
=
(
List
<
Map
>)
jsonMap
.
get
(
"pics"
);
String
picbase64
=
(
String
)
pics
.
get
(
0
).
get
(
"pic_base64"
);
byteBuf
.
writeInt
(
picbase64
.
getBytes
().
length
);
byteBuf
.
writeInt
((
Integer
)
pics
.
get
(
0
).
get
(
"width"
));
byteBuf
.
writeInt
((
Integer
)
pics
.
get
(
0
).
get
(
"hight"
));
//这两个坐标不知道怎么赋值
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
}
else
{
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
}
//车牌需要转换一下
convertPlate
(
plate
,
byteBuf
);
//事件录像存储位置不知道怎么赋值
byteBuf
.
writeBytes
(
new
byte
[
128
]);
//车辆类型没法给需要转换
convertCarType
(
carType
,
byteBuf
);
//车身颜色需要转换
convertCarColor
(
carColor
,
byteBuf
);
byteBuf
.
writeInt
(
speed
);
//行驶方向需要转换
convertDirection
(
direction
,
byteBuf
);
//地点名称暂时没有赋值,确认是不是用locationName
byteBuf
.
writeBytes
(
new
byte
[
32
]);
//事件快照路径不知道怎么赋值
byteBuf
.
writeBytes
(
new
byte
[
128
]);
//车身颜色2,3和权重暂时未赋值
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
//车型细分暂时未赋值
byteBuf
.
writeInt
(
0
);
//预置位我不知道怎么赋值
byteBuf
.
writeInt
(
0
);
//录像时长
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeBytes
(
new
byte
[
24
]);
}
public
void
convertBehaviorCode
(
String
event_type
,
ByteBuf
byteBuf
){
}
public
void
convertPlate
(
String
plate
,
ByteBuf
byteBuf
){
}
public
void
convertCarType
(
String
carType
,
ByteBuf
byteBuf
){
}
public
void
convertCarColor
(
String
carColor
,
ByteBuf
byteBuf
){
}
public
void
convertDirection
(
String
direction
,
ByteBuf
byteBuf
){
public
BaseModel
process
(
JSONObject
jsonObject
)
{
//
//
// if(!jsonMap.get("event_cate").equals("behavior")){
// return;
// }
// Integer event_refid=(Integer) jsonMap.get("event_refid");
// byteBuf.writeInt(event_refid);
// DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// if(jsonMap.get("video")!=null){
// List<Map> videoList= (List<Map>) jsonMap.get("video");
// for (Map map : videoList) {
// Date start_dt= (Date) map.get("start_dt");
// Date end_dt= (Date) map.get("end_dt");
// Date startDateSecond=dateFormat.parse(dateFormat.format(start_dt));
// Date endDateSecond=dateFormat.parse(dateFormat.format(end_dt));
// byteBuf.writeInt((int) startDateSecond.getTime());
// byteBuf.writeInt((int) start_dt.getTime());
// byteBuf.writeInt((int) endDateSecond.getTime());
// byteBuf.writeInt((int) end_dt.getTime());
// }
// }else{
// byteBuf.writeInt(0);
// byteBuf.writeInt(0);
// byteBuf.writeInt(0);
// byteBuf.writeInt(0);
// }
// Date event_dt= (Date) jsonMap.get("event_dt");
// Date eventDateSecond=dateFormat.parse(dateFormat.format(event_dt));
// byteBuf.writeInt((int) eventDateSecond.getTime());
// byteBuf.writeInt((int) event_dt.getTime());
// String event_type= (String) jsonMap.get("event_type");
// //事件类型需要转换
// convertBehaviorCode(event_type,byteBuf);
// Integer laneNumber=0;
// String plate="";
// String carType="";
// String carColor="";
// Integer speed=0;
// String direction="";
// String locationName="";
// if(jsonMap.get("event_data")!=null){
// Map event_data= (Map) jsonMap.get("event_data");
// if(event_data.get("lane")!=null){
// Map laneMap= (Map) event_data.get("lane");
// if(laneMap.get("number")!=null){
// laneNumber=(Integer) laneMap.get("number");
// }
// }
// if(event_data.get("vehicle")!=null){
// Map vehicle= (Map) event_data.get("vehicle");
// if(vehicle.get("plate")!=null){
// Map plateMap= (Map) vehicle.get("plate");
// plate= (String) plateMap.get("text");
// }
// }
// if(event_data.get("uservehicle")!=null){
// Map userVehicle= (Map) event_data.get("uservehicle");
// carType= (String) userVehicle.get("vehicletype");
// carColor= (String) userVehicle.get("vehiclecolor");
// }
// speed=Integer.valueOf(String.valueOf((Double)event_data.get("speed")));
// if(event_data.get("location")!=null){
// Map locationMap= (Map) event_data.get("location");
// if(locationMap.get("drive_direction")!=null){
// Map directionMap= (Map) locationMap.get("drive_direction");
// direction= (String) directionMap.get("code");
// }
// locationName= (String) locationMap.get("name");
// }
// }
// byteBuf.writeInt(laneNumber);
// if(jsonMap.get("pics")!=null){
// List<Map> pics= (List<Map>) jsonMap.get("pics");
// String picbase64= (String) pics.get(0).get("pic_base64");
// byteBuf.writeInt(picbase64.getBytes().length);
// byteBuf.writeInt((Integer) pics.get(0).get("width"));
// byteBuf.writeInt((Integer) pics.get(0).get("hight"));
// //这两个坐标不知道怎么赋值
// byteBuf.writeInt(0);
// byteBuf.writeInt(0);
// }else{
// byteBuf.writeInt(0);
// byteBuf.writeInt(0);
// byteBuf.writeInt(0);
// byteBuf.writeInt(0);
// byteBuf.writeInt(0);
// }
// //车牌需要转换一下
// convertPlate(plate,byteBuf);
// //事件录像存储位置不知道怎么赋值
// byteBuf.writeBytes(new byte[128]);
// //车辆类型没法给需要转换
// convertCarType(carType,byteBuf);
// //车身颜色需要转换
// convertCarColor(carColor,byteBuf);
// byteBuf.writeInt(speed);
// //行驶方向需要转换
// convertDirection(direction,byteBuf);
// //地点名称暂时没有赋值,确认是不是用locationName
// byteBuf.writeBytes(new byte[32]);
// //事件快照路径不知道怎么赋值
// byteBuf.writeBytes(new byte[128]);
//
// //车身颜色2,3和权重暂时未赋值
// byteBuf.writeInt(0);
// byteBuf.writeInt(0);
// byteBuf.writeInt(0);
// byteBuf.writeInt(0);
// byteBuf.writeInt(0);
// //车型细分暂时未赋值
// byteBuf.writeInt(0);
// //预置位我不知道怎么赋值
// byteBuf.writeInt(0);
// //录像时长
// byteBuf.writeInt(0);
// byteBuf.writeInt(0);
// byteBuf.writeInt(0);
// byteBuf.writeBytes(new byte[24]);
return
null
;
}
}
src/main/java/com/viontech/process/Process.java
View file @
2b7277a
package
com
.
viontech
.
process
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBufAllocator
;
import
java.util.Map
;
import
com.alibaba.fastjson.JSONObject
;
import
com.viontech.model.BaseModel
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
/**
* .
...
...
@@ -12,23 +11,31 @@ import java.util.Map;
* @author 谢明辉
* @date 2020/8/20
*/
public
interface
Process
{
Logger
log
=
LoggerFactory
.
getLogger
(
Process
.
class
);
public
abstract
class
Process
{
public
void
process
(
String
jsonStr
)
throws
Exception
{
ByteBuf
byteBuf
=
ByteBufAllocator
.
DEFAULT
.
ioBuffer
();
ObjectMapper
objectMapper
=
new
ObjectMapper
();
Map
jsonMap
=
objectMapper
.
readValue
(
jsonStr
,
Map
.
class
);
String
vchan_refid
=
(
String
)
jsonMap
.
get
(
"vchan_refid"
);
//封装包头,暂未赋值
byteBuf
.
writeInt
(
0
);
//相机编号为String,他们要的是int,暂时保留
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
//封装包体
getData
(
jsonMap
,
byteBuf
);
/**
* 分类处理繁星发来的数据
*
* @param jsonStr 繁星发来的数据
*
* @return 转换成对接方需要的
*/
static
BaseModel
process
(
String
jsonStr
)
throws
Exception
{
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
jsonStr
);
String
eventCate
=
jsonObject
.
getString
(
"event_cate"
);
if
(
eventCate
==
null
)
{
return
null
;
}
switch
(
eventCate
)
{
case
"traffic"
:
return
new
TrafficProcess
().
process
(
jsonObject
);
case
"behavior"
:
return
new
BehaviorProcess
().
process
(
jsonObject
);
default
:
return
null
;
}
}
public
abstract
void
getData
(
Map
jsonMap
,
ByteBuf
byteBuf
)
throws
Exception
;
BaseModel
process
(
JSONObject
jsonObject
)
throws
Exception
;
}
src/main/java/com/viontech/process/TrafficProcess.java
View file @
2b7277a
package
com
.
viontech
.
process
;
import
io.netty.buffer.ByteBuf
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
com.viontech.model.BaseModel
;
import
com.viontech.model.TrafficModel
;
import
com.viontech.utils.DateUtil
;
import
java.text.DateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Map
;
import
java.nio.charset.StandardCharsets
;
import
java.text.ParseException
;
import
java.util.Base64
;
import
java.util.Optional
;
public
class
TrafficProcess
extends
Process
{
public
class
TrafficProcess
implements
Process
{
@Override
public
void
getData
(
Map
jsonMap
,
ByteBuf
byteBuf
)
throws
Exception
{
if
(!
jsonMap
.
get
(
"event_cate"
).
equals
(
"traffic"
)){
return
;
}
Integer
event_refid
=(
Integer
)
jsonMap
.
get
(
"event_refid"
);
byteBuf
.
writeInt
(
event_refid
);
DateFormat
dateFormat
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
Date
event_dt
=
(
Date
)
jsonMap
.
get
(
"event_dt"
);
Date
eventDateSecond
=
dateFormat
.
parse
(
dateFormat
.
format
(
event_dt
));
byteBuf
.
writeInt
((
int
)
eventDateSecond
.
getTime
());
byteBuf
.
writeInt
((
int
)
event_dt
.
getTime
());
String
plate
=
""
;
String
plateColor
=
""
;
Integer
plateScore
=
0
;
Integer
laneNumber
=
0
;
String
carType
=
""
;
String
carColor
=
""
;
Integer
speed
=
0
;
String
direction
=
""
;
String
logoType
=
""
;
String
locationName
=
""
;
String
illegalType
=
""
;
Integer
limit_speed
=
0
;
Integer
spark_speed
=
0
;
Integer
red_start_time
=
0
;
Integer
red_end_time
=
0
;
Integer
red_start_second
=
0
;
Integer
red_end_second
=
0
;
if
(
jsonMap
.
get
(
"event_data"
)!=
null
){
Map
event_data
=
(
Map
)
jsonMap
.
get
(
"event_data"
);
if
(
event_data
.
get
(
"vehicle"
)!=
null
){
Map
vehicle
=
(
Map
)
event_data
.
get
(
"vehicle"
);
if
(
vehicle
.
get
(
"plate"
)!=
null
){
Map
plateMap
=
(
Map
)
vehicle
.
get
(
"plate"
);
plate
=
(
String
)
plateMap
.
get
(
"text"
);
plateScore
=
(
Integer
)
plateMap
.
get
(
"score"
);
}
public
BaseModel
process
(
JSONObject
jsonObject
)
throws
Exception
{
TrafficModel
model
=
new
TrafficModel
();
JSONObject
eventData
=
jsonObject
.
getJSONObject
(
"event_data"
);
if
(
eventData
!=
null
)
{
JSONObject
vehicle
=
eventData
.
getJSONObject
(
"vehicle"
);
JSONObject
lane
=
eventData
.
getJSONObject
(
"lane"
);
JSONObject
location
=
eventData
.
getJSONObject
(
"location"
);
JSONObject
illegal
=
eventData
.
getJSONObject
(
"illegal"
);
JSONArray
pics
=
eventData
.
getJSONArray
(
"pics"
);
// ------------------- 车速 ------------------
Float
speedF
=
eventData
.
getFloat
(
"speed"
);
model
.
setSpeed
(
Optional
.
ofNullable
(
speedF
).
orElse
(
0
F
).
intValue
());
// ------------------------ 车道 --------------
if
(
lane
!=
null
)
{
Integer
laneNo
=
lane
.
getInteger
(
"number"
);
model
.
setLane
(
laneNo
);
}
if
(
event_data
.
get
(
"uservehicle"
)!=
null
){
Map
userVehicle
=
(
Map
)
event_data
.
get
(
"uservehicle"
);
plateColor
=
(
String
)
userVehicle
.
get
(
"platetype"
);
carType
=
(
String
)
userVehicle
.
get
(
"vehicletype"
);
carColor
=
(
String
)
userVehicle
.
get
(
"vehiclecolor"
);
logoType
=
(
String
)
userVehicle
.
get
(
"logotype"
);
// ------------------------ 车辆信息 车牌信息 --------------
String
eventTimeStr
=
jsonObject
.
getString
(
"event_dt"
);
long
eventTime
=
DateUtil
.
parse
(
DateUtil
.
FORMAT_FULL
,
eventTimeStr
).
getTime
();
model
.
setDiscernTime
(
BaseModel
.
toInt
(
eventTime
/
1000
));
model
.
setDiscernMillisecondTime
(
BaseModel
.
toInt
(
eventTime
%
1000
));
vehicle
(
model
,
vehicle
);
// ------------------------ 图片 --------------
pic
(
model
,
pics
);
// ---------------- 车速,方向,地点 --------------
location
(
model
,
location
);
// ------------------ 违章 --------------------
illegal
(
model
,
illegal
);
return
model
;
}
else
{
return
null
;
}
}
private
void
pic
(
TrafficModel
model
,
JSONArray
pics
)
{
if
(
pics
!=
null
&&
pics
.
size
()
>
0
)
{
// --------------- 图片2 ----------
if
(
pics
.
size
()
>
1
)
{
JSONObject
pic2
=
pics
.
getJSONObject
(
1
);
Integer
width
=
pic2
.
getInteger
(
"width"
);
Integer
height
=
pic2
.
getInteger
(
"hight"
);
model
.
setSecondPictureHeight
(
height
);
model
.
setSecondPictureWidth
(
width
);
model
.
setSecondPictureTime
(
model
.
getDiscernTime
());
model
.
setSecondPictureMillisecondTime
(
model
.
getDiscernMillisecondTime
());
String
picBase64
=
pic2
.
getString
(
"pic_base64"
);
byte
[]
pic2Bytes
=
Base64
.
getDecoder
().
decode
(
picBase64
);
model
.
setPicture2
(
pic2Bytes
);
model
.
setSecondPictureSize
(
pic2Bytes
.
length
);
}
if
(
event_data
.
get
(
"lane"
)!=
null
){
Map
laneMap
=
(
Map
)
event_data
.
get
(
"lane"
);
if
(
laneMap
.
get
(
"number"
)!=
null
){
laneNumber
=(
Integer
)
laneMap
.
get
(
"number"
);
// ------------------ 图片1 -------------
JSONObject
pic1
=
pics
.
getJSONObject
(
0
);
Integer
width
=
pic1
.
getInteger
(
"width"
);
Integer
height
=
pic1
.
getInteger
(
"hight"
);
model
.
setFirstPictureWidth
(
width
);
model
.
setFirstPictureHeight
(
height
);
String
picBase64
=
pic1
.
getString
(
"pic_base64"
);
byte
[]
pic1Bytes
=
Base64
.
getDecoder
().
decode
(
picBase64
);
model
.
setPicture1
(
pic1Bytes
);
model
.
setFirstPictureSize
(
pic1Bytes
.
length
);
byte
[]
pic1UrlBytes
=
pic1
.
getString
(
"src_url"
).
getBytes
();
System
.
arraycopy
(
pic1UrlBytes
,
0
,
model
.
getPicturePath
(),
0
,
Math
.
min
(
128
,
pic1UrlBytes
.
length
));
}
}
private
void
vehicle
(
TrafficModel
model
,
JSONObject
vehicle
)
{
if
(
vehicle
!=
null
)
{
JSONObject
plate
=
vehicle
.
getJSONObject
(
"plate"
);
// ----------------- 车牌 ------------------
if
(
plate
!=
null
)
{
byte
[]
text
=
plate
.
getString
(
"text"
).
getBytes
(
StandardCharsets
.
UTF_8
);
System
.
arraycopy
(
text
,
0
,
model
.
getPlateText
(),
0
,
Math
.
min
(
text
.
length
,
16
));
// todo 车牌类型待实现
model
.
setPlateType
(
1
);
JSONObject
rect
=
plate
.
getJSONObject
(
"rect"
);
if
(
rect
!=
null
)
{
Float
top
=
rect
.
getFloat
(
"top"
);
Float
left
=
rect
.
getFloat
(
"left"
);
Float
bottom
=
rect
.
getFloat
(
"bottom"
);
Float
right
=
rect
.
getFloat
(
"right"
);
model
.
setPatternTop
((
int
)
(
top
*
10
)).
setPatternLeft
((
int
)
(
left
*
10
))
.
setPatternBottom
((
int
)
(
bottom
*
10
)).
setPatternRight
((
int
)
(
right
*
10
));
}
Integer
plateScore
=
plate
.
getInteger
(
"score"
);
model
.
setConfidence
(
plateScore
);
// todo 车牌单双行没有
model
.
setPlateStructure
(
1
);
}
speed
=
Integer
.
valueOf
(
String
.
valueOf
((
Double
)
event_data
.
get
(
"speed"
)));
if
(
event_data
.
get
(
"location"
)!=
null
){
Map
locationMap
=
(
Map
)
event_data
.
get
(
"location"
);
if
(
locationMap
.
get
(
"drive_direction"
)!=
null
){
Map
directionMap
=
(
Map
)
locationMap
.
get
(
"drive_direction"
);
direction
=
(
String
)
directionMap
.
get
(
"code"
);
JSONObject
body
=
vehicle
.
getJSONObject
(
"body"
);
// -------------------- 车身 --------------------
if
(
body
!=
null
)
{
JSONObject
type
=
body
.
getJSONObject
(
"type"
);
if
(
type
!=
null
)
{
// todo 大车小车 (vehicleType) 待实现
model
.
setVehicleType
(
4
);
// todo 车型细分
String
categoryCode
=
type
.
getString
(
"code"
);
model
.
setVehicleCategory
(
1
);
}
locationName
=
(
String
)
locationMap
.
get
(
"name"
);
}
if
(
event_data
.
get
(
"illegal"
)!=
null
){
Map
illegalMap
=
(
Map
)
event_data
.
get
(
"illegal"
);
if
((
Integer
)
illegalMap
.
get
(
"state"
)==
1
){
illegalType
=
(
String
)
illegalMap
.
get
(
"code"
);
if
(
illegalMap
.
get
(
"detail"
)!=
null
){
Map
detailMap
=
(
Map
)
illegalMap
.
get
(
"detail"
);
Date
red_start_dt
=
(
Date
)
detailMap
.
get
(
"start_dt"
);
Date
red_end_dt
=
(
Date
)
detailMap
.
get
(
"end_dt"
);
Date
startDateSecond
=
dateFormat
.
parse
(
dateFormat
.
format
(
red_start_dt
));
Date
endDateSecond
=
dateFormat
.
parse
(
dateFormat
.
format
(
red_end_dt
));
red_start_time
=(
int
)
red_start_dt
.
getTime
();
red_end_time
=(
int
)
red_end_dt
.
getTime
();
red_start_second
=(
int
)
startDateSecond
.
getTime
();
red_end_second
=(
int
)
endDateSecond
.
getTime
();
limit_speed
=
Integer
.
valueOf
(
String
.
valueOf
((
Double
)
event_data
.
get
(
"limit_speed"
)));
spark_speed
=
Integer
.
valueOf
(
String
.
valueOf
((
Double
)
event_data
.
get
(
"spark_speed"
)));
}
JSONObject
color
=
body
.
getJSONObject
(
"color"
);
if
(
color
!=
null
)
{
// todo 车身颜色待转换
String
colorCode
=
color
.
getString
(
"code"
);
model
.
setColor
(
0
);
}
JSONObject
logo
=
body
.
getJSONObject
(
"logo"
);
if
(
logo
!=
null
)
{
String
code
=
logo
.
getString
(
"code"
);
// todo 车辆品牌待转换
model
.
setManufacturerLogo
(
Integer
.
parseInt
(
code
));
}
}
}
//车牌需要转换一下
convertPlate
(
plate
,
byteBuf
);
//车牌类型(颜色)需要转换一下
convertPlateColor
(
plateColor
,
byteBuf
);
byteBuf
.
writeInt
(
plateScore
);
byteBuf
.
writeInt
(
laneNumber
);
//车辆类型需要转换
convertCarType
(
carType
,
byteBuf
);
if
(
jsonMap
.
get
(
"pics"
)!=
null
){
List
<
Map
>
pics
=
(
List
<
Map
>)
jsonMap
.
get
(
"pics"
);
if
(
pics
.
size
()<
2
){
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
}
else
{
String
picbase64
=
(
String
)
pics
.
get
(
1
).
get
(
"pic_base64"
);
byteBuf
.
writeInt
(
picbase64
.
getBytes
().
length
);
byteBuf
.
writeInt
((
Integer
)
pics
.
get
(
1
).
get
(
"width"
));
byteBuf
.
writeInt
((
Integer
)
pics
.
get
(
1
).
get
(
"hight"
));
}
String
picbase64
=
(
String
)
pics
.
get
(
0
).
get
(
"pic_base64"
);
byteBuf
.
writeInt
(
picbase64
.
getBytes
().
length
);
byteBuf
.
writeInt
((
Integer
)
pics
.
get
(
0
).
get
(
"width"
));
byteBuf
.
writeInt
((
Integer
)
pics
.
get
(
0
).
get
(
"hight"
));
if
(
pics
.
get
(
0
).
get
(
"object_rect"
)!=
null
){
Map
object_rect
=
(
Map
)
pics
.
get
(
0
).
get
(
"object_rect"
);
byteBuf
.
writeInt
(
Integer
.
valueOf
(
String
.
valueOf
((
Double
)
object_rect
.
get
(
"left"
))));
byteBuf
.
writeInt
(
Integer
.
valueOf
(
String
.
valueOf
((
Double
)
object_rect
.
get
(
"top"
))));
byteBuf
.
writeInt
(
Integer
.
valueOf
(
String
.
valueOf
((
Double
)
object_rect
.
get
(
"right"
))));
byteBuf
.
writeInt
(
Integer
.
valueOf
(
String
.
valueOf
((
Double
)
object_rect
.
get
(
"bottom"
))));
}
else
{
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
}
}
else
{
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
}
//车身颜色需要转换
convertCarColor
(
carColor
,
byteBuf
);
byteBuf
.
writeInt
(
speed
);
//行驶方向需要转换
convertDirection
(
direction
,
byteBuf
);
//车商标志需要转换
convertLogo
(
logoType
,
byteBuf
);
//地点名称暂时没有赋值,确认是不是用locationName
byteBuf
.
writeBytes
(
new
byte
[
32
]);
//录像路径不知道怎么赋值
byteBuf
.
writeBytes
(
new
byte
[
128
]);
//第一张全景图路径不知道怎么赋值
byteBuf
.
writeBytes
(
new
byte
[
128
]);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
//车型细分暂时未赋值
byteBuf
.
writeInt
(
0
);
//车牌结构暂时未赋值
byteBuf
.
writeInt
(
0
);
//违章类型需要转换
convertIllegalType
(
illegalType
,
byteBuf
);
byteBuf
.
writeInt
(
0
);
//第二张图片时间不知道
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
0
);
byteBuf
.
writeInt
(
red_start_second
);
byteBuf
.
writeInt
(
red_start_time
);
byteBuf
.
writeInt
(
red_end_second
);
byteBuf
.
writeInt
(
red_end_time
);
byteBuf
.
writeInt
(
limit_speed
);
byteBuf
.
writeInt
(
spark_speed
);
//保留字段,char[4]
byteBuf
.
writeBytes
(
new
byte
[
64
]);
}
public
void
convertPlate
(
String
plate
,
ByteBuf
byteBuf
){
}
public
void
convertPlateColor
(
String
plateColor
,
ByteBuf
byteBuf
){
}
public
void
convertCarType
(
String
carType
,
ByteBuf
byteBuf
){
}
public
void
convertCarColor
(
String
carColor
,
ByteBuf
byteBuf
){
}
public
void
convertDirection
(
String
direction
,
ByteBuf
byteBuf
){
}
public
void
convertLogo
(
String
logo
,
ByteBuf
byteBuf
){
private
void
location
(
TrafficModel
model
,
JSONObject
location
)
{
if
(
location
!=
null
)
{
JSONObject
driveDirection
=
location
.
getJSONObject
(
"drive_direction"
);
if
(
driveDirection
!=
null
)
{
Integer
directionCode
=
driveDirection
.
getInteger
(
"code"
);
// todo 方向待转换
model
.
setDirection
(
directionCode
);
}
String
locationName
=
location
.
getString
(
"name"
);
byte
[]
locationNameBytes
=
locationName
.
getBytes
(
StandardCharsets
.
UTF_8
);
System
.
arraycopy
(
locationNameBytes
,
0
,
model
.
getRouteLocation
(),
0
,
Math
.
min
(
locationNameBytes
.
length
,
32
));
// todo 录像路径
byte
[]
videoPathBytes
=
"testVideoPath"
.
getBytes
(
StandardCharsets
.
UTF_8
);
System
.
arraycopy
(
videoPathBytes
,
0
,
model
.
getVideoPath
(),
0
,
videoPathBytes
.
length
);
}
}
public
void
convertIllegalType
(
String
illegal
,
ByteBuf
byteBuf
){
private
void
illegal
(
TrafficModel
model
,
JSONObject
illegal
)
throws
ParseException
{
if
(
illegal
!=
null
&&
illegal
.
getInteger
(
"state"
)
==
1
)
{
String
illegalCode
=
illegal
.
getString
(
"code"
);
// todo 需要交通违法类型转换 illegalCode --> illegalType
model
.
setIllegalType
(
0
);
JSONObject
detail
=
illegal
.
getJSONObject
(
"detail"
);
if
(
detail
!=
null
)
{
Float
limitSpeed
=
detail
.
getFloat
(
"limit_speed"
);
Float
sparkSpeed
=
detail
.
getFloat
(
"spark_speed"
);
String
redStartStr
=
detail
.
getString
(
"start_dt"
);
long
redStart
=
0
;
long
redEnd
=
0
;
if
(
redStartStr
!=
null
&&
!
""
.
equals
(
redStartStr
.
trim
()))
{
redStart
=
DateUtil
.
parse
(
DateUtil
.
FORMAT_FULL
,
redStartStr
).
getTime
();
}
String
redEndStr
=
detail
.
getString
(
"end_dt"
);
if
(
redEndStr
!=
null
&&
!
""
.
equals
(
redEndStr
.
trim
()))
{
redEnd
=
DateUtil
.
parse
(
DateUtil
.
FORMAT_FULL
,
redEndStr
).
getTime
();
}
model
.
setRedLightBeginTime
((
int
)
(
redStart
/
1000
));
model
.
setRedLightBeginMillisecondTime
((
int
)
(
redStart
%
1000
));
model
.
setRedLightEndTime
((
int
)
(
redEnd
/
1000
));
model
.
setRedLightEndMillisecondTime
((
int
)
(
redEnd
%
1000
));
model
.
setSpeedLimit
(
Optional
.
ofNullable
(
limitSpeed
).
orElse
(
0
F
).
intValue
());
model
.
setOverSpeedThreshold
(
Optional
.
ofNullable
(
sparkSpeed
).
orElse
(
0
F
).
intValue
());
}
}
}
}
src/main/java/com/viontech/utils/DateUtil.java
0 → 100644
View file @
2b7277a
package
com
.
viontech
.
utils
;
import
org.apache.commons.lang3.time.DateUtils
;
import
org.springframework.context.i18n.LocaleContextHolder
;
import
java.text.DateFormatSymbols
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
public
class
DateUtil
extends
DateUtils
{
public
static
final
String
UTC_FULL
=
"yyyy-MM-dd'T'HH:mm:ss.SSS"
;
/**
* 英文简写(默认)如:2010
*/
public
static
String
FORMAT_YYYY
=
"yyyy"
;
/**
* 英文简写(默认)如:2010年
*/
public
static
String
FORMAT_YYYY_CN
=
"yyyy年"
;
/**
* 英文简写(默认)如:2010-12-31 24
*/
public
static
String
FORMAT_YYYY_MM_DD_HH
=
"yyyy-MM-dd HH"
;
/**
* 英文简写(默认)如:12-31 24
*/
public
static
String
FORMAT_MM_DD_HH
=
"MM-dd HH"
;
/**
* 英文简写(默认)如:31 24
*/
public
static
String
FORMAT_DD_HH
=
"dd HH"
;
/**
* 英文简写(默认)如:24
*/
public
static
String
FORMAT_HH
=
"HH"
;
/**
* 星期的简写 周一 周二 周三 周四等
*/
public
static
String
FORMAT_DAY_OF_WEEK_SORT_CN
=
"E"
;
public
static
String
FORMAT_WEEK_OF_YEAR
=
"w"
;
/**
* 英文简写(默认)如:2010-12
*/
public
static
String
FORMAT_YYYY_MM
=
"yyyy-MM"
;
/**
* 英文简写(默认)如:2010-12
*/
public
static
String
FORMAT_YYYY_MM_CN
=
"yyyy年MM月"
;
/**
* 英文简写(默认)如:12-31
*/
public
static
String
FORMAT_MM_DD
=
"MM-dd"
;
/**
* 英文简写带星期(默认)如:12-31(周三)
*/
public
static
String
FORMAT_MM_DD_E
=
"MM-dd(E)"
;
/**
* 英文简写(默认)如:31
*/
public
static
String
FORMAT_DD
=
"dd"
;
/**
* 英文简写(默认)如:2010-12-01
*/
public
static
String
FORMAT_SHORT
=
"yyyy-MM-dd"
;
/**
* 英文全称 如:2010-12-01 23:15
*/
public
static
String
FORMAT_YYYY_MM_DD_HH_MM
=
"yyyy-MM-dd HH:mm:ss"
;
/**
* 英文全称 如:2010-12-01 23:15:06
*/
public
static
String
FORMAT_LONG
=
"yyyy-MM-dd HH:mm:ss"
;
/**
* 精确到毫秒的完整时间 如:2010-12-01 23:15:06.999
*/
public
static
String
FORMAT_FULL
=
"yyyy-MM-dd HH:mm:ss.S"
;
/**
* 中文简写 如:2010年12月01日
*/
public
static
String
FORMAT_SHORT_CN
=
"yyyy年MM月dd日"
;
/**
* 中文简写 如:2010年12月01日(周日)
*/
public
static
String
FORMAT_SHORT_CN_WEEK
=
"yyyy年MM月dd日(E)"
;
public
static
String
FORMAT_SHORT_WEEK
=
"yyyy-MM-dd(E)"
;
/**
* 中文全称 如:2010年12月01日 23时15分06秒
*/
public
static
String
FORMAT_LONG_CN
=
"yyyy年MM月dd日 HH时mm分ss秒"
;
/**
* 精确到毫秒的完整中文时间 如:2010年12月01日 23时15分06秒999毫秒
*/
public
static
String
FORMAT_FULL_CN
=
"yyyy年MM月dd日 HH时mm分ss秒SSS毫秒"
;
public
static
String
FORMAT_HHMMSS
=
"HH:mm:ss"
;
public
static
String
FORMAT_HHMMSSS
=
"HH:mm:ss.S"
;
public
static
String
FORMAT_HHMM
=
"HH:mm"
;
//设置一周的周几为一周第一天
public
static
int
FIRST_DAY_OF_WEEK
=
Calendar
.
MONDAY
;
// 中国周一是一周的第一天
private
static
DateFormatSymbols
dateformatSymbols
=
new
DateFormatSymbols
(
Locale
.
CHINA
);
//设置一周的周几为休息日
private
static
int
[]
weekends
=
null
;
static
{
dateformatSymbols
.
setShortWeekdays
(
new
String
[]{
""
,
"周日"
,
"周一"
,
"周二"
,
"周三"
,
"周四"
,
"周五"
,
"周六"
});
}
public
static
void
setFirstDayOfWeek
(
int
firstDayOfWeek
)
{
FIRST_DAY_OF_WEEK
=
firstDayOfWeek
;
}
public
static
void
setWeekend
(
int
[]
weekends
)
{
DateUtil
.
weekends
=
weekends
;
}
/**
* 获取上周同期
*
* @param date
*
* @return 上周同天
*/
public
static
Date
getLastWeek
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
calendar
.
add
(
Calendar
.
WEEK_OF_MONTH
,
-
1
);
return
calendar
.
getTime
();
}
/**
* 获取上月同期
*
* @param date 当日
*
* @return
*/
public
static
Date
getLastMonth
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
calendar
.
add
(
Calendar
.
MONTH
,
-
1
);
return
calendar
.
getTime
();
}
/**
* 获取下月同期
*
* @param date 当日
*
* @return
*/
public
static
Date
getNextMonth
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
calendar
.
add
(
Calendar
.
MONTH
,
1
);
return
calendar
.
getTime
();
}
/**
* 获取去年同期
*
* @param date 当日
*
* @return
*/
public
static
Date
getLastYear
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
calendar
.
add
(
Calendar
.
YEAR
,
-
1
);
return
calendar
.
getTime
();
}
/**
* 获取昨日日期
*
* @param date 当日
*
* @return
*/
public
static
Date
getYesterday
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
calendar
.
add
(
Calendar
.
DAY_OF_YEAR
,
-
1
);
return
calendar
.
getTime
();
}
/**
* 获取明天日期
*
* @param date 当日
*
* @return
*/
public
static
Date
getTorromow
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
calendar
.
add
(
Calendar
.
DAY_OF_YEAR
,
1
);
return
calendar
.
getTime
();
}
/**
* 获取下一小时
*
* @param date 日期
*
* @return
*/
public
static
Date
getNextHour
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
calendar
.
add
(
Calendar
.
HOUR_OF_DAY
,
1
);
return
calendar
.
getTime
();
}
/**
* 获取前一小时
*
* @param date 日期
*
* @return
*/
public
static
Date
getPreviousHour
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
calendar
.
add
(
Calendar
.
HOUR_OF_DAY
,
-
1
);
return
calendar
.
getTime
();
}
/**
* 判断 yesterDay 是否是 today 的昨天
*
* @param today 今天日期
* @param yesterDay 昨天日期
*
* @return
*/
public
static
boolean
isYesterday
(
Date
today
,
Date
yesterDay
)
{
return
isSameDay
(
getYesterday
(
today
),
yesterDay
);
}
/**
* 判断 lastWeekDay 是否是 today 的上周同期
*
* @param today 今天日期
* @param lastWeekDay 上周同期日期
*
* @return
*/
public
static
boolean
isLastWeekDay
(
Date
today
,
Date
lastWeekDay
)
{
return
isSameDay
(
getLastWeek
(
today
),
lastWeekDay
);
}
/**
* 判断 lastMonthDay 是否是 today 的上月同期
*
* @param today 今天日期
* @param lastMonthDay 上月同期
*
* @return
*/
public
static
boolean
isLastMonthDay
(
Date
today
,
Date
lastMonthDay
)
{
return
isSameDay
(
getLastMonth
(
today
),
lastMonthDay
);
}
/**
* 判断 lastYearDay 是否是 today 的去年同期
*
* @param today 今天日期
* @param lastYearDay 去年同期日期
*
* @return
*/
public
static
boolean
isLastYearDay
(
Date
today
,
Date
lastYearDay
)
{
return
isSameDay
(
getLastYear
(
today
),
lastYearDay
);
}
/**
* 判断两个日期是否是同一天
*
* @param date1 日期1
* @param date2 日期2
*
* @return
*/
public
static
boolean
isSameHour
(
Date
date1
,
Date
date2
)
{
if
(!
isSameDay
(
date1
,
date2
))
{
return
false
;
}
if
(
date1
.
getHours
()
!=
date2
.
getHours
())
{
return
false
;
}
return
true
;
}
/**
* 判断两个日期是否是同一天
*
* @param date1 日期1
* @param date2 日期2
*
* @return
*/
public
static
boolean
isSameDay
(
Date
date1
,
Date
date2
)
{
if
(!
isSameMonth
(
date1
,
date2
))
{
return
false
;
}
if
(
date1
.
getDate
()
!=
date2
.
getDate
())
{
return
false
;
}
return
true
;
}
/**
* 判断两个日期是否是同一周
*
* @return
*/
public
static
boolean
isSameWeek
(
Date
week
,
Date
otherWeek
)
{
return
isSameDay
(
getMondayOfWeek
(
week
),
getMondayOfWeek
(
otherWeek
));
}
/**
* 判断两个日期是否是同一月
*
* @param date1 日期1
* @param date2 日期2
*
* @return
*/
public
static
boolean
isSameMonth
(
Date
date1
,
Date
date2
)
{
if
(
date1
.
getYear
()
!=
date2
.
getYear
())
{
return
false
;
}
if
(
date1
.
getMonth
()
!=
date2
.
getMonth
())
{
return
false
;
}
return
true
;
}
/***
* 判断日期2是否是日期1的上周
* @param date1
* @param date2
* @return
*/
public
static
boolean
isLastWeek
(
Date
date1
,
Date
date2
)
{
return
isSameWeek
(
getLastWeek
(
date1
),
date2
);
}
/***
* 判断日期2是否是日期1的去年同周
* @param date1
* @param date2
* @return
*/
public
static
boolean
isLastYearWeek
(
Date
today
,
Date
otherDay
)
{
//return isSameWeek(getLastYear(date1), date2);
if
(!
isSameYear
(
getLastYear
(
today
),
otherDay
))
{
return
false
;
}
return
getWeekOfYear
(
today
)
==
getWeekOfYear
(
otherDay
);
}
/***
/***
* 判断日期2是否是日期1的上月
* @param date1
* @param date2
* @return
*/
public
static
boolean
isLastMonth
(
Date
date1
,
Date
date2
)
{
return
isSameMonth
(
getLastMonth
(
date1
),
date2
);
}
/***
* 判断日期2是否是日期1的去年同月
* @param date1
* @param date2
* @return
*/
public
static
boolean
isLastYearMonth
(
Date
date1
,
Date
date2
)
{
return
isSameMonth
(
getLastYear
(
date1
),
date2
);
}
/**
* 判断两个日期是否是同一季度
*
* @param date1 日期1
* @param date2 日期2
*
* @return
*/
public
static
boolean
isSameSeason
(
Date
date1
,
Date
date2
)
{
return
getSeason
(
date1
)
==
getSeason
(
date2
);
}
/**
* 判断 日期2 是否是 日期1的 上一季度
*
* @param date1 日期1
* @param date2 日期2
*
* @return
*/
public
static
boolean
isLastSeason
(
Date
date1
,
Date
date2
)
{
return
getSeason
(
date1
)
==
getSeason
(
date2
)
+
1
;
}
/**
* 判断两个日期是否是同一年
*
* @param date1 日期1
* @param date2 日期2
*
* @return
*/
@SuppressWarnings
(
"deprecation"
)
public
static
boolean
isSameYear
(
Date
date1
,
Date
date2
)
{
if
(
date1
.
getYear
()
!=
date2
.
getYear
())
{
return
false
;
}
return
true
;
}
/**
* 将HH:mm解析成整点对象
*
* @param HMStr
* @param before true 往前取整 false 往后取整
*
* @return
* @throws ParseException 解析异常
*/
public
static
Date
parseHMDate2NowTime
(
String
HMStr
,
boolean
before
)
throws
ParseException
{
Calendar
calendar
=
Calendar
.
getInstance
();
Date
HMTime
=
parse
(
FORMAT_HHMM
,
HMStr
);
calendar
.
setTime
(
HMTime
);
int
hour
=
calendar
.
get
(
Calendar
.
HOUR_OF_DAY
);
int
minute
=
calendar
.
get
(
Calendar
.
MINUTE
);
int
second
=
calendar
.
get
(
Calendar
.
SECOND
);
int
milliSecond
=
calendar
.
get
(
Calendar
.
MILLISECOND
);
if
(
before
)
{
minute
=
0
;
second
=
0
;
milliSecond
=
0
;
}
else
{
minute
=
59
;
second
=
59
;
milliSecond
=
999
;
}
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
hour
);
calendar
.
set
(
Calendar
.
MINUTE
,
minute
);
calendar
.
set
(
Calendar
.
SECOND
,
second
);
calendar
.
set
(
Calendar
.
MILLISECOND
,
milliSecond
);
return
calendar
.
getTime
();
}
/**
* 将日期设置为整点
*
* @param date 需要设置的日期
* @param HMTime 小时分钟信息
*
* @return
*/
public
static
Date
setDayHMS
(
Date
date
,
Date
HMTime
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
HMTime
.
getHours
());
calendar
.
set
(
Calendar
.
MINUTE
,
HMTime
.
getMinutes
());
calendar
.
set
(
Calendar
.
SECOND
,
HMTime
.
getSeconds
());
calendar
.
set
(
Calendar
.
MILLISECOND
,
0
);
return
calendar
.
getTime
();
}
/**
* 将日期设置为对应的时分信息
*
* @param date 需要设置的日期
*
* @return
*/
public
static
Date
setDayHM
(
Date
date
,
String
HMTimeStr
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
//HH:mm:ss.S
Date
HMTime
=
null
;
try
{
HMTime
=
parse
(
FORMAT_HHMM
,
HMTimeStr
);
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
}
Calendar
hmCalendar
=
Calendar
.
getInstance
();
hmCalendar
.
setTime
(
HMTime
);
//HH:mm:ss.S
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
hmCalendar
.
get
(
Calendar
.
HOUR_OF_DAY
));
calendar
.
set
(
Calendar
.
MINUTE
,
hmCalendar
.
get
(
Calendar
.
MINUTE
));
calendar
.
set
(
Calendar
.
SECOND
,
0
);
return
calendar
.
getTime
();
}
/**
* 将日期设置为对应的时分秒
*
* @param date 需要设置的日期
*
* @return
*/
public
static
Date
setDayHMS
(
Date
date
,
String
HMSTimeStr
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
//HH:mm:ss.S
Date
HMSTime
=
null
;
try
{
HMSTime
=
parse
(
FORMAT_HHMMSS
,
HMSTimeStr
);
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
}
Calendar
hmsCalendar
=
Calendar
.
getInstance
();
hmsCalendar
.
setTime
(
HMSTime
);
//HH:mm:ss.S
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
hmsCalendar
.
get
(
Calendar
.
HOUR_OF_DAY
));
calendar
.
set
(
Calendar
.
MINUTE
,
hmsCalendar
.
get
(
Calendar
.
MINUTE
));
calendar
.
set
(
Calendar
.
SECOND
,
hmsCalendar
.
get
(
Calendar
.
SECOND
));
return
calendar
.
getTime
();
}
/**
* 日期只取小时数据
*
* @param date 需要设置的日期
*
* @return
*/
public
static
Date
setDayH
(
Date
date
,
String
HTimeStr
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
//HH:mm:ss.S
Date
HTime
=
null
;
try
{
HTime
=
parse
(
FORMAT_HH
,
HTimeStr
);
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
}
Calendar
hCalendar
=
Calendar
.
getInstance
();
hCalendar
.
setTime
(
HTime
);
//HH:mm:ss.S
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
hCalendar
.
get
(
Calendar
.
HOUR_OF_DAY
));
calendar
.
set
(
Calendar
.
MINUTE
,
0
);
calendar
.
set
(
Calendar
.
SECOND
,
0
);
return
calendar
.
getTime
();
}
/**
* 获取时间戳
*/
public
static
String
getTimeString
()
{
Calendar
calendar
=
Calendar
.
getInstance
();
return
format
(
FORMAT_FULL
,
calendar
.
getTime
());
}
/**
* 获取日期年份
*
* @param date 日期
*
* @return
*/
public
static
int
getYear
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
return
calendar
.
get
(
Calendar
.
YEAR
);
}
/**
* 功能描述:返回月
*
* @param date Date 日期
*
* @return 返回月份
*/
public
static
int
getMonth
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
return
calendar
.
get
(
Calendar
.
MONTH
)
+
1
;
}
// /**
// * 功能描述:返回月
// *
// * @param date
// * Date 日期
// * @return 返回月份
// */
// public static String getMonthStr(Date date) {
// Calendar calendar = Calendar.getInstance();
// calendar.setTime(date);
// int monthNum = calendar.get(Calendar.MONTH) + 1;
// return MessageFormat.format(MONTHOFYEAR_STR_FORMAT, monthNum);
// }
/**
* 功能描述:返回日
*
* @param date Date 日期
*
* @return 返回日份
*/
public
static
int
getDay
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
return
calendar
.
get
(
Calendar
.
DAY_OF_MONTH
);
}
/**
* 功能描述:返回小时
*
* @param date 日期
*
* @return 返回小时
*/
public
static
int
getHour
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
return
calendar
.
get
(
Calendar
.
HOUR_OF_DAY
);
}
/**
* 功能描述:返回分
*
* @param date 日期
*
* @return 返回分钟
*/
public
static
int
getMinute
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
return
calendar
.
get
(
Calendar
.
MINUTE
);
}
/**
* 功能描述:返回分
*
* @param date 日期
*
* @return 返回分钟
*/
public
static
int
getMinuteOfDay
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
return
calendar
.
get
(
Calendar
.
HOUR_OF_DAY
)
*
60
+
calendar
.
get
(
Calendar
.
MINUTE
);
}
/**
* 返回秒钟
*
* @param date Date 日期
*
* @return 返回秒钟
*/
public
static
int
getSecond
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
return
calendar
.
get
(
Calendar
.
SECOND
);
}
/**
* 功能描述:返回毫秒
*
* @param date 日期
*
* @return 返回毫
*/
public
static
int
getMillis
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
return
calendar
.
get
(
calendar
.
MILLISECOND
);
}
/**
* 取得当天日期是周几
*
* @param date
*
* @return
*/
public
static
int
getDayOfWeek
(
Date
date
)
{
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
c
.
setFirstDayOfWeek
(
FIRST_DAY_OF_WEEK
);
int
day_of_week
=
c
.
get
(
Calendar
.
DAY_OF_WEEK
);
day_of_week
-=
1
;
if
(
day_of_week
==
0
)
{
day_of_week
=
7
;
}
return
day_of_week
;
}
/**
* 获取一个月 周一 到 周日 的个数 用count存储
*
* @param date
*
* @return 下标0 代表周一的天数 下表1代表周二的天数 依次类推
*/
public
static
int
[]
getCountDayOfWeekInMonth
(
Date
date
)
{
int
[]
count
=
new
int
[
7
];
Date
firstDate
=
getFirstDateOfMonth
(
date
);
int
dayOfWeek
=
getDayOfWeek
(
firstDate
);
int
dayOfMonth
=
getPassDayOfMonth
(
date
);
for
(
int
i
=
0
;
i
<
dayOfMonth
;
i
++)
{
count
[
dayOfWeek
-
1
]++;
dayOfWeek
++;
if
(
dayOfWeek
>
7
)
{
dayOfWeek
=
1
;
}
}
return
count
;
}
/**
* 取得一月的第几周
*
* @param date
*
* @return
*/
public
static
int
getWeekOfMonth
(
Date
date
)
{
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
int
week_of_year
=
c
.
get
(
Calendar
.
WEEK_OF_MONTH
);
return
week_of_year
;
}
/**
* 取得一年的第几周 (中式计算方法,以周一为一周开始)
*
* @param date
*
* @return
*/
public
static
int
getWeekOfYear
(
Date
date
)
{
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
c
.
setFirstDayOfWeek
(
FIRST_DAY_OF_WEEK
);
int
week_of_year
=
c
.
get
(
Calendar
.
WEEK_OF_YEAR
);
return
week_of_year
;
}
// /**
// * 取得一年的第几周,格式为 “第1周” (中式计算方法,以周一为一周开始)
// *
// * @param date
// * @return
// */
// public static String getWeekOfYearStr(Date date) {
// Calendar c = Calendar.getInstance();
// c.setTime(date);
// c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);
// int week_of_year = c.get(Calendar.WEEK_OF_YEAR);
// return MessageFormat.format(WEEKOFYEAR_STR_FORMAT, week_of_year);
// }
/**
* 根据日期取得对应周周一日期
*
* @param date
*
* @return
*/
public
static
Date
getMondayOfWeek
(
Date
date
)
{
Calendar
monday
=
Calendar
.
getInstance
();
monday
.
setTime
(
date
);
monday
.
setFirstDayOfWeek
(
FIRST_DAY_OF_WEEK
);
monday
.
set
(
Calendar
.
DAY_OF_WEEK
,
Calendar
.
MONDAY
);
return
monday
.
getTime
();
}
/**
* 根据日期取得对应周周日日期
*
* @param date
*
* @return
*/
public
static
Date
getSundayOfWeek
(
Date
date
)
{
Calendar
sunday
=
Calendar
.
getInstance
();
sunday
.
setTime
(
date
);
sunday
.
setFirstDayOfWeek
(
FIRST_DAY_OF_WEEK
);
sunday
.
set
(
Calendar
.
DAY_OF_WEEK
,
Calendar
.
SUNDAY
);
return
sunday
.
getTime
();
}
public
static
boolean
isWeekday
(
Date
date
)
{
int
dayOfWeek
=
getDayOfWeek
(
date
);
if
(
weekends
==
null
)
{
if
(
dayOfWeek
>
5
)
{
return
false
;
}
return
true
;
}
else
{
for
(
int
weekend
:
weekends
)
{
if
(
weekend
==
dayOfWeek
)
{
return
false
;
}
}
return
true
;
}
}
// public static boolean isWeekday(Date date){
// if (getDayOfWeek(date) > 5) {
// return false;
// }
// return true;
// }
/**
* 根据日期取得对应月的第一个相同周几
*
* @param date
*
* @return
*/
public
static
Date
getFirstSametWeekOfMonth
(
Date
date
)
{
Calendar
c
=
Calendar
.
getInstance
();
date
=
setDayMinTime
(
date
);
c
.
setTime
(
date
);
int
weekIndex
=
c
.
get
(
Calendar
.
DAY_OF_WEEK
);
c
.
set
(
Calendar
.
DAY_OF_MONTH
,
1
);
while
(
true
)
{
if
(
weekIndex
==
c
.
get
(
Calendar
.
DAY_OF_WEEK
))
break
;
c
.
add
(
Calendar
.
DAY_OF_MONTH
,
1
);
}
return
c
.
getTime
();
}
/**
* 取得月的剩余天数
*
* @param date
*
* @return
*/
public
static
int
getRemainDayOfMonth
(
Date
date
)
{
int
dayOfMonth
=
getDayOfMonth
(
date
);
int
day
=
getPassDayOfMonth
(
date
);
return
dayOfMonth
-
day
;
}
/**
* 取得月已经过的天数
*
* @param date
*
* @return
*/
public
static
int
getPassDayOfMonth
(
Date
date
)
{
Date
monthStart
=
setDayMinTime
(
getFirstDateOfMonth
(
new
Date
()));
if
(
monthStart
.
after
(
date
))
return
getDayOfMonth
(
date
);
Date
monthEnd
=
setDayMaxTime
(
getLastDateOfMonth
(
new
Date
()));
if
(
monthEnd
.
before
(
date
))
return
0
;
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
new
Date
());
return
c
.
get
(
Calendar
.
DAY_OF_MONTH
);
}
/**
* 取得月天数
*
* @param date
*
* @return
*/
public
static
int
getDayOfMonth
(
Date
date
)
{
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
return
c
.
getActualMaximum
(
Calendar
.
DAY_OF_MONTH
);
}
/**
* 取得月第一天
*
* @param date
*
* @return
*/
public
static
Date
getFirstDateOfMonth
(
Date
date
)
{
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
c
.
set
(
Calendar
.
DAY_OF_MONTH
,
c
.
getActualMinimum
(
Calendar
.
DAY_OF_MONTH
));
return
c
.
getTime
();
}
/**
* 取得月最后一天
*
* @param date
*
* @return
*/
public
static
Date
getLastDateOfMonth
(
Date
date
)
{
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
c
.
set
(
Calendar
.
DAY_OF_MONTH
,
c
.
getActualMaximum
(
Calendar
.
DAY_OF_MONTH
));
return
c
.
getTime
();
}
/**
* 取得季度第一天
*
* @param date
*
* @return
*/
public
static
Date
getFirstDateOfSeason
(
Date
date
)
{
return
getFirstDateOfMonth
(
getSeasonDate
(
date
)[
0
]);
}
/**
* 取得季度最后一天
*
* @param date
*
* @return
*/
public
static
Date
getLastDateOfSeason
(
Date
date
)
{
return
getLastDateOfMonth
(
getSeasonDate
(
date
)[
2
]);
}
/**
* 取得季度天数
*
* @param date
*
* @return
*/
public
static
int
getDayOfSeason
(
Date
date
)
{
int
day
=
0
;
Date
[]
seasonDates
=
getSeasonDate
(
date
);
for
(
Date
date2
:
seasonDates
)
{
day
+=
getDayOfMonth
(
date2
);
}
return
day
;
}
/**
* 取得季度剩余天数
*
* @param date
*
* @return
*/
public
static
int
getRemainDayOfSeason
(
Date
date
)
{
return
getDayOfSeason
(
date
)
-
getPassDayOfSeason
(
date
);
}
/**
* 取得季度已过天数
*
* @param date
*
* @return
*/
public
static
int
getPassDayOfSeason
(
Date
date
)
{
int
day
=
0
;
Date
[]
seasonDates
=
getSeasonDate
(
date
);
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
int
month
=
c
.
get
(
Calendar
.
MONTH
);
if
(
month
==
Calendar
.
JANUARY
||
month
==
Calendar
.
APRIL
||
month
==
Calendar
.
JULY
||
month
==
Calendar
.
OCTOBER
)
{
// 季度第一个月
day
=
getPassDayOfMonth
(
seasonDates
[
0
]);
}
else
if
(
month
==
Calendar
.
FEBRUARY
||
month
==
Calendar
.
MAY
||
month
==
Calendar
.
AUGUST
||
month
==
Calendar
.
NOVEMBER
)
{
// 季度第二个月
day
=
getDayOfMonth
(
seasonDates
[
0
])
+
getPassDayOfMonth
(
seasonDates
[
1
]);
}
else
if
(
month
==
Calendar
.
MARCH
||
month
==
Calendar
.
JUNE
||
month
==
Calendar
.
SEPTEMBER
||
month
==
Calendar
.
DECEMBER
)
{
// 季度第三个月
day
=
getDayOfMonth
(
seasonDates
[
0
])
+
getDayOfMonth
(
seasonDates
[
1
])
+
getPassDayOfMonth
(
seasonDates
[
2
]);
}
return
day
;
}
/**
* 取得季度月
*
* @param date
*
* @return
*/
public
static
Date
[]
getSeasonDate
(
Date
date
)
{
Date
[]
season
=
new
Date
[
3
];
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
int
nSeason
=
getSeason
(
date
);
if
(
nSeason
==
1
)
{
// 第一季度
c
.
set
(
Calendar
.
MONTH
,
Calendar
.
JANUARY
);
season
[
0
]
=
c
.
getTime
();
c
.
set
(
Calendar
.
MONTH
,
Calendar
.
FEBRUARY
);
season
[
1
]
=
c
.
getTime
();
c
.
set
(
Calendar
.
MONTH
,
Calendar
.
MARCH
);
season
[
2
]
=
c
.
getTime
();
}
else
if
(
nSeason
==
2
)
{
// 第二季度
c
.
set
(
Calendar
.
MONTH
,
Calendar
.
APRIL
);
season
[
0
]
=
c
.
getTime
();
c
.
set
(
Calendar
.
MONTH
,
Calendar
.
MAY
);
season
[
1
]
=
c
.
getTime
();
c
.
set
(
Calendar
.
MONTH
,
Calendar
.
JUNE
);
season
[
2
]
=
c
.
getTime
();
}
else
if
(
nSeason
==
3
)
{
// 第三季度
c
.
set
(
Calendar
.
MONTH
,
Calendar
.
JULY
);
season
[
0
]
=
c
.
getTime
();
c
.
set
(
Calendar
.
MONTH
,
Calendar
.
AUGUST
);
season
[
1
]
=
c
.
getTime
();
c
.
set
(
Calendar
.
MONTH
,
Calendar
.
SEPTEMBER
);
season
[
2
]
=
c
.
getTime
();
}
else
if
(
nSeason
==
4
)
{
// 第四季度
c
.
set
(
Calendar
.
MONTH
,
Calendar
.
OCTOBER
);
season
[
0
]
=
c
.
getTime
();
c
.
set
(
Calendar
.
MONTH
,
Calendar
.
NOVEMBER
);
season
[
1
]
=
c
.
getTime
();
c
.
set
(
Calendar
.
MONTH
,
Calendar
.
DECEMBER
);
season
[
2
]
=
c
.
getTime
();
}
return
season
;
}
/**
* 1 第一季度 2 第二季度 3 第三季度 4 第四季度
*
* @param date
*
* @return
*/
public
static
int
getSeason
(
Date
date
)
{
int
season
=
0
;
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
int
month
=
c
.
get
(
Calendar
.
MONTH
);
switch
(
month
)
{
case
Calendar
.
JANUARY
:
case
Calendar
.
FEBRUARY
:
case
Calendar
.
MARCH
:
season
=
1
;
break
;
case
Calendar
.
APRIL
:
case
Calendar
.
MAY
:
case
Calendar
.
JUNE
:
season
=
2
;
break
;
case
Calendar
.
JULY
:
case
Calendar
.
AUGUST
:
case
Calendar
.
SEPTEMBER
:
season
=
3
;
break
;
case
Calendar
.
OCTOBER
:
case
Calendar
.
NOVEMBER
:
case
Calendar
.
DECEMBER
:
season
=
4
;
break
;
default
:
break
;
}
return
season
;
}
/**
* 取得年第一天
*
* @param date
*
* @return
*/
public
static
Date
getFirstDateOfYear
(
Date
date
)
{
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
c
.
set
(
Calendar
.
DAY_OF_YEAR
,
c
.
getActualMinimum
(
Calendar
.
DAY_OF_YEAR
));
return
setDayMinTime
(
c
.
getTime
());
}
/**
* 取得年最后一天
*
* @param date
*
* @return
*/
public
static
Date
getLastDateOfYear
(
Date
date
)
{
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
c
.
set
(
Calendar
.
DAY_OF_YEAR
,
c
.
getActualMaximum
(
Calendar
.
DAY_OF_YEAR
));
return
setDayMaxTime
(
c
.
getTime
());
}
/**
* String转Date
* 支持:2010-12-01 23:15:06.999
* 2010-12-01 23:15:06
* 2010-12-01 23:15
* 2010-12-01
*
* @param dateStr
*
* @return
*/
public
static
Date
dateFormat
(
String
dateStr
)
throws
ParseException
{
if
(
dateStr
.
length
()
==
23
)
{
if
(
dateStr
.
contains
(
"T"
))
{
return
parse
(
UTC_FULL
,
dateStr
);
}
else
{
return
parse
(
FORMAT_FULL
,
dateStr
);
}
}
if
(
dateStr
.
length
()
==
19
)
return
DateUtil
.
parse
(
FORMAT_LONG
,
dateStr
);
else
if
(
dateStr
.
length
()
==
16
)
return
DateUtil
.
parse
(
FORMAT_YYYY_MM_DD_HH_MM
,
dateStr
);
else
if
(
dateStr
.
length
()
==
10
)
return
DateUtil
.
parse
(
FORMAT_SHORT
,
dateStr
);
else
return
null
;
}
/**
* 设置一天的最大值
*
* @param date
*
* @return
*/
public
static
Date
setDayMaxTime
(
Date
date
)
{
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
c
.
set
(
Calendar
.
HOUR_OF_DAY
,
c
.
getActualMaximum
(
Calendar
.
HOUR_OF_DAY
));
c
.
set
(
Calendar
.
MINUTE
,
c
.
getActualMaximum
(
Calendar
.
MINUTE
));
c
.
set
(
Calendar
.
SECOND
,
c
.
getActualMaximum
(
Calendar
.
SECOND
));
c
.
set
(
Calendar
.
MILLISECOND
,
c
.
getActualMaximum
(
Calendar
.
MILLISECOND
));
return
c
.
getTime
();
}
/**
* 设置一天的最小值
*
* @param date
*
* @return
*/
public
static
Date
setDayMinTime
(
Date
date
)
{
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
c
.
set
(
Calendar
.
HOUR_OF_DAY
,
c
.
getActualMinimum
(
Calendar
.
HOUR_OF_DAY
));
c
.
set
(
Calendar
.
MINUTE
,
c
.
getActualMinimum
(
Calendar
.
MINUTE
));
c
.
set
(
Calendar
.
SECOND
,
c
.
getActualMinimum
(
Calendar
.
SECOND
));
c
.
set
(
Calendar
.
MILLISECOND
,
c
.
getActualMinimum
(
Calendar
.
MILLISECOND
));
return
c
.
getTime
();
}
/**
* 当前日期加n毫秒
*
* @param date
* @param num
*
* @return
*/
public
static
Date
dateAddMillisecond
(
Date
date
,
int
num
)
{
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
c
.
add
(
Calendar
.
MILLISECOND
,
num
);
return
c
.
getTime
();
}
/**
* 获取两个时间点之间的天(包括开始、结束日期)
*
* @param startDate 开始时间
* @param endDate 结束时间
*
* @return
*/
public
static
List
<
Date
>
getDaysBetweenDates
(
Date
startDate
,
Date
endDate
)
{
List
<
Date
>
dates
=
new
ArrayList
<>();
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
startDate
);
while
(
c
.
getTime
().
before
(
endDate
))
{
dates
.
add
(
c
.
getTime
());
c
.
add
(
Calendar
.
DAY_OF_MONTH
,
1
);
}
dates
.
add
(
endDate
);
return
dates
;
}
/**
* 获取某天所在周的所有日期
*
* @param date 日期
*
* @return
*/
public
static
List
<
Date
>
getDaysOfWeek
(
Date
date
)
{
Calendar
day
=
Calendar
.
getInstance
();
day
.
setTime
(
date
);
day
.
setFirstDayOfWeek
(
FIRST_DAY_OF_WEEK
);
day
.
set
(
Calendar
.
DAY_OF_WEEK
,
Calendar
.
MONDAY
);
Date
startDate
=
day
.
getTime
();
day
.
set
(
Calendar
.
DAY_OF_WEEK
,
Calendar
.
SUNDAY
);
Date
endDate
=
day
.
getTime
();
List
<
Date
>
dates
=
new
ArrayList
<>();
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
startDate
);
while
(
c
.
getTime
().
before
(
endDate
))
{
dates
.
add
(
c
.
getTime
());
c
.
add
(
Calendar
.
DAY_OF_MONTH
,
1
);
}
dates
.
add
(
endDate
);
return
dates
;
}
/**
* 将字符串解析成时间,如果无法解析返回null
*
* @param sdf
* @param dateStr
*
* @return
*/
public
static
Date
parseDate
(
SimpleDateFormat
sdf
,
String
dateStr
)
{
try
{
return
sdf
.
parse
(
dateStr
);
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
public
static
Date
maxDate
(
Date
date1
,
Date
date2
)
{
if
(
date1
.
before
(
date2
))
{
return
date2
;
}
else
{
return
date1
;
}
}
public
static
Date
minDate
(
Date
date1
,
Date
date2
)
{
if
(
date1
.
before
(
date2
))
{
return
date1
;
}
else
{
return
date2
;
}
}
public
static
Date
UTC2Local
(
Date
date
)
{
SimpleDateFormat
sdf1
=
new
SimpleDateFormat
(
"yyyyMMddHHmmss.SSS"
);
String
utcDate
=
sdf1
.
format
(
date
);
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyyMMddHHmmss.SSS"
);
TimeZone
utcZone
=
TimeZone
.
getTimeZone
(
"UTC"
);
sdf
.
setTimeZone
(
utcZone
);
Date
result
=
null
;
try
{
result
=
sdf
.
parse
(
utcDate
);
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
}
return
result
;
}
public
static
Date
local2UTC
(
Date
localDate
)
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyyMMddHHmmss.SSS"
);
TimeZone
utcZone
=
TimeZone
.
getTimeZone
(
"UTC"
);
sdf
.
setTimeZone
(
utcZone
);
String
utcTime
=
sdf
.
format
(
localDate
);
// System.out.println(utcTime);
Date
result
=
null
;
try
{
SimpleDateFormat
sdf1
=
new
SimpleDateFormat
(
"yyyyMMddHHmmss.SSS"
);
result
=
sdf1
.
parse
(
utcTime
);
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
}
return
result
;
}
public
static
List
<
Date
>
getMonthDyas
(
Date
date
)
{
Date
monthStart
=
getFirstDateOfMonth
(
date
);
Date
monthEnd
=
getLastDateOfMonth
(
date
);
Date
currentDate
=
new
Date
();
monthEnd
=
monthEnd
.
before
(
currentDate
)
?
monthEnd
:
currentDate
;
List
<
Date
>
dates
=
getDaysBetweenDates
(
monthStart
,
monthEnd
);
return
dates
;
}
public
static
String
format
(
String
dateFormatStr
,
Date
date
)
{
Locale
locale
=
LocaleContextHolder
.
getLocale
();
return
format
(
dateFormatStr
,
date
,
locale
);
}
public
static
Date
parse
(
String
dateFormatStr
,
String
dateStr
)
throws
ParseException
{
Locale
locale
=
LocaleContextHolder
.
getLocale
();
return
parse
(
dateFormatStr
,
dateStr
,
locale
);
}
public
static
String
format
(
String
dateFormatStr
,
Date
date
,
Locale
locale
)
{
SimpleDateFormat
sdf
=
null
;
if
(
locale
!=
null
&&
!
"zh"
.
equals
(
locale
.
getLanguage
()))
{
//如果不是中文用其默认的格式 否则使用中文的自定义格式
sdf
=
new
SimpleDateFormat
(
dateFormatStr
,
locale
);
}
else
{
sdf
=
new
SimpleDateFormat
(
dateFormatStr
,
dateformatSymbols
);
}
return
sdf
.
format
(
date
);
}
public
static
Date
parse
(
String
dateFormatStr
,
String
dateStr
,
Locale
locale
)
throws
ParseException
{
SimpleDateFormat
sdf
=
null
;
if
(
locale
!=
null
&&
!
"zh"
.
equals
(
locale
.
getLanguage
()))
{
//如果不是中文用其默认的格式 否则使用中文的自定义格式
sdf
=
new
SimpleDateFormat
(
dateFormatStr
,
locale
);
}
else
{
sdf
=
new
SimpleDateFormat
(
dateFormatStr
,
dateformatSymbols
);
}
return
sdf
.
parse
(
dateStr
);
}
/**
* 指定格式化日期格式,比较两个字符串日期的大小
*
* @param dateFormatStr
* @param DATE1
* @param DATE2
*
* @return 1:date1>date2 -1 date1 < date2
*/
public
static
int
compareDate
(
String
dateFormatStr
,
String
DATE1
,
String
DATE2
)
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
dateFormatStr
);
try
{
Date
dt1
=
sdf
.
parse
(
DATE1
);
Date
dt2
=
sdf
.
parse
(
DATE2
);
if
(
dt1
.
getTime
()
>
dt2
.
getTime
())
{
return
1
;
}
else
if
(
dt1
.
getTime
()
<
dt2
.
getTime
())
{
return
-
1
;
}
else
{
return
0
;
}
}
catch
(
Exception
exception
)
{
exception
.
printStackTrace
();
}
return
0
;
}
/**
* 指定格式化日期格式,比较两个日期的大小
*
* @param dt1
* @param dt2
*
* @return 1:date1>date2 -1 date1 < date2
*/
public
static
int
compareDate
(
Date
dt1
,
Date
dt2
)
{
try
{
if
(
dt1
.
getTime
()
>
dt2
.
getTime
())
{
return
1
;
}
else
if
(
dt1
.
getTime
()
<
dt2
.
getTime
())
{
return
-
1
;
}
else
{
return
0
;
}
}
catch
(
Exception
exception
)
{
exception
.
printStackTrace
();
}
return
0
;
}
/**
* 获取指定日期 向后推指定时间 的时间段
*
* @param date 日期
* @param min 向后推的时间
* @param dateFormatStr 需要生成的日期格式
*
* @return
*/
public
static
String
DateBlockFormatStr
(
Date
date
,
int
min
,
String
dateFormatStr
)
{
Date
dt2
=
addMinutes
(
date
,
min
);
return
format
(
dateFormatStr
,
date
)
+
"-"
+
format
(
dateFormatStr
,
dt2
);
}
/**
* 作废,中心判断数据是否在营业时间
* 判断时间是否在时间范围内(只比较小时、分钟)
*
* @param date
* @param startDate
* @param endDate
*
* @return
*/
public
static
boolean
isInOpenTime
(
Date
date
,
Date
startDate
,
Date
endDate
)
{
boolean
flag
=
true
;
if
(
flag
)
{
return
isInBusiness
(
date
,
startDate
,
endDate
);
}
Calendar
date_c
=
Calendar
.
getInstance
();
date_c
.
setTime
(
date
);
int
year
=
date_c
.
get
(
Calendar
.
YEAR
);
int
month
=
date_c
.
get
(
Calendar
.
MONTH
);
int
day
=
date_c
.
get
(
Calendar
.
DAY_OF_MONTH
);
int
hour
=
date_c
.
get
(
Calendar
.
HOUR_OF_DAY
);
//开始、结束日期设置统一的天级以上的日期
Calendar
start_c
=
Calendar
.
getInstance
();
start_c
.
setTime
(
startDate
);
start_c
.
set
(
year
,
month
,
day
);
int
start_hour
=
start_c
.
get
(
Calendar
.
HOUR_OF_DAY
);
startDate
=
start_c
.
getTime
();
Calendar
end_c
=
Calendar
.
getInstance
();
end_c
.
setTime
(
endDate
);
end_c
.
set
(
year
,
month
,
day
);
endDate
=
end_c
.
getTime
();
//跨天数据特殊处理
if
(
endDate
.
before
(
startDate
)
||
endDate
.
equals
(
startDate
))
{
Calendar
end_n
=
Calendar
.
getInstance
();
end_n
.
setTime
(
endDate
);
end_n
.
add
(
Calendar
.
DAY_OF_MONTH
,
1
);
endDate
=
end_n
.
getTime
();
}
if
(
DateUtil
.
compareDate
(
date
,
endDate
)
<=
0
)
{
if
(
DateUtil
.
compareDate
(
date
,
startDate
)
>=
0
)
return
true
;
else
{
if
(
hour
==
start_hour
)
//开业时间同一个小时的数据,统计到小时数据中
return
true
;
else
return
false
;
}
}
return
false
;
}
public
static
boolean
isInBusiness
(
Date
counttime
,
Date
startTime
,
Date
endTime
)
{
Calendar
counttimeCalendar
=
Calendar
.
getInstance
();
counttimeCalendar
.
setTime
(
counttime
);
Calendar
startTimeCalendar
=
Calendar
.
getInstance
();
startTimeCalendar
.
setTime
(
startTime
);
int
counttimeHour
=
counttimeCalendar
.
get
(
Calendar
.
HOUR_OF_DAY
);
int
counttimeMinute
=
counttimeCalendar
.
get
(
Calendar
.
MINUTE
);
int
startTimeHour
=
startTimeCalendar
.
get
(
Calendar
.
HOUR_OF_DAY
);
int
startTimeMinute
=
startTimeCalendar
.
get
(
Calendar
.
MINUTE
);
Calendar
endTimeCalendar
=
Calendar
.
getInstance
();
endTimeCalendar
.
setTime
(
endTime
);
int
endTimeHour
=
endTimeCalendar
.
get
(
Calendar
.
HOUR_OF_DAY
);
int
endTimeMinute
=
endTimeCalendar
.
get
(
Calendar
.
MINUTE
);
if
(
startTimeHour
==
endTimeHour
&&
startTimeMinute
==
endTimeMinute
)
{
return
true
;
}
else
if
(
startTimeHour
<
endTimeHour
||
(
startTimeHour
==
endTimeHour
&&
startTimeMinute
<
endTimeMinute
))
{
//如果是正常的营业
if
(
counttimeHour
<
startTimeHour
)
{
//如果小时小于营业时间 不用比分钟了
return
false
;
}
else
if
(
counttimeHour
==
startTimeHour
)
{
if
(
counttimeMinute
<
startTimeMinute
)
{
return
false
;
}
}
if
(
counttimeHour
>
endTimeHour
)
{
//如果小时大于营业时间 也不用比分钟了
return
false
;
}
else
if
(
counttimeHour
==
endTimeHour
)
{
if
(
counttimeMinute
>
endTimeMinute
)
{
return
false
;
}
}
return
true
;
}
else
{
//如果是跨天的营业时间
if
(
counttimeHour
>
startTimeHour
)
{
//如果小时小于营业时间 不用比分钟了
return
true
;
}
else
if
(
counttimeHour
==
startTimeHour
)
{
if
(
counttimeMinute
>
startTimeMinute
)
{
return
true
;
}
}
if
(
counttimeHour
<
endTimeHour
)
{
return
true
;
}
else
if
(
counttimeHour
==
endTimeHour
)
{
if
(
counttimeMinute
<
endTimeMinute
)
{
return
true
;
}
}
return
false
;
}
}
public
static
String
getStringUTC
(
String
dateTime
)
{
try
{
Date
date
=
dateFormat
(
dateTime
);
Calendar
cal
=
Calendar
.
getInstance
(
Locale
.
CHINA
);
cal
.
setTime
(
date
);
int
zoneOffset
=
cal
.
get
(
Calendar
.
ZONE_OFFSET
);
int
dstOffset
=
cal
.
get
(
Calendar
.
DST_OFFSET
);
cal
.
add
(
Calendar
.
MILLISECOND
,
-(
zoneOffset
+
dstOffset
));
return
format
(
FORMAT_FULL
,
cal
.
getTime
());
}
catch
(
ParseException
e
)
{
System
.
out
.
println
(
"字符串格式不正确yyyy-MM-dd HH:mm:ss.S"
);
}
return
"error"
;
}
/**
* 获取服务器UTC时间
*
* @return
*/
public
static
Calendar
getCurrentUTC
()
{
Calendar
cal
=
Calendar
.
getInstance
(
Locale
.
CHINA
);
int
zoneOffset
=
cal
.
get
(
Calendar
.
ZONE_OFFSET
);
int
dstOffset
=
cal
.
get
(
Calendar
.
DST_OFFSET
);
cal
.
add
(
Calendar
.
MILLISECOND
,
-(
zoneOffset
+
dstOffset
));
return
cal
;
}
}
src/main/resources/fxlb.properties
View file @
2b7277a
1
=
1
2
=
2
3
=
3
4
=
4
4
=
4
\ No newline at end of file
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