Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
guns-vip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chenjunxiong
guns-vip
Commits
5874e4c9
Commit
5874e4c9
authored
Jul 23, 2017
by
tanliansheng
Committed by
stylefeng
Jul 23, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request !22 from ilaotan/dev
parents
f92d4358
ec940189
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
79 deletions
+63
-79
pom.xml
+6
-0
src/main/java/com/stylefeng/guns/core/util/DateUtil.java
+57
-79
No files found.
pom.xml
View file @
5874e4c9
...
...
@@ -34,6 +34,7 @@
<druid.version>
1.0.28
</druid.version>
<beetl.version>
2.7.15
</beetl.version>
<swagger.version>
2.2.2
</swagger.version>
<commons-lang3.version>
3.6
</commons-lang3.version>
</properties>
<dependencies>
...
...
@@ -174,6 +175,11 @@
<artifactId>
springfox-swagger-ui
</artifactId>
<version>
${swagger.version}
</version>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
<version>
${commons-lang3.version}
</version>
</dependency>
</dependencies>
...
...
src/main/java/com/stylefeng/guns/core/util/DateUtil.java
View file @
5874e4c9
...
...
@@ -22,108 +22,111 @@ import java.text.SimpleDateFormat;
import
java.util.Calendar
;
import
java.util.Date
;
public
class
DateUtil
{
private
final
static
SimpleDateFormat
sdfYear
=
new
SimpleDateFormat
(
"yyyy"
);
private
final
static
SimpleDateFormat
sdfDay
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.time.DateFormatUtils
;
import
org.apache.commons.lang3.time.DateUtils
;
private
final
static
SimpleDateFormat
sdfDays
=
new
SimpleDateFormat
(
"yyyyMMdd"
);
private
final
static
SimpleDateFormat
sdfTime
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
private
final
static
SimpleDateFormat
sdfmsTime
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss.SSS"
);
private
final
static
SimpleDateFormat
allTime
=
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
);
public
class
DateUtil
{
/**
* 获取YYYY格式
*
*
* @return
*/
public
static
String
getYear
()
{
return
sdfYear
.
format
(
new
Date
()
);
return
formatDate
(
new
Date
(),
"yyyy"
);
}
/**
* 获取YYYY格式
*
*
* @return
*/
public
static
String
getYear
(
Date
date
)
{
return
sdfYear
.
format
(
date
);
return
formatDate
(
date
,
"yyyy"
);
}
/**
* 获取YYYY-MM-DD格式
*
*
* @return
*/
public
static
String
getDay
()
{
return
sdfDay
.
format
(
new
Date
()
);
return
formatDate
(
new
Date
(),
"yyyy-MM-dd"
);
}
/**
* 获取YYYY-MM-DD格式
*
*
* @return
*/
public
static
String
getDay
(
Date
date
)
{
return
sdfDay
.
format
(
date
);
return
formatDate
(
date
,
"yyyy-MM-dd"
);
}
/**
* 获取YYYYMMDD格式
*
*
* @return
*/
public
static
String
getDays
()
{
return
sdfDays
.
format
(
new
Date
()
);
return
formatDate
(
new
Date
(),
"yyyyMMdd"
);
}
/**
* 获取YYYYMMDD格式
*
*
* @return
*/
public
static
String
getDays
(
Date
date
)
{
return
sdfDays
.
format
(
date
);
return
formatDate
(
date
,
"yyyyMMdd"
);
}
/**
* 获取YYYY-MM-DD HH:mm:ss格式
*
*
* @return
*/
public
static
String
getTime
()
{
return
sdfTime
.
format
(
new
Date
()
);
return
formatDate
(
new
Date
(),
"yyyy-MM-dd HH:mm:ss"
);
}
/**
* 获取YYYY-MM-DD HH:mm:ss.SSS格式
*
*
* @return
*/
public
static
String
getMsTime
()
{
return
sdfmsTime
.
format
(
new
Date
()
);
return
formatDate
(
new
Date
(),
"yyyy-MM-dd HH:mm:ss.SSS"
);
}
/**
* 获取YYYYMMDDHHmmss格式
*
*
* @return
*/
public
static
String
getAllTime
()
{
return
allTime
.
format
(
new
Date
()
);
return
formatDate
(
new
Date
(),
"yyyyMMddHHmmss"
);
}
/**
* 获取YYYY-MM-DD HH:mm:ss格式
*
*
* @return
*/
public
static
String
getTime
(
Date
date
)
{
return
sdfTime
.
format
(
date
);
return
formatDate
(
date
,
"yyyy-MM-dd HH:mm:ss"
);
}
public
static
String
formatDate
(
Date
date
,
String
pattern
)
{
String
formatDate
=
null
;
if
(
StringUtils
.
isNotBlank
(
pattern
))
{
formatDate
=
DateFormatUtils
.
format
(
date
,
pattern
);
}
else
{
formatDate
=
DateFormatUtils
.
format
(
date
,
"yyyy-MM-dd"
);
}
return
formatDate
;
}
/**
...
...
@@ -144,41 +147,30 @@ public class DateUtil {
/**
* 格式化日期
*
*
* @return
*/
public
static
Date
parseDate
(
String
date
)
{
try
{
return
sdfDay
.
parse
(
date
);
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
return
null
;
}
return
parse
(
date
,
"yyyy-MM-dd"
);
}
/**
* 格式化日期
*
*
* @return
*/
public
static
Date
parseTime
(
String
date
)
{
try
{
return
sdfTime
.
parse
(
date
);
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
return
null
;
}
return
parse
(
date
,
"yyyy-MM-dd HH:mm:ss"
);
}
/**
* 格式化日期
*
*
* @return
*/
public
static
Date
parse
(
String
date
,
String
pattern
)
{
DateFormat
fmt
=
new
SimpleDateFormat
(
pattern
);
try
{
return
fmt
.
parse
(
date
);
return
DateUtils
.
parseDate
(
date
,
pattern
);
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
return
null
;
...
...
@@ -187,53 +179,39 @@ public class DateUtil {
/**
* 格式化日期
*
*
* @return
*/
public
static
String
format
(
Date
date
,
String
pattern
)
{
DateFormat
fmt
=
new
SimpleDateFormat
(
pattern
);
return
fmt
.
format
(
date
);
return
DateFormatUtils
.
format
(
date
,
pattern
);
}
/**
* 把日期转换为Timestamp
*
*
* @param date
* @return
*/
public
static
Timestamp
format
(
Date
date
)
{
return
new
java
.
sql
.
Timestamp
(
date
.
getTime
());
return
new
Timestamp
(
date
.
getTime
());
}
/**
* 校验日期是否合法
*
*
* @return
*/
public
static
boolean
isValidDate
(
String
s
)
{
try
{
sdfTime
.
parse
(
s
);
return
true
;
}
catch
(
Exception
e
)
{
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
return
false
;
}
return
parse
(
s
,
"yyyy-MM-dd HH:mm:ss"
)
!=
null
;
}
/**
* 校验日期是否合法
*
*
* @return
*/
public
static
boolean
isValidDate
(
String
s
,
String
pattern
)
{
DateFormat
fmt
=
new
SimpleDateFormat
(
pattern
);
try
{
fmt
.
parse
(
s
);
return
true
;
}
catch
(
Exception
e
)
{
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
return
false
;
}
return
parse
(
s
,
pattern
)
!=
null
;
}
public
static
int
getDiffYear
(
String
startTime
,
String
endTime
)
{
...
...
@@ -250,7 +228,7 @@ public class DateUtil {
/**
* <li>功能描述:时间相减得到天数
*
*
* @param beginDateStr
* @param endDateStr
* @return long
...
...
@@ -258,10 +236,10 @@ public class DateUtil {
*/
public
static
long
getDaySub
(
String
beginDateStr
,
String
endDateStr
)
{
long
day
=
0
;
java
.
text
.
SimpleDateFormat
format
=
new
java
.
text
.
SimpleDateFormat
(
SimpleDateFormat
format
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
java
.
util
.
Date
beginDate
=
null
;
java
.
util
.
Date
endDate
=
null
;
Date
beginDate
=
null
;
Date
endDate
=
null
;
try
{
beginDate
=
format
.
parse
(
beginDateStr
);
...
...
@@ -277,7 +255,7 @@ public class DateUtil {
/**
* 得到n天之后的日期
*
*
* @param days
* @return
*/
...
...
@@ -296,7 +274,7 @@ public class DateUtil {
/**
* 得到n天之后是周几
*
*
* @param days
* @return
*/
...
...
@@ -325,7 +303,7 @@ public class DateUtil {
// return Func.toStr(value);
// }
// }
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
getTime
(
new
Date
()));
System
.
out
.
println
(
getAfterDayWeek
(
"3"
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment