Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
dd77cfc9
Commit
dd77cfc9
authored
Jun 11, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
压缩视频
parent
b7e5a348
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
6 deletions
+86
-6
DateHelper.java
...src/main/java/com/yanzuoguang/util/helper/DateHelper.java
+63
-6
TestDateHelper.java
yzg-util-base/src/test/java/helper/TestDateHelper.java
+23
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/DateHelper.java
View file @
dd77cfc9
...
...
@@ -18,6 +18,7 @@ import java.util.Date;
public
class
DateHelper
{
private
static
final
String
FORMAT_YEAR_STRING
=
"yyyy-01-01"
;
private
static
final
String
FORMAT_YEAR_END_STRING
=
"yyyy-12-31"
;
private
static
final
String
FORMAT_MONTH_STRING
=
"yyyy-MM-01"
;
private
static
final
String
FORMAT_DAY_STRING
=
"yyyy-MM-dd"
;
private
static
final
String
FORMAT_DAY_HOUR_STRING
=
"yyyy-MM-dd HH:00:00"
;
...
...
@@ -249,6 +250,14 @@ public class DateHelper {
return
begin
;
}
private
static
void
setDayEnd
(
Calendar
cal
)
{
cal
.
set
(
Calendar
.
HOUR_OF_DAY
,
23
);
cal
.
set
(
Calendar
.
MINUTE
,
59
);
cal
.
set
(
Calendar
.
SECOND
,
59
);
cal
.
set
(
Calendar
.
MILLISECOND
,
999
);
}
/**
* 获取某一天起始和结束时间
*
...
...
@@ -268,8 +277,7 @@ public class DateHelper {
*/
public
static
Date
getDayEnd
(
Date
date
)
{
Calendar
end
=
getDayCalendarBegin
(
date
);
end
.
add
(
Calendar
.
DAY_OF_MONTH
,
1
);
end
.
add
(
Calendar
.
SECOND
,
-
1
);
setDayEnd
(
end
);
return
end
.
getTime
();
}
...
...
@@ -281,7 +289,9 @@ public class DateHelper {
* @return 返回的日期
*/
public
static
Date
getMonthStart
(
Date
date
)
{
return
DateHelper
.
getDateTime
(
getDate
(
date
,
FORMAT_MONTH_STRING
));
Calendar
cal
=
getDayCalendarBegin
(
date
);
cal
.
set
(
Calendar
.
DAY_OF_MONTH
,
1
);
return
cal
.
getTime
();
}
/**
...
...
@@ -293,6 +303,37 @@ public class DateHelper {
public
static
Date
getMonthEnd
(
Date
date
)
{
Calendar
cal
=
getDayCalendarBegin
(
date
);
cal
.
set
(
Calendar
.
DAY_OF_MONTH
,
cal
.
getActualMaximum
(
Calendar
.
DAY_OF_MONTH
));
setDayEnd
(
cal
);
return
cal
.
getTime
();
}
/**
* 获取本季度开始时间
*
* @param date 某一天
* @return 返回的日期
*/
public
static
Date
getMonth3Start
(
Date
date
)
{
Calendar
cal
=
getDayCalendarBegin
(
date
);
int
month
=
cal
.
get
(
Calendar
.
MONTH
)
/
3
*
3
;
cal
.
set
(
Calendar
.
MONTH
,
month
);
cal
.
set
(
Calendar
.
DAY_OF_MONTH
,
1
);
return
cal
.
getTime
();
}
/**
* 获得本月最后一天24点时间
*
* @param date 某一天
* @return 返回的值
*/
public
static
Date
getMonth3End
(
Date
date
)
{
Calendar
cal
=
getDayCalendarBegin
(
date
);
int
month
=
cal
.
get
(
Calendar
.
MONTH
)
/
3
*
3
;
cal
.
set
(
Calendar
.
MONTH
,
month
+
2
);
cal
.
set
(
Calendar
.
DAY_OF_MONTH
,
cal
.
getActualMaximum
(
Calendar
.
DAY_OF_MONTH
));
setDayEnd
(
cal
);
return
cal
.
getTime
();
}
...
...
@@ -303,7 +344,23 @@ public class DateHelper {
* @return 返回值
*/
public
static
Date
getYearStart
(
Date
date
)
{
return
DateHelper
.
getDateTime
(
getDate
(
date
,
FORMAT_YEAR_STRING
));
Calendar
cal
=
getDayCalendarBegin
(
date
);
cal
.
set
(
Calendar
.
MONTH
,
0
);
cal
.
set
(
Calendar
.
DAY_OF_MONTH
,
1
);
return
cal
.
getTime
();
}
/**
* 获取本年结束时间
*
* @param date 某一天
* @return 返回值
*/
public
static
Date
getYearEnd
(
Date
date
)
{
Calendar
cal
=
getDayCalendarBegin
(
date
);
cal
.
set
(
Calendar
.
MONTH
,
11
);
cal
.
set
(
Calendar
.
DAY_OF_MONTH
,
31
);
return
cal
.
getTime
();
}
/**
...
...
@@ -601,7 +658,7 @@ public class DateHelper {
/**
* 增加分钟处理
*
* @param date 当前日期
* @param date
当前日期
* @param minute 需要增加的分钟
* @return 增加之后的值
*/
...
...
@@ -615,7 +672,7 @@ public class DateHelper {
/**
* 增加秒处理
*
* @param date 当前日期
* @param date
当前日期
* @param second 需要增加的分钟
* @return 增加之后的值
*/
...
...
yzg-util-base/src/test/java/helper/TestDateHelper.java
0 → 100755
View file @
dd77cfc9
package
helper
;
import
com.yanzuoguang.util.helper.DateHelper
;
import
org.junit.Test
;
import
java.text.ParseException
;
import
java.util.Date
;
public
class
TestDateHelper
{
@Test
public
void
testDateHelper
()
throws
ParseException
{
Date
date
=
new
Date
();
System
.
out
.
println
(
"本日:"
+
DateHelper
.
getDateTimeString
(
DateHelper
.
getDayStart
(
date
)));
System
.
out
.
println
(
"本日:"
+
DateHelper
.
getDateTimeString
(
DateHelper
.
getDayEnd
(
date
)));
System
.
out
.
println
(
"本年:"
+
DateHelper
.
getDateTimeString
(
DateHelper
.
getYearStart
(
date
)));
System
.
out
.
println
(
"本年:"
+
DateHelper
.
getDateTimeString
(
DateHelper
.
getYearEnd
(
date
)));
System
.
out
.
println
(
"本月:"
+
DateHelper
.
getDateTimeString
(
DateHelper
.
getMonthStart
(
date
)));
System
.
out
.
println
(
"本月:"
+
DateHelper
.
getDateTimeString
(
DateHelper
.
getMonthEnd
(
date
)));
System
.
out
.
println
(
"本季度:"
+
DateHelper
.
getDateTimeString
(
DateHelper
.
getMonth3Start
(
date
)));
System
.
out
.
println
(
"本季度:"
+
DateHelper
.
getDateTimeString
(
DateHelper
.
getMonth3End
(
date
)));
}
}
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