Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
amos-boot-zx-biz
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
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
Jobs
Commits
Open sidebar
项目统一框架
一体化_户用光伏项目代码
amos-boot-zx-biz
Commits
8ce016da
Commit
8ce016da
authored
May 22, 2024
by
wujiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加时间转换
parent
7bc290f0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
897 additions
and
860 deletions
+897
-860
DateUtils.java
...ava/com/yeejoin/amos/boot/biz/common/utils/DateUtils.java
+897
-860
No files found.
amos-boot-biz-common/src/main/java/com/yeejoin/amos/boot/biz/common/utils/DateUtils.java
View file @
8ce016da
...
@@ -15,607 +15,645 @@ import java.util.*;
...
@@ -15,607 +15,645 @@ import java.util.*;
**/
**/
public
class
DateUtils
{
public
class
DateUtils
{
public
static
final
String
DATE_TIME_PATTERN
=
"yyyy-MM-dd HH:mm:ss"
;
public
static
final
String
DATE_TIME_PATTERN
=
"yyyy-MM-dd HH:mm:ss"
;
public
static
final
String
MINUTE_PATTERN
=
"yyyy-MM-dd HH:mm"
;
public
static
final
String
MINUTE_PATTERN
=
"yyyy-MM-dd HH:mm"
;
public
static
final
String
HOUR_PATTERN
=
"yyyy-MM-dd HH:mm:ss"
;
public
static
final
String
HOUR_PATTERN
=
"yyyy-MM-dd HH"
;
public
static
final
String
DATE_PATTERN
=
"yyyy-MM-dd"
;
public
static
final
String
DATE_PATTERN
=
"yyyy-MM-dd"
;
public
static
final
String
MONTH_PATTERN
=
"yyyy-MM"
;
public
static
final
String
MONTH_PATTERN
=
"yyyy-MM"
;
public
static
final
String
YEAR_PATTERN
=
"yyyy"
;
public
static
final
String
YEAR_PATTERN
=
"yyyy"
;
public
static
final
String
MINUTE_ONLY_PATTERN
=
"mm"
;
public
static
final
String
MINUTE_ONLY_PATTERN
=
"mm"
;
public
static
final
String
HOUR_ONLY_PATTERN
=
"HH"
;
public
static
final
String
HOUR_ONLY_PATTERN
=
"HH"
;
public
static
final
String
DATE_PATTERN_NUM
=
"yyyyMMdd"
;
public
static
final
String
DATE_PATTERN_NUM
=
"yyyyMMdd"
;
public
static
final
String
CHN_DATE_PATTERN_YEAR
=
"yyyy年"
;
public
static
final
String
CHN_DATE_PATTERN_YEAR
=
"yyyy年"
;
public
static
final
String
CHN_DATE_PATTERN_MONTH
=
"MM月"
;
public
static
final
String
CHN_DATE_PATTERN_MONTH
=
"MM月"
;
public
static
final
String
CHN_DATE_PATTERN
=
"yyyy年MM月dd日"
;
public
static
final
String
CHN_DATE_PATTERN
=
"yyyy年MM月dd日"
;
/**
/**
* 获取 当前年、半年、季度、月、日、小时 开始结束时间
* 获取 当前年、半年、季度、月、日、小时 开始结束时间
*/
*/
private
static
final
SimpleDateFormat
shortSdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
private
static
final
SimpleDateFormat
shortSdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
private
static
final
SimpleDateFormat
longHourSdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH"
);
private
static
final
SimpleDateFormat
longHourSdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH"
);
private
static
final
SimpleDateFormat
longSdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
private
static
final
SimpleDateFormat
longSdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
private
static
final
Calendar
calendar
=
Calendar
.
getInstance
();
private
static
final
Calendar
calendar
=
Calendar
.
getInstance
();
private
static
final
SimpleDateFormat
shortDateNew
=
new
SimpleDateFormat
(
"yyyy/M/d"
);
private
static
final
SimpleDateFormat
shortDateNew
=
new
SimpleDateFormat
(
"yyyy/M/d"
);
/**
/**
* 获取时分秒
* 获取时分秒
*/
*/
private
static
final
SimpleDateFormat
timeSdf
=
new
SimpleDateFormat
(
"HH:mm:ss"
);
private
static
final
SimpleDateFormat
timeSdf
=
new
SimpleDateFormat
(
"HH:mm:ss"
);
/**
/**
* 获取当前时间
* 获取当前时间
*
*
* @return 当前日期时间
* @return 当前日期时间
*/
*/
public
static
Date
getDateNow
()
{
public
static
Date
getDateNow
()
{
return
new
Date
();
return
new
Date
();
}
}
/**
/**
* 获取当前时间
* 获取当前时间
*
*
* @return 当前日期时间
* @return 当前日期时间
*/
*/
public
static
String
getDateNowString
()
{
public
static
String
getDateNowString
()
{
return
convertDateToString
(
getDateNow
(),
DATE_TIME_PATTERN
);
return
convertDateToString
(
getDateNow
(),
DATE_TIME_PATTERN
);
}
}
public
static
String
date2LongStr
(
Date
dateDate
)
{
public
static
String
date2LongStr
(
Date
dateDate
)
{
SimpleDateFormat
formatter
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
SimpleDateFormat
formatter
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
String
dateString
=
formatter
.
format
(
dateDate
);
String
dateString
=
formatter
.
format
(
dateDate
);
return
dateString
;
return
dateString
;
}
}
/**
/**
* 获取现在日期字符串
* 获取现在日期字符串
*
*
* @return返回字符串格式 yyyy-MM-dd
* @return返回字符串格式 yyyy-MM-dd
*/
*/
public
static
String
getDateNowShortStr
()
{
public
static
String
getDateNowShortStr
()
{
Date
currentTime
=
new
Date
();
Date
currentTime
=
new
Date
();
SimpleDateFormat
formatter
=
new
SimpleDateFormat
(
DATE_PATTERN
);
SimpleDateFormat
formatter
=
new
SimpleDateFormat
(
DATE_PATTERN
);
String
dateString
=
formatter
.
format
(
currentTime
);
String
dateString
=
formatter
.
format
(
currentTime
);
return
dateString
;
return
dateString
;
}
}
/**
/**
* 将日期转换为字符串
* 将日期转换为字符串
*
*
* @param date 日期
* @param date 日期
* @param format 字符串格式
* @param format 字符串格式
* @return 日期字符串
* @return 日期字符串
*/
*/
public
static
String
convertDateToString
(
Date
date
,
String
format
)
{
public
static
String
convertDateToString
(
Date
date
,
String
format
)
{
SimpleDateFormat
df
=
null
;
SimpleDateFormat
df
=
null
;
String
returnValue
=
""
;
String
returnValue
=
""
;
if
(
null
!=
date
)
{
if
(
null
!=
date
)
{
df
=
new
SimpleDateFormat
(
format
);
df
=
new
SimpleDateFormat
(
format
);
returnValue
=
df
.
format
(
date
);
returnValue
=
df
.
format
(
date
);
}
}
return
returnValue
;
return
returnValue
;
}
}
/**
/**
* 获取当前时间任意
* 获取当前时间任意
*
*
* @return
* @return
*/
*/
public
static
String
get
(
int
field
)
{
public
static
String
get
(
int
field
)
{
return
String
.
valueOf
(
getCalendar
().
get
(
field
));
return
String
.
valueOf
(
getCalendar
().
get
(
field
));
}
}
/**
/**
* 当前日历,这里用中国时间表示
* 当前日历,这里用中国时间表示
*
*
* @return 以当地时区表示的系统当前日历
* @return 以当地时区表示的系统当前日历
*/
*/
public
static
Calendar
getCalendar
()
{
public
static
Calendar
getCalendar
()
{
return
Calendar
.
getInstance
();
return
Calendar
.
getInstance
();
}
}
/**
/**
* 日期相加减天数
* 日期相加减天数
*
*
* @param date 如果为Null,则为当前时间
* @param date 如果为Null,则为当前时间
* @param days 加减天数
* @param days 加减天数
* @param includeTime 是否包括时分秒,true表示包含
* @param includeTime 是否包括时分秒,true表示包含
* @return
* @return
* @throws ParseException
* @throws ParseException
*/
*/
public
static
Date
dateAdd
(
Date
date
,
int
days
,
boolean
includeTime
)
throws
ParseException
{
public
static
Date
dateAdd
(
Date
date
,
int
days
,
boolean
includeTime
)
throws
ParseException
{
if
(
date
==
null
)
{
if
(
date
==
null
)
{
date
=
new
Date
();
date
=
new
Date
();
}
}
if
(!
includeTime
)
{
if
(!
includeTime
)
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
DateUtils
.
DATE_PATTERN
);
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
DateUtils
.
DATE_PATTERN
);
date
=
sdf
.
parse
(
sdf
.
format
(
date
));
date
=
sdf
.
parse
(
sdf
.
format
(
date
));
}
}
Calendar
cal
=
Calendar
.
getInstance
();
Calendar
cal
=
Calendar
.
getInstance
();
cal
.
setTime
(
date
);
cal
.
setTime
(
date
);
cal
.
add
(
Calendar
.
DATE
,
days
);
cal
.
add
(
Calendar
.
DATE
,
days
);
return
cal
.
getTime
();
return
cal
.
getTime
();
}
}
/**
/**
* 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
* 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
*
*
* @param strDate
* @param strDate
* @return
* @return
*/
*/
public
static
Date
longStr2Date
(
String
strDate
)
{
public
static
Date
longStr2Date
(
String
strDate
)
{
SimpleDateFormat
formatter
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
SimpleDateFormat
formatter
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
ParsePosition
pos
=
new
ParsePosition
(
0
);
ParsePosition
pos
=
new
ParsePosition
(
0
);
Date
strtodate
=
formatter
.
parse
(
strDate
,
pos
);
Date
strtodate
=
formatter
.
parse
(
strDate
,
pos
);
return
strtodate
;
return
strtodate
;
}
}
/**
/**
* 时间格式化成字符串
* 时间格式化成字符串
*
*
* @param date Date
* @param date Date
* @param pattern StrUtils.DATE_TIME_PATTERN || StrUtils.DATE_PATTERN,
* @param pattern StrUtils.DATE_TIME_PATTERN || StrUtils.DATE_PATTERN, 如果为空,则为yyyy-MM-dd
* 如果为空,则为yyyy-MM-dd
* @return
* @return
* @throws ParseException
* @throws ParseException
*/
*/
public
static
String
dateFormat
(
Date
date
,
String
pattern
)
throws
ParseException
{
public
static
String
dateFormat
(
Date
date
,
String
pattern
)
throws
ParseException
{
if
(
StringUtils
.
isEmpty
(
pattern
))
{
if
(
StringUtils
.
isEmpty
(
pattern
))
{
pattern
=
DateUtils
.
DATE_PATTERN
;
pattern
=
DateUtils
.
DATE_PATTERN
;
}
}
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
pattern
);
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
pattern
);
return
sdf
.
format
(
date
);
return
sdf
.
format
(
date
);
}
}
/**
/**
* 暂时不操作原生截取做下转换
* 暂时不操作原生截取做下转换
*
*
* @param str
* @param str
* @return
* @return
* @throws ParseException
* @throws ParseException
*/
*/
public
static
String
dateToString
(
String
str
)
{
public
static
String
dateToString
(
String
str
)
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
Date
parse
=
null
;
Date
parse
=
null
;
String
content
=
null
;
String
content
=
null
;
try
{
try
{
parse
=
sdf
.
parse
(
str
);
parse
=
sdf
.
parse
(
str
);
content
=
DateUtils
.
dateFormat
(
parse
,
DateUtils
.
DATE_PATTERN
);
content
=
DateUtils
.
dateFormat
(
parse
,
DateUtils
.
DATE_PATTERN
);
}
catch
(
ParseException
e
)
{
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
return
content
;
return
content
;
}
}
/*
/*
* 将时间戳转换为时间
* 将时间戳转换为时间
*/
*/
public
static
String
stampToDate
(
Long
s
,
String
format
)
{
public
static
String
stampToDate
(
Long
s
,
String
format
)
{
String
res
;
String
res
;
SimpleDateFormat
simpleDateFormat
=
new
SimpleDateFormat
(
format
);
SimpleDateFormat
simpleDateFormat
=
new
SimpleDateFormat
(
format
);
Date
date
=
new
Date
(
s
);
Date
date
=
new
Date
(
s
);
res
=
simpleDateFormat
.
format
(
date
);
res
=
simpleDateFormat
.
format
(
date
);
return
res
;
return
res
;
}
}
/**
/**
* 字符串解析成时间对象
* 字符串解析成时间对象
*
*
* @param dateTimeString String
* @param dateTimeString String
* @param pattern StrUtils.DATE_TIME_PATTERN ||
* @param pattern StrUtils.DATE_TIME_PATTERN || StrUtils.DATE_PATTERN,如果为空,则为yyyy-MM-dd
* StrUtils.DATE_PATTERN,如果为空,则为yyyy-MM-dd
* @return
* @return
* @throws ParseException
* @throws ParseException
*/
*/
public
static
Date
dateParse
(
String
dateTimeString
,
String
pattern
)
throws
ParseException
{
public
static
Date
dateParse
(
String
dateTimeString
,
String
pattern
)
throws
ParseException
{
if
(
StringUtils
.
isEmpty
(
pattern
))
{
if
(
StringUtils
.
isEmpty
(
pattern
))
{
pattern
=
DateUtils
.
DATE_PATTERN
;
pattern
=
DateUtils
.
DATE_PATTERN
;
}
}
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
pattern
);
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
pattern
);
return
sdf
.
parse
(
dateTimeString
);
return
sdf
.
parse
(
dateTimeString
);
}
}
/**
/**
* 将日期时间格式成只有日期的字符串(可以直接使用dateFormat,Pattern为Null进行格式化)
* 字符串解析成时间对象
*
*
* @param dateTime Date
* @param dateTimeString String
* @return
* @param pattern StrUtils.DATE_TIME_PATTERN ||
* @throws ParseException
* StrUtils.DATE_PATTERN,如果为空,则为yyyy-MM-dd
*/
* @return
public
static
String
dateTimeToDateString
(
Date
dateTime
)
throws
ParseException
{
* @throws ParseException
String
dateTimeString
=
DateUtils
.
dateFormat
(
dateTime
,
DateUtils
.
DATE_TIME_PATTERN
);
*/
return
dateTimeString
.
substring
(
0
,
10
);
public
static
Date
dateParse
(
String
dateTimeString
)
{
}
if
(
dateTimeString
==
null
)
{
return
null
;
/**
}
* 当时、分、秒为00:00:00时,将日期时间格式成只有日期的字符串,
try
{
* 当时、分、秒不为00:00:00时,直接返回
if
(
dateTimeString
.
length
()
==
10
)
{
*
return
dateParse
(
dateTimeString
,
DateUtils
.
DATE_PATTERN
);
* @param dateTime Date
}
else
if
(
dateTimeString
.
length
()
==
13
)
{
* @return
return
dateParse
(
dateTimeString
,
DateUtils
.
HOUR_PATTERN
);
* @throws ParseException
}
else
if
(
dateTimeString
.
length
()
==
16
)
{
*/
return
dateParse
(
dateTimeString
,
DateUtils
.
MINUTE_PATTERN
);
public
static
String
dateTimeToDateStringIfTimeEndZero
(
Date
dateTime
)
throws
ParseException
{
}
else
{
String
dateTimeString
=
DateUtils
.
dateFormat
(
dateTime
,
DateUtils
.
DATE_TIME_PATTERN
);
return
dateParse
(
dateTimeString
,
DateUtils
.
DATE_TIME_PATTERN
);
if
(
dateTimeString
.
endsWith
(
"00:00:00"
))
{
}
return
dateTimeString
.
substring
(
0
,
10
);
}
catch
(
ParseException
e
)
{
}
else
{
e
.
printStackTrace
();
return
dateTimeString
;
return
null
;
}
}
}
}
/**
/**
* 将日期时间格式成日期对象,和dateParse互用
* 将日期时间格式成只有日期的字符串(可以直接使用dateFormat,Pattern为Null进行格式化)
*
*
* @param dateTime Date
* @param dateTime Date
* @return Date
* @return
* @throws ParseException
* @throws ParseException
*/
*/
public
static
Date
dateTimeToDate
(
Date
dateTime
)
throws
ParseException
{
public
static
String
dateTimeToDateString
(
Date
dateTime
)
throws
ParseException
{
Calendar
cal
=
Calendar
.
getInstance
();
String
dateTimeString
=
DateUtils
.
dateFormat
(
dateTime
,
DateUtils
.
DATE_TIME_PATTERN
);
cal
.
setTime
(
dateTime
);
return
dateTimeString
.
substring
(
0
,
10
);
cal
.
set
(
Calendar
.
HOUR_OF_DAY
,
0
);
}
cal
.
set
(
Calendar
.
MINUTE
,
0
);
cal
.
set
(
Calendar
.
SECOND
,
0
);
/**
cal
.
set
(
Calendar
.
MILLISECOND
,
0
);
* 当时、分、秒为00:00:00时,将日期时间格式成只有日期的字符串, 当时、分、秒不为00:00:00时,直接返回
return
cal
.
getTime
();
*
}
* @param dateTime Date
* @return
/**
* @throws ParseException
* 时间加减小时
*/
*
public
static
String
dateTimeToDateStringIfTimeEndZero
(
Date
dateTime
)
throws
ParseException
{
* @param startDate 要处理的时间,Null则为当前时间
String
dateTimeString
=
DateUtils
.
dateFormat
(
dateTime
,
DateUtils
.
DATE_TIME_PATTERN
);
* @param hours 加减的小时
if
(
dateTimeString
.
endsWith
(
"00:00:00"
))
{
* @return Date
return
dateTimeString
.
substring
(
0
,
10
);
*/
}
else
{
public
static
Date
dateAddHours
(
Date
startDate
,
int
hours
)
{
return
dateTimeString
;
if
(
startDate
==
null
)
{
}
startDate
=
new
Date
();
}
}
Calendar
c
=
Calendar
.
getInstance
();
/**
c
.
setTime
(
startDate
);
* 将日期时间格式成日期对象,和dateParse互用
c
.
set
(
Calendar
.
HOUR
,
c
.
get
(
Calendar
.
HOUR
)
+
hours
);
*
return
c
.
getTime
();
* @param dateTime Date
}
* @return Date
* @throws ParseException
/**
*/
* 时间加减分钟
public
static
Date
dateTimeToDate
(
Date
dateTime
)
throws
ParseException
{
*
Calendar
cal
=
Calendar
.
getInstance
();
* @param startDate 要处理的时间,Null则为当前时间
cal
.
setTime
(
dateTime
);
* @param minutes 加减的分钟
cal
.
set
(
Calendar
.
HOUR_OF_DAY
,
0
);
* @return
cal
.
set
(
Calendar
.
MINUTE
,
0
);
*/
cal
.
set
(
Calendar
.
SECOND
,
0
);
public
static
Date
dateAddMinutes
(
Date
startDate
,
int
minutes
)
{
cal
.
set
(
Calendar
.
MILLISECOND
,
0
);
if
(
startDate
==
null
)
{
return
cal
.
getTime
();
startDate
=
new
Date
();
}
}
Calendar
c
=
Calendar
.
getInstance
();
/**
c
.
setTime
(
startDate
);
* 时间加减小时
c
.
set
(
Calendar
.
MINUTE
,
c
.
get
(
Calendar
.
MINUTE
)
+
minutes
);
*
return
c
.
getTime
();
* @param startDate 要处理的时间,Null则为当前时间
}
* @param hours 加减的小时
* @return Date
/**
*/
* 时间加减秒数
public
static
Date
dateAddHours
(
Date
startDate
,
int
hours
)
{
*
if
(
startDate
==
null
)
{
* @param startDate 要处理的时间,Null则为当前时间
startDate
=
new
Date
();
* @param
}
* @return
Calendar
c
=
Calendar
.
getInstance
();
*/
c
.
setTime
(
startDate
);
public
static
Date
dateAddSeconds
(
Date
startDate
,
int
seconds
)
{
c
.
set
(
Calendar
.
HOUR
,
c
.
get
(
Calendar
.
HOUR
)
+
hours
);
if
(
startDate
==
null
)
{
return
c
.
getTime
();
startDate
=
new
Date
();
}
}
Calendar
c
=
Calendar
.
getInstance
();
/**
c
.
setTime
(
startDate
);
* 时间加减分钟
c
.
set
(
Calendar
.
SECOND
,
c
.
get
(
Calendar
.
SECOND
)
+
seconds
);
*
return
c
.
getTime
();
* @param startDate 要处理的时间,Null则为当前时间
}
* @param minutes 加减的分钟
* @return
/**
*/
* 时间加减天数
public
static
Date
dateAddMinutes
(
Date
startDate
,
int
minutes
)
{
*
if
(
startDate
==
null
)
{
* @param startDate 要处理的时间,Null则为当前时间
startDate
=
new
Date
();
* @param days 加减的天数
}
* @return Date
Calendar
c
=
Calendar
.
getInstance
();
*/
c
.
setTime
(
startDate
);
public
static
Date
dateAddDays
(
Date
startDate
,
int
days
)
{
c
.
set
(
Calendar
.
MINUTE
,
c
.
get
(
Calendar
.
MINUTE
)
+
minutes
);
if
(
startDate
==
null
)
{
return
c
.
getTime
();
startDate
=
new
Date
();
}
}
Calendar
c
=
Calendar
.
getInstance
();
/**
c
.
setTime
(
startDate
);
* 时间加减秒数
c
.
set
(
Calendar
.
DATE
,
c
.
get
(
Calendar
.
DATE
)
+
days
);
*
return
c
.
getTime
();
* @param startDate 要处理的时间,Null则为当前时间
}
* @param
* @return
/**
*/
* 时间加减月数
public
static
Date
dateAddSeconds
(
Date
startDate
,
int
seconds
)
{
*
if
(
startDate
==
null
)
{
* @param startDate 要处理的时间,Null则为当前时间
startDate
=
new
Date
();
* @param months 加减的月数
}
* @return Date
Calendar
c
=
Calendar
.
getInstance
();
*/
c
.
setTime
(
startDate
);
public
static
Date
dateAddMonths
(
Date
startDate
,
int
months
)
{
c
.
set
(
Calendar
.
SECOND
,
c
.
get
(
Calendar
.
SECOND
)
+
seconds
);
if
(
startDate
==
null
)
{
return
c
.
getTime
();
startDate
=
new
Date
();
}
}
Calendar
c
=
Calendar
.
getInstance
();
/**
c
.
setTime
(
startDate
);
* 时间加减天数
c
.
set
(
Calendar
.
MONTH
,
c
.
get
(
Calendar
.
MONTH
)
+
months
);
*
return
c
.
getTime
();
* @param startDate 要处理的时间,Null则为当前时间
}
* @param days 加减的天数
* @return Date
/**
*/
* 时间加减年数
public
static
Date
dateAddDays
(
Date
startDate
,
int
days
)
{
*
if
(
startDate
==
null
)
{
* @param startDate 要处理的时间,Null则为当前时间
startDate
=
new
Date
();
* @param years 加减的年数
}
* @return Date
Calendar
c
=
Calendar
.
getInstance
();
*/
c
.
setTime
(
startDate
);
public
static
Date
dateAddYears
(
Date
startDate
,
int
years
)
{
c
.
set
(
Calendar
.
DATE
,
c
.
get
(
Calendar
.
DATE
)
+
days
);
if
(
startDate
==
null
)
{
return
c
.
getTime
();
startDate
=
new
Date
();
}
}
Calendar
c
=
Calendar
.
getInstance
();
/**
c
.
setTime
(
startDate
);
* 时间加减月数
c
.
set
(
Calendar
.
YEAR
,
c
.
get
(
Calendar
.
YEAR
)
+
years
);
*
return
c
.
getTime
();
* @param startDate 要处理的时间,Null则为当前时间
}
* @param months 加减的月数
* @return Date
/**
*/
* 时间比较(如果myDate>compareDate返回1,<返回-1,相等返回0)
public
static
Date
dateAddMonths
(
Date
startDate
,
int
months
)
{
*
if
(
startDate
==
null
)
{
* @param myDate 时间
startDate
=
new
Date
();
* @param compareDate 要比较的时间
}
* @return int
Calendar
c
=
Calendar
.
getInstance
();
*/
c
.
setTime
(
startDate
);
public
static
int
dateCompare
(
Date
myDate
,
Date
compareDate
)
{
c
.
set
(
Calendar
.
MONTH
,
c
.
get
(
Calendar
.
MONTH
)
+
months
);
Calendar
myCal
=
Calendar
.
getInstance
();
return
c
.
getTime
();
Calendar
compareCal
=
Calendar
.
getInstance
();
}
myCal
.
setTime
(
myDate
);
compareCal
.
setTime
(
compareDate
);
/**
return
myCal
.
compareTo
(
compareCal
);
* 时间加减年数
}
*
* @param startDate 要处理的时间,Null则为当前时间
/**
* @param years 加减的年数
* 获取两个时间中最小的一个时间
* @return Date
*
*/
* @param date
public
static
Date
dateAddYears
(
Date
startDate
,
int
years
)
{
* @param compareDate
if
(
startDate
==
null
)
{
* @return
startDate
=
new
Date
();
*/
}
public
static
Date
dateMin
(
Date
date
,
Date
compareDate
)
{
Calendar
c
=
Calendar
.
getInstance
();
if
(
date
==
null
)
{
c
.
setTime
(
startDate
);
return
compareDate
;
c
.
set
(
Calendar
.
YEAR
,
c
.
get
(
Calendar
.
YEAR
)
+
years
);
}
return
c
.
getTime
();
if
(
compareDate
==
null
)
{
}
return
date
;
}
/**
if
(
1
==
dateCompare
(
date
,
compareDate
))
{
* 时间比较(如果myDate>compareDate返回1,<返回-1,相等返回0)
return
compareDate
;
*
}
else
if
(-
1
==
dateCompare
(
date
,
compareDate
))
{
* @param myDate 时间
return
date
;
* @param compareDate 要比较的时间
}
* @return int
return
date
;
*/
}
public
static
int
dateCompare
(
Date
myDate
,
Date
compareDate
)
{
Calendar
myCal
=
Calendar
.
getInstance
();
/**
Calendar
compareCal
=
Calendar
.
getInstance
();
* 获取两个时间中最大的一个时间
myCal
.
setTime
(
myDate
);
*
compareCal
.
setTime
(
compareDate
);
* @param date
return
myCal
.
compareTo
(
compareCal
);
* @param compareDate
}
* @return
*/
/**
public
static
Date
dateMax
(
Date
date
,
Date
compareDate
)
{
* 获取两个时间中最小的一个时间
if
(
date
==
null
)
{
*
return
compareDate
;
* @param date
}
* @param compareDate
if
(
compareDate
==
null
)
{
* @return
return
date
;
*/
}
public
static
Date
dateMin
(
Date
date
,
Date
compareDate
)
{
if
(
1
==
dateCompare
(
date
,
compareDate
))
{
if
(
date
==
null
)
{
return
date
;
return
compareDate
;
}
else
if
(-
1
==
dateCompare
(
date
,
compareDate
))
{
}
return
compareDate
;
if
(
compareDate
==
null
)
{
}
return
date
;
return
date
;
}
}
if
(
1
==
dateCompare
(
date
,
compareDate
))
{
return
compareDate
;
/**
}
else
if
(-
1
==
dateCompare
(
date
,
compareDate
))
{
* 获取两个日期(不含时分秒)相差的天数,不包含今天
return
date
;
*
}
* @param startDate
return
date
;
* @param endDate
}
* @return
* @throws ParseException
/**
*/
* 获取两个时间中最大的一个时间
public
static
int
dateBetween
(
Date
startDate
,
Date
endDate
)
throws
ParseException
{
*
Date
dateStart
=
dateParse
(
dateFormat
(
startDate
,
DATE_PATTERN
),
DATE_PATTERN
);
* @param date
Date
dateEnd
=
dateParse
(
dateFormat
(
endDate
,
DATE_PATTERN
),
DATE_PATTERN
);
* @param compareDate
return
(
int
)
((
dateEnd
.
getTime
()
-
dateStart
.
getTime
())
/
1000
/
60
/
60
/
24
);
* @return
}
*/
public
static
Date
dateMax
(
Date
date
,
Date
compareDate
)
{
/**
if
(
date
==
null
)
{
* 获取两个日期(不含时分秒)相差的天数,包含今天
return
compareDate
;
*
}
* @param startDate
if
(
compareDate
==
null
)
{
* @param endDate
return
date
;
* @return
}
* @throws ParseException
if
(
1
==
dateCompare
(
date
,
compareDate
))
{
*/
return
date
;
public
static
int
dateBetweenIncludeToday
(
Date
startDate
,
Date
endDate
)
throws
ParseException
{
}
else
if
(-
1
==
dateCompare
(
date
,
compareDate
))
{
return
dateBetween
(
startDate
,
endDate
)
+
1
;
return
compareDate
;
}
}
return
date
;
/**
}
* 获取日期时间的年份,如2017-02-13,返回2017
*
/**
* @param date
* 获取两个日期(不含时分秒)相差的天数,不包含今天
* @return
*
*/
* @param startDate
public
static
int
getYear
(
Date
date
)
{
* @param endDate
Calendar
cal
=
Calendar
.
getInstance
();
* @return
cal
.
setTime
(
date
);
* @throws ParseException
return
cal
.
get
(
Calendar
.
YEAR
);
*/
}
public
static
int
dateBetween
(
Date
startDate
,
Date
endDate
)
throws
ParseException
{
Date
dateStart
=
dateParse
(
dateFormat
(
startDate
,
DATE_PATTERN
),
DATE_PATTERN
);
/**
Date
dateEnd
=
dateParse
(
dateFormat
(
endDate
,
DATE_PATTERN
),
DATE_PATTERN
);
* 获取日期时间的月份,如2017年2月13日,返回2
return
(
int
)
((
dateEnd
.
getTime
()
-
dateStart
.
getTime
())
/
1000
/
60
/
60
/
24
);
*
}
* @param date
* @return
/**
*/
* 获取两个日期(不含时分秒)相差的天数,包含今天
public
static
int
getMonth
(
Date
date
)
{
*
Calendar
cal
=
Calendar
.
getInstance
();
* @param startDate
cal
.
setTime
(
date
);
* @param endDate
return
cal
.
get
(
Calendar
.
MONTH
)
+
1
;
* @return
}
* @throws ParseException
*/
/**
public
static
int
dateBetweenIncludeToday
(
Date
startDate
,
Date
endDate
)
throws
ParseException
{
* 获取日期时间的第几天(即返回日期的dd),如2017-02-13,返回13
return
dateBetween
(
startDate
,
endDate
)
+
1
;
*
}
* @param date
* @return
/**
*/
* 获取日期时间的年份,如2017-02-13,返回2017
public
static
int
getDate
(
Date
date
)
{
*
Calendar
cal
=
Calendar
.
getInstance
();
* @param date
cal
.
setTime
(
date
);
* @return
return
cal
.
get
(
Calendar
.
DATE
);
*/
}
public
static
int
getYear
(
Date
date
)
{
Calendar
cal
=
Calendar
.
getInstance
();
/**
cal
.
setTime
(
date
);
* 获取日期时间当月的总天数,如2017-02-13,返回28
return
cal
.
get
(
Calendar
.
YEAR
);
*
}
* @param date
* @return
/**
*/
* 获取日期时间的月份,如2017年2月13日,返回2
public
static
int
getDaysOfMonth
(
Date
date
)
{
*
Calendar
cal
=
Calendar
.
getInstance
();
* @param date
cal
.
setTime
(
date
);
* @return
return
cal
.
getActualMaximum
(
Calendar
.
DATE
);
*/
}
public
static
int
getMonth
(
Date
date
)
{
Calendar
cal
=
Calendar
.
getInstance
();
/**
cal
.
setTime
(
date
);
* 获取日期时间当年的总天数,如2017-02-13,返回2017年的总天数
return
cal
.
get
(
Calendar
.
MONTH
)
+
1
;
*
}
* @param date
* @return
/**
*/
* 获取日期时间的第几天(即返回日期的dd),如2017-02-13,返回13
public
static
int
getDaysOfYear
(
Date
date
)
{
*
Calendar
cal
=
Calendar
.
getInstance
();
* @param date
cal
.
setTime
(
date
);
* @return
return
cal
.
getActualMaximum
(
Calendar
.
DAY_OF_YEAR
);
*/
}
public
static
int
getDate
(
Date
date
)
{
Calendar
cal
=
Calendar
.
getInstance
();
/**
cal
.
setTime
(
date
);
* 根据时间获取当月最大的日期
return
cal
.
get
(
Calendar
.
DATE
);
* <li>2017-02-13,返回2017-02-28</li>
}
* <li>2016-02-13,返回2016-02-29</li>
* <li>2016-01-11,返回2016-01-31</li>
/**
*
* 获取日期时间当月的总天数,如2017-02-13,返回28
* @param date Date
*
* @return
* @param date
* @throws Exception
* @return
*/
*/
public
static
Date
maxDateOfMonth
(
Date
date
)
throws
Exception
{
public
static
int
getDaysOfMonth
(
Date
date
)
{
Calendar
cal
=
Calendar
.
getInstance
();
Calendar
cal
=
Calendar
.
getInstance
();
cal
.
setTime
(
date
);
cal
.
setTime
(
date
);
int
value
=
cal
.
getActualMaximum
(
Calendar
.
DATE
);
return
cal
.
getActualMaximum
(
Calendar
.
DATE
);
return
dateParse
(
dateFormat
(
date
,
MONTH_PATTERN
)
+
"-"
+
value
,
null
);
}
}
/**
/**
* 获取日期时间当年的总天数,如2017-02-13,返回2017年的总天数
* 根据时间获取当月最小的日期,也就是返回当月的1号日期对象
*
*
* @param date
* @param date Date
* @return
* @return
*/
* @throws Exception
public
static
int
getDaysOfYear
(
Date
date
)
{
*/
Calendar
cal
=
Calendar
.
getInstance
();
public
static
Date
minDateOfMonth
(
Date
date
)
throws
Exception
{
cal
.
setTime
(
date
);
Calendar
cal
=
Calendar
.
getInstance
();
return
cal
.
getActualMaximum
(
Calendar
.
DAY_OF_YEAR
);
cal
.
setTime
(
date
);
}
int
value
=
cal
.
getActualMinimum
(
Calendar
.
DATE
);
return
dateParse
(
dateFormat
(
date
,
MONTH_PATTERN
)
+
"-"
+
value
,
null
);
/**
}
* 根据时间获取当月最大的日期
* <li>2017-02-13,返回2017-02-28</li>
/**
* <li>2016-02-13,返回2016-02-29</li>
* 根据年月返回第一天
* <li>2016-01-11,返回2016-01-31</li>
*
*
* @param yearParam
* @param date Date
* @param monthParam
* @return
* @return
* @throws Exception
*/
*/
public
static
Date
getFirstDayByMonth
(
int
yearParam
,
int
monthParam
)
{
public
static
Date
maxDateOfMonth
(
Date
date
)
throws
Exception
{
Calendar
c
=
Calendar
.
getInstance
(
Locale
.
CHINA
);
Calendar
cal
=
Calendar
.
getInstance
();
c
.
set
(
yearParam
,
monthParam
-
1
,
1
);
cal
.
setTime
(
date
);
try
{
int
value
=
cal
.
getActualMaximum
(
Calendar
.
DATE
);
Date
date
=
shortSdf
.
parse
(
shortSdf
.
format
(
c
.
getTime
())
+
" 00:00:00"
);
return
dateParse
(
dateFormat
(
date
,
MONTH_PATTERN
)
+
"-"
+
value
,
null
);
return
date
;
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
/**
}
* 根据时间获取当月最小的日期,也就是返回当月的1号日期对象
return
null
;
*
}
* @param date Date
* @return
/**
* @throws Exception
* 获取某月的日期List
*/
*
public
static
Date
minDateOfMonth
(
Date
date
)
throws
Exception
{
* @param yearParam
Calendar
cal
=
Calendar
.
getInstance
();
* @param monthParam
cal
.
setTime
(
date
);
* @return
int
value
=
cal
.
getActualMinimum
(
Calendar
.
DATE
);
*/
return
dateParse
(
dateFormat
(
date
,
MONTH_PATTERN
)
+
"-"
+
value
,
null
);
public
static
List
<
String
>
getDayByMonth
(
int
yearParam
,
int
monthParam
)
{
}
List
list
=
new
ArrayList
();
Calendar
aCalendar
=
Calendar
.
getInstance
(
Locale
.
CHINA
);
/**
aCalendar
.
set
(
yearParam
,
monthParam
-
1
,
1
);
* 根据年月返回第一天
int
year
=
aCalendar
.
get
(
Calendar
.
YEAR
);
//年份
*
int
month
=
aCalendar
.
get
(
Calendar
.
MONTH
)
+
1
;
//月份
* @param yearParam
int
day
=
aCalendar
.
getActualMaximum
(
Calendar
.
DATE
);
* @param monthParam
for
(
int
i
=
1
;
i
<=
day
;
i
++)
{
* @return
*/
list
.
add
(
year
+
"/"
+
month
+
"/"
+
i
);
public
static
Date
getFirstDayByMonth
(
int
yearParam
,
int
monthParam
)
{
}
Calendar
c
=
Calendar
.
getInstance
(
Locale
.
CHINA
);
return
list
;
c
.
set
(
yearParam
,
monthParam
-
1
,
1
);
}
try
{
Date
date
=
shortSdf
.
parse
(
shortSdf
.
format
(
c
.
getTime
())
+
" 00:00:00"
);
return
date
;
/**
}
catch
(
Exception
e
)
{
* 当前月的结束时间,即2012-01-31 23:59:59
e
.
printStackTrace
();
*
}
* @return
return
null
;
*/
}
public
static
Date
getCurrentMonthEndTime
(
Date
date
)
{
Calendar
c
=
Calendar
.
getInstance
();
/**
c
.
setTime
(
date
);
* 获取某月的日期List
Date
now
=
null
;
*
c
.
set
(
Calendar
.
DATE
,
1
);
* @param yearParam
c
.
add
(
Calendar
.
MONTH
,
1
);
* @param monthParam
c
.
add
(
Calendar
.
DATE
,
-
1
);
* @return
try
{
*/
now
=
longSdf
.
parse
(
shortSdf
.
format
(
c
.
getTime
())
+
" 23:59:59"
);
public
static
List
<
String
>
getDayByMonth
(
int
yearParam
,
int
monthParam
)
{
}
catch
(
ParseException
e
)
{
List
list
=
new
ArrayList
();
e
.
printStackTrace
();
Calendar
aCalendar
=
Calendar
.
getInstance
(
Locale
.
CHINA
);
}
aCalendar
.
set
(
yearParam
,
monthParam
-
1
,
1
);
return
now
;
int
year
=
aCalendar
.
get
(
Calendar
.
YEAR
);
// 年份
int
month
=
aCalendar
.
get
(
Calendar
.
MONTH
)
+
1
;
// 月份
}
int
day
=
aCalendar
.
getActualMaximum
(
Calendar
.
DATE
);
for
(
int
i
=
1
;
i
<=
day
;
i
++)
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
/*System.out.println(dateTimeToDate(new Date()));
list
.
add
(
year
+
"/"
+
month
+
"/"
+
i
);
System.out.println(dateParse("2017-02-04 14:58:20", null));
}
System.out.println(dateTimeToDateStringIfTimeEndZero(new Date()));
return
list
;
System.out.println(dateTimeToDateStringIfTimeEndZero(dateTimeToDate(new Date())));*/
}
//System.out.println(dateBetween(dateParse("2017-01-30", null), dateParse("2017-02-01", null)));
//System.out.println(dateBetweenIncludeToday(dateParse("2017-01-30", null), dateParse("2017-02-01", null)));
/**
* 当前月的结束时间,即2012-01-31 23:59:59
*
* @return
*/
public
static
Date
getCurrentMonthEndTime
(
Date
date
)
{
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
Date
now
=
null
;
c
.
set
(
Calendar
.
DATE
,
1
);
c
.
add
(
Calendar
.
MONTH
,
1
);
c
.
add
(
Calendar
.
DATE
,
-
1
);
try
{
now
=
longSdf
.
parse
(
shortSdf
.
format
(
c
.
getTime
())
+
" 23:59:59"
);
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
}
return
now
;
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
/*
* System.out.println(dateTimeToDate(new Date()));
* System.out.println(dateParse("2017-02-04 14:58:20", null));
* System.out.println(dateTimeToDateStringIfTimeEndZero(new Date()));
* System.out.println(dateTimeToDateStringIfTimeEndZero(dateTimeToDate(new
* Date())));
*/
// System.out.println(dateBetween(dateParse("2017-01-30", null),
// dateParse("2017-02-01", null)));
// System.out.println(dateBetweenIncludeToday(dateParse("2017-01-30", null),
// dateParse("2017-02-01", null)));
// System.out.println(getDate(dateParse("2017-01-17", null)));
// System.out.println(getDate(dateParse("2017-01-17", null)));
//System.out.println(getDate(dateParse("2017-01-17", null)));
// System.out.println(getDate(dateParse("2017-01-17", null)));
/*
/*
System.out.println(getDaysOfMonth(dateParse("2017-02-01", null)));
* System.out.println(getDaysOfMonth(dateParse("2017-02-01", null)));
System.out.println(getDaysOfYear(dateParse("2017-01-30", null)));*/
* System.out.println(getDaysOfYear(dateParse("2017-01-30", null)));
//System.out.println(dateFormat(dateAddMonths(dateParse("2017-02-07", StrUtils.MONTH_PATTERN), -12), StrUtils
*/
// .MONTH_PATTERN));
// System.out.println(dateFormat(dateAddMonths(dateParse("2017-02-07",
/*System.out.println(dateFormat(maxDateOfMonth(dateParse("2016-02", "yyyy-MM")), null));
// StrUtils.MONTH_PATTERN), -12), StrUtils
System.out.println(dateFormat(minDateOfMonth(dateParse("2016-03-31", null)), null));*/
// .MONTH_PATTERN));
/*
* System.out.println(dateFormat(maxDateOfMonth(dateParse("2016-02",
* "yyyy-MM")), null));
* System.out.println(dateFormat(minDateOfMonth(dateParse("2016-03-31", null)),
* null));
*/
// System.out.println(dateFormat(new Date(), CHN_DATE_PATTERN_YEAR));
// System.out.println(dateFormat(new Date(), CHN_DATE_PATTERN_YEAR));
// System.out.println(dateFormat(new Date(), CHN_DATE_PATTERN_MONTH));
// System.out.println(dateFormat(new Date(), CHN_DATE_PATTERN_MONTH));
...
@@ -623,264 +661,263 @@ public class DateUtils {
...
@@ -623,264 +661,263 @@ public class DateUtils {
// System.out.println(getQuarterStr(getMonth(dateParse("2021-5-11", null))));
// System.out.println(getQuarterStr(getMonth(dateParse("2021-5-11", null))));
// System.out.println(getWeekBeginDate(dateParse("2021-10-11", null)));
// System.out.println(getWeekBeginDate(dateParse("2021-10-11", null)));
// System.out.println(getWeekEndDate(dateParse("2021-10-11", null)));
// System.out.println(getWeekEndDate(dateParse("2021-10-11", null)));
System
.
out
.
println
(
secondsToTimeStr
(
3600
));
System
.
out
.
println
(
secondsToTimeStr
(
3600
));
}
}
/**
/**
* 当前日期毫秒.xls
* 当前日期毫秒.xls
*
*
* @return
* @return
*/
*/
public
static
String
formatName
()
{
public
static
String
formatName
()
{
Calendar
calen
=
Calendar
.
getInstance
();
Calendar
calen
=
Calendar
.
getInstance
();
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyyMMddhhmmss"
);
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyyMMddhhmmss"
);
String
name
=
sdf
.
format
(
calen
.
getTime
());
String
name
=
sdf
.
format
(
calen
.
getTime
());
return
name
;
return
name
;
}
}
/**
/**
* 获取某月的日期List
* 获取某月的日期List
*
*
* @param dateStr
* @param dateStr
* @return
* @return
*/
*/
public
static
List
<
Date
>
getDayByMonth
(
String
dateStr
)
{
public
static
List
<
Date
>
getDayByMonth
(
String
dateStr
)
{
List
<
Date
>
list
=
new
ArrayList
();
List
<
Date
>
list
=
new
ArrayList
();
Date
date
=
null
;
Date
date
=
null
;
try
{
try
{
date
=
shortSdf
.
parse
(
dateStr
);
date
=
shortSdf
.
parse
(
dateStr
);
calendar
.
setTime
(
date
);
calendar
.
setTime
(
date
);
int
month
=
calendar
.
get
(
Calendar
.
MONTH
)
+
1
;
//月份
int
month
=
calendar
.
get
(
Calendar
.
MONTH
)
+
1
;
// 月份
int
year
=
calendar
.
get
(
Calendar
.
YEAR
);
//年份
int
year
=
calendar
.
get
(
Calendar
.
YEAR
);
// 年份
int
day
=
calendar
.
getActualMaximum
(
Calendar
.
DATE
);
int
day
=
calendar
.
getActualMaximum
(
Calendar
.
DATE
);
for
(
int
i
=
1
;
i
<=
day
;
i
++)
{
for
(
int
i
=
1
;
i
<=
day
;
i
++)
{
String
source
=
year
+
"-"
+
month
+
"-"
+
i
;
String
source
=
year
+
"-"
+
month
+
"-"
+
i
;
list
.
add
(
shortSdf
.
parse
(
source
));
list
.
add
(
shortSdf
.
parse
(
source
));
}
}
}
catch
(
ParseException
e
)
{
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
return
list
;
return
list
;
}
}
/**
/**
* 判断一个时间是否在一个时间段内
* 判断一个时间是否在一个时间段内
*
*
* @param nowTime 当前时间
* @param nowTime 当前时间
* @param beginTime 开始时间
* @param beginTime 开始时间
* @param endTime 结束时间
* @param endTime 结束时间
*/
*/
public
static
boolean
belongCalendar
(
Date
nowTime
,
Date
beginTime
,
Date
endTime
)
{
public
static
boolean
belongCalendar
(
Date
nowTime
,
Date
beginTime
,
Date
endTime
)
{
Calendar
date
=
Calendar
.
getInstance
();
Calendar
date
=
Calendar
.
getInstance
();
date
.
setTime
(
nowTime
);
date
.
setTime
(
nowTime
);
Calendar
begin
=
Calendar
.
getInstance
();
Calendar
begin
=
Calendar
.
getInstance
();
begin
.
setTime
(
beginTime
);
begin
.
setTime
(
beginTime
);
Calendar
end
=
Calendar
.
getInstance
();
Calendar
end
=
Calendar
.
getInstance
();
end
.
setTime
(
endTime
);
end
.
setTime
(
endTime
);
return
date
.
after
(
begin
)
&&
date
.
before
(
end
);
return
date
.
after
(
begin
)
&&
date
.
before
(
end
);
}
}
/**
/**
* 由出生日期获得年龄
* 由出生日期获得年龄
*
*
* @param birthDay 出生日期
* @param birthDay 出生日期
*/
*/
public
static
int
getAge
(
Date
birthDay
)
{
public
static
int
getAge
(
Date
birthDay
)
{
if
(
birthDay
==
null
)
{
if
(
birthDay
==
null
)
{
return
0
;
return
0
;
}
}
LocalDate
now
=
LocalDate
.
now
();
LocalDate
now
=
LocalDate
.
now
();
Calendar
cal
=
Calendar
.
getInstance
();
Calendar
cal
=
Calendar
.
getInstance
();
cal
.
setTime
(
birthDay
);
cal
.
setTime
(
birthDay
);
int
yearBirth
=
cal
.
get
(
Calendar
.
YEAR
);
int
yearBirth
=
cal
.
get
(
Calendar
.
YEAR
);
int
monthBirth
=
cal
.
get
(
Calendar
.
MONTH
)
+
1
;
int
monthBirth
=
cal
.
get
(
Calendar
.
MONTH
)
+
1
;
int
dayOfMonthBirth
=
cal
.
get
(
Calendar
.
DAY_OF_MONTH
);
int
dayOfMonthBirth
=
cal
.
get
(
Calendar
.
DAY_OF_MONTH
);
LocalDate
birth
=
LocalDate
.
of
(
yearBirth
,
monthBirth
,
dayOfMonthBirth
);
LocalDate
birth
=
LocalDate
.
of
(
yearBirth
,
monthBirth
,
dayOfMonthBirth
);
if
(
birth
.
isAfter
(
now
))
{
if
(
birth
.
isAfter
(
now
))
{
return
0
;
return
0
;
}
}
int
age
=
birth
.
until
(
now
).
getYears
();
int
age
=
birth
.
until
(
now
).
getYears
();
return
age
;
return
age
;
}
}
/**
/**
* 根据两个日期返回相差的时分秒
* 根据两个日期返回相差的时分秒
*
*
* @param newTime 靠后时间
* @param newTime 靠后时间
* @param oldTime 靠前时间
* @param oldTime 靠前时间
* @return
* @return
*/
*/
public
static
String
getTimestr
(
Date
newTime
,
Date
oldTime
)
{
public
static
String
getTimestr
(
Date
newTime
,
Date
oldTime
)
{
Long
newTimes
=
newTime
.
getTime
();
Long
newTimes
=
newTime
.
getTime
();
Long
oldTimes
=
oldTime
.
getTime
();
Long
oldTimes
=
oldTime
.
getTime
();
// 不改时间会多加八个小时
// 不改时间会多加八个小时
timeSdf
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"GMT+00:00"
));
timeSdf
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"GMT+00:00"
));
return
timeSdf
.
format
(
newTimes
-
oldTimes
);
return
timeSdf
.
format
(
newTimes
-
oldTimes
);
}
}
/**
/**
* 获取现在日期字符串时间戳格式
* 获取现在日期字符串时间戳格式
*
*
* @return返回字符串格式 yyyyMMdd
* @return返回字符串格式 yyyyMMdd
*/
*/
public
static
String
getDateNowShortNumber
()
{
public
static
String
getDateNowShortNumber
()
{
Date
currentTime
=
new
Date
();
Date
currentTime
=
new
Date
();
SimpleDateFormat
formatter
=
new
SimpleDateFormat
(
DATE_PATTERN_NUM
);
SimpleDateFormat
formatter
=
new
SimpleDateFormat
(
DATE_PATTERN_NUM
);
String
dateString
=
formatter
.
format
(
currentTime
);
String
dateString
=
formatter
.
format
(
currentTime
);
return
dateString
;
return
dateString
;
}
}
/**
/**
* 获取一年的第几周
* 获取一年的第几周
*
*
* @param date
* @param date
* @return
* @return
*/
*/
public
static
int
getWeekOfYear
(
Date
date
)
{
public
static
int
getWeekOfYear
(
Date
date
)
{
Calendar
c
=
Calendar
.
getInstance
();
Calendar
c
=
Calendar
.
getInstance
();
c
.
setTime
(
date
);
c
.
setTime
(
date
);
int
week_of_year
=
c
.
get
(
Calendar
.
WEEK_OF_YEAR
);
int
week_of_year
=
c
.
get
(
Calendar
.
WEEK_OF_YEAR
);
return
week_of_year
;
return
week_of_year
;
}
}
/**
/**
* 通过月份计算季度
* 通过月份计算季度
*
*
* @param month
* @param month
* @return
* @return
*/
*/
public
static
int
getQuarter
(
int
month
)
{
public
static
int
getQuarter
(
int
month
)
{
if
(
month
<
1
||
month
>
12
)
{
if
(
month
<
1
||
month
>
12
)
{
throw
new
IllegalArgumentException
(
"month is invalid."
);
throw
new
IllegalArgumentException
(
"month is invalid."
);
}
}
return
(
month
-
1
)
/
3
+
1
;
return
(
month
-
1
)
/
3
+
1
;
}
}
/**
/**
* 通过月份计算季度
* 通过月份计算季度
*
*
* @param month
* @param month
* @return
* @return
*/
*/
public
static
String
getQuarterStr
(
int
month
)
{
public
static
String
getQuarterStr
(
int
month
)
{
if
(
month
<
1
||
month
>
12
)
{
if
(
month
<
1
||
month
>
12
)
{
throw
new
IllegalArgumentException
(
"month is invalid."
);
throw
new
IllegalArgumentException
(
"month is invalid."
);
}
}
return
(
month
-
1
)
/
3
+
1
+
""
;
return
(
month
-
1
)
/
3
+
1
+
""
;
}
}
/**
/**
* 获得本天的开始时间,即2012-01-01 00:00:00
* 获得本天的开始时间,即2012-01-01 00:00:00
*
*
* @return
* @return
*/
*/
public
static
Date
getCurrentDayStartTime
(
Date
date
)
{
public
static
Date
getCurrentDayStartTime
(
Date
date
)
{
try
{
try
{
date
=
shortSdf
.
parse
(
shortSdf
.
format
(
date
)
+
" 00:00:00"
);
date
=
shortSdf
.
parse
(
shortSdf
.
format
(
date
)
+
" 00:00:00"
);
}
catch
(
ParseException
e
)
{
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
return
date
;
return
date
;
}
}
/**
/**
* 获得本天的结束时间,即2012-01-01 23:59:59
* 获得本天的结束时间,即2012-01-01 23:59:59
*
*
* @return
* @return
*/
*/
public
static
Date
getCurrentDayEndTime
(
Date
date
)
{
public
static
Date
getCurrentDayEndTime
(
Date
date
)
{
try
{
try
{
date
=
longSdf
.
parse
(
shortSdf
.
format
(
date
)
+
" 23:59:59"
);
date
=
longSdf
.
parse
(
shortSdf
.
format
(
date
)
+
" 23:59:59"
);
}
catch
(
ParseException
e
)
{
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
return
date
;
return
date
;
}
}
/**
/**
* 获取指定时间所在周的第一天日期
* 获取指定时间所在周的第一天日期
*
*
* @param date
* @param date
* @return
* @return
*/
*/
public
static
int
getWeekBeginDate
(
Date
date
)
{
public
static
int
getWeekBeginDate
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setFirstDayOfWeek
(
Calendar
.
MONDAY
);
calendar
.
setFirstDayOfWeek
(
Calendar
.
MONDAY
);
calendar
.
setTime
(
date
);
calendar
.
setTime
(
date
);
calendar
.
set
(
Calendar
.
DAY_OF_WEEK
,
Calendar
.
MONDAY
);
calendar
.
set
(
Calendar
.
DAY_OF_WEEK
,
Calendar
.
MONDAY
);
System
.
out
.
println
(
calendar
.
getTime
());
System
.
out
.
println
(
calendar
.
getTime
());
return
getDate
(
calendar
.
getTime
());
return
getDate
(
calendar
.
getTime
());
}
}
public
static
Date
beginDateOfWeek
(
Date
date
)
{
public
static
Date
beginDateOfWeek
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setFirstDayOfWeek
(
Calendar
.
MONDAY
);
calendar
.
setFirstDayOfWeek
(
Calendar
.
MONDAY
);
calendar
.
setTime
(
date
);
calendar
.
setTime
(
date
);
calendar
.
set
(
Calendar
.
DAY_OF_WEEK
,
Calendar
.
MONDAY
);
calendar
.
set
(
Calendar
.
DAY_OF_WEEK
,
Calendar
.
MONDAY
);
return
calendar
.
getTime
();
return
calendar
.
getTime
();
}
}
public
static
Date
endDateOfWeek
(
Date
date
)
{
public
static
Date
endDateOfWeek
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setFirstDayOfWeek
(
Calendar
.
MONDAY
);
calendar
.
setFirstDayOfWeek
(
Calendar
.
MONDAY
);
calendar
.
setTime
(
date
);
calendar
.
setTime
(
date
);
calendar
.
set
(
Calendar
.
DAY_OF_WEEK
,
Calendar
.
SUNDAY
);
calendar
.
set
(
Calendar
.
DAY_OF_WEEK
,
Calendar
.
SUNDAY
);
return
calendar
.
getTime
();
return
calendar
.
getTime
();
}
}
/**
/**
* 获取指定时间所在周的最后一天日期
* 获取指定时间所在周的最后一天日期
*
*
* @param date
* @param date
* @return
* @return
*/
*/
public
static
int
getWeekEndDate
(
Date
date
)
{
public
static
int
getWeekEndDate
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setFirstDayOfWeek
(
Calendar
.
MONDAY
);
calendar
.
setFirstDayOfWeek
(
Calendar
.
MONDAY
);
calendar
.
setTime
(
date
);
calendar
.
setTime
(
date
);
calendar
.
set
(
Calendar
.
DAY_OF_WEEK
,
Calendar
.
SUNDAY
);
calendar
.
set
(
Calendar
.
DAY_OF_WEEK
,
Calendar
.
SUNDAY
);
System
.
out
.
println
(
calendar
.
getTime
());
System
.
out
.
println
(
calendar
.
getTime
());
return
getDate
(
calendar
.
getTime
());
return
getDate
(
calendar
.
getTime
());
}
}
public
static
Date
getBeginDateOfMonth
(
Date
date
)
{
public
static
Date
getBeginDateOfMonth
(
Date
date
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
date
);
calendar
.
setTime
(
date
);
calendar
.
set
(
Calendar
.
DAY_OF_MONTH
,
calendar
.
getActualMinimum
(
Calendar
.
DAY_OF_MONTH
));
calendar
.
set
(
Calendar
.
DAY_OF_MONTH
,
calendar
.
getActualMinimum
(
Calendar
.
DAY_OF_MONTH
));
return
calendar
.
getTime
();
return
calendar
.
getTime
();
}
}
/**
/**
* 将秒数转换为时分秒格式
* 将秒数转换为时分秒格式
*
*
* @param times
* @param times
* @return
* @return
*/
*/
public
static
String
secondsToTimeStr
(
int
times
)
{
public
static
String
secondsToTimeStr
(
int
times
)
{
if
(
times
<=
0
)
{
if
(
times
<=
0
)
{
return
"00:00:00"
;
return
"00:00:00"
;
}
}
int
h
=
times
/
3600
;
int
h
=
times
/
3600
;
int
m
=
(
times
-
h
*
3600
)
/
60
;
int
m
=
(
times
-
h
*
3600
)
/
60
;
int
s
=
times
-
h
*
3600
-
m
*
60
;
int
s
=
times
-
h
*
3600
-
m
*
60
;
String
time
=
"%02d:%02d:%02d"
;
String
time
=
"%02d:%02d:%02d"
;
time
=
String
.
format
(
time
,
h
,
m
,
s
);
time
=
String
.
format
(
time
,
h
,
m
,
s
);
return
time
;
return
time
;
}
}
public
static
Date
getTargetEndTime
(
Date
target
)
{
public
static
Date
getTargetEndTime
(
Date
target
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
target
);
calendar
.
setTime
(
target
);
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
23
);
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
23
);
calendar
.
set
(
Calendar
.
MINUTE
,
59
);
calendar
.
set
(
Calendar
.
MINUTE
,
59
);
calendar
.
set
(
Calendar
.
SECOND
,
59
);
calendar
.
set
(
Calendar
.
SECOND
,
59
);
Date
end
=
calendar
.
getTime
();
Date
end
=
calendar
.
getTime
();
return
end
;
return
end
;
}
}
}
}
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