Date屬於java.util.Date包下,表示時間對象
新建Date對象:Date date = new Date();
常用方法:
- 獲得年份:date.getYear() 注意年份要加上1900,這樣才是日期對象所代表的年份
- 獲取月份:date.getMonth() 注意月份要加1,這樣才是日期對象所代表的月份
- 獲取日期:date.getDate()
- 獲取小時:date..getHours() 不設置默認為0
- 獲取分鐘:date.getMinutes()
- 獲取秒:date.getSeconds()
- 獲取毫秒:date.getTime()
- 獲取星期:date.getDay() 注意:0代表星期日、1代表星期1、2代表星期2,其他的一次類推了
工具類常用方法:
<code>public
static
final
String FULL_DATE_FORMAT ="yyyy-MM-dd HH:mm:ss"
;public
static
final
String MON_DATE_FORMAT ="yyyy-MM"
;public
static
final
String FULL_DATE_FORMAT_CN ="yyyy年MM月dd日 HH時mm分ss秒"
;public
static
final
String PART_DATE_FORMAT ="yyyy-MM-dd"
;public
static
final
String PART_DATE_FORMAT_CN ="yyyy年MM月dd日"
;public
static
final
String YEAR_DATE_FORMAT ="yyyy"
;public
static
final
String MONTH_DATE_FORMAT ="MM"
;public
static
final
String DAY_DATE_FORMAT ="dd"
;public
static
final
String WEEK_DATE_FORMAT ="week"
;public
static
final
String FULL_DATE_FORMAT_JC ="yyyyMMddHHmmss"
;/<code>
1、將日期類型轉換為字符串
<code>public
static
String
getFormatDate(Date
date,String
xFormat){ date = date ==null
?new
Date
() : date; xFormat = StringUtils.isNotEmpty(xFormat) ==true
? xFormat : FULL_DATE_FORMAT; SimpleDateFormat sdf =new
SimpleDateFormat(xFormat);return
sdf.format(date); }/<code>
2、將日期字符串轉換為日期格式類型
<code>public
static
Date
parseString2Date(String
xDate,String
xFormat) {while
(!isNotDate(xDate)){ xFormat = StringUtils.isNotEmpty(xFormat) ==true
? xFormat : PART_DATE_FORMAT; SimpleDateFormat sdf =new
SimpleDateFormat(xFormat);Date
date =null
;try
{ date = sdf.parse(xDate); }catch
(ParseException e) { e.printStackTrace();return
null
; }return
date; }return
null
; }/<code>
3、比較日期大小:x < y return [-1]; x = y return [0] ; x > y return [1] ;
<code>public
static
int
compareDate
(Date dateX,Date dateY)
{return
dateX.compareTo(dateY); }/<code>
4、判斷需要轉換類型的日期字符串是否符合格式要求
<code>public
static
boolean
isNotDate
(String xDate)
{ SimpleDateFormat sdf =new
SimpleDateFormat(PART_DATE_FORMAT);try
{if
(StringUtils.isEmpty(xDate)){return
true
; } sdf.parse(xDate);return
false
; }catch
(ParseException e) { e.printStackTrace();return
true
; } }/<code>
5、獲取倆個日期之間相差天數
<code>public
static
int
getDiffDays
(Date dateX,Date dateY
){if
((dateX ==null
) || (dateY ==null
)){return
0
; }int
dayX = (int
)(dateX.getTime() / (60
*60
*1000
*24
));int
dayY = (int
)(dateY.getTime() / (60
*60
*1000
*24
));return
dayX > dayY ? dayX - dayY : dayY - dayX; }/<code>
6、獲取倆個日期之間相差
<code>public
static
StringgetDiffNoABS
(Date dateX,Date dateY
){if
((dateX ==null
) || (dateY ==null
)){return
""
; }long
nd =1000
*24
*60
*60
;long
nh =1000
*60
*60
;long
nm =1000
*60
;long
c = dateX.getTime() - dateY.getTime();long
day = c / nd;long
hour = c % nd / nh;long
min = c % nd % nh / nm; String date =""
;if
(day !=0
) { date += day +"天"
; }if
(hour !=0
) { date += hour +"小時"
; }if
(min !=0
) { date += min +"分鐘"
; }return
date; }/<code>
7、獲取星期字符串
<code>public
static
StringgetWeekString
(Object xDate)
{int
week = getDateTimeParam(xDate,WEEK_DATE_FORMAT);switch
(week) {case
1
:return
"星期一"
;case
2
:return
"星期二"
;case
3
:return
"星期三"
;case
4
:return
"星期四"
;case
5
:return
"星期五"
;case
6
:return
"星期六"
;case
7
:return
"星期日"
;default
:return
""
; } }/<code>
8、獲取某天開始時間
<code>public
static
DategetStartTime
(Date date
){ Calendar calendar = Calendar.getInstance(); date=date==null
?new
Date() : date; calendar.setTime(date); calendar.set
(Calendar.HOUR_OF_DAY,0
); calendar.set
(Calendar.MINUTE,0
); calendar.set
(Calendar.SECOND,0
); calendar.set
(Calendar.MILLISECOND,0
);return
calendar.getTime(); }/<code>
9、獲取某天結束時間
<code>public
static
DategetEndTime
(Date date
){ Calendar calendar = Calendar.getInstance(); date=date==null
?new
Date() : date; calendar.setTime(date); calendar.set
(Calendar.HOUR_OF_DAY,23
); calendar.set
(Calendar.MINUTE,59
); calendar.set
(Calendar.SECOND,59
); calendar.set
(Calendar.MILLISECOND,999
);return
calendar.getTime(); }/<code>
10、獲取本月第一天
<code>public
static
StringgetMonthFrist
(Date time,String xFormat)
{ SimpleDateFormat format =new
SimpleDateFormat(xFormat); Calendar ca = Calendar.getInstance(); ca.setTime(time); ca.set
(Calendar.DAY_OF_MONTH,1
); ca.set
(Calendar.HOUR_OF_DAY,0
); ca.set
(Calendar.MINUTE,0
); ca.set
(Calendar.SECOND,0
);return
format.format(ca.getTime()); }/<code>
11、獲取本月最後一天
<code>public
static
String
getFormatDate(Date
date,String
xFormat){ date = date ==null
?new
Date
() : date; xFormat = StringUtils.isNotEmpty(xFormat) ==true
? xFormat : FULL_DATE_FORMAT; SimpleDateFormat sdf =new
SimpleDateFormat(xFormat);return
sdf.format(date); }/<code>
12、把2019-4轉換成2019-04
<code>public
static
Date
parseString2Date(String
xDate,String
xFormat) {while
(!isNotDate(xDate)){ xFormat = StringUtils.isNotEmpty(xFormat) ==true
? xFormat : PART_DATE_FORMAT; SimpleDateFormat sdf =new
SimpleDateFormat(xFormat);Date
date =null
;try
{ date = sdf.parse(xDate); }catch
(ParseException e) { e.printStackTrace();return
null
; }return
date; }return
null
; }/<code>
13、獲取本月有多少天
<code>public
static
int
compareDate
(Date dateX,Date dateY)
{return
dateX.compareTo(dateY); }/<code>
14、獲取當前時間是一年當中的第幾周(年的第一個週一為第一週)
<code>public
static
int
getDayIsWeek
(Date date
) {int
isWeek =0
; Calendar calendar = Calendar.getInstance(); calendar.setFirstDayOfWeek(Calendar.MONDAY); calendar.setTime(date); isWeek = calendar.get
(Calendar.WEEK_OF_YEAR);int
mouth = calendar.get
(Calendar.MONTH);if
(mouth >=11
&& isWeek <=1
) { isWeek +=52
; } calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set
(Calendar.MONTH,0
); calendar.set
(Calendar.DAY_OF_MONTH,1
);int
week = getDateTimeParam(calendar.getTime(),"week"
);if
(week !=1
) { isWeek = isWeek -1
; }return
isWeek; }/<code>
好好學習,天天搬磚,請點擊關注學習更多java小知識