您的位置:首页 > 编程语言 > Java开发

DateUtils.java 日期处理相关工具类

2013-12-23 02:01 429 查看
ackage
com.bdvcd.utils;


002
003
import
java.text.ParseException;
004
import
java.text.SimpleDateFormat;
005
import
java.util.Date;
006
import
java.util.TimeZone;
007
008
/**
009
*文件名:DateUtils.java日期处理相关工具类
010
*版本信息:V1.0
011
*日期:2013-03-11
012
*CopyrightBDVCDCorporation2013
'target='_blank'>http://www.bdvcd.com[/code]
013
*版权所有
014
*/
015
public
class

DateUtils{
016
017
/**定义常量**/
018
public

static
final

StringDATE_JFP_STR=
"yyyyMM"
;
019
public

static
final

StringDATE_FULL_STR=
"yyyy-MM-ddHH:mm:ss"
;
020
public

static
final

StringDATE_SMALL_STR=
"yyyy-MM-dd"
;
021
public

static
final

StringDATE_KEY_STR=
"yyMMddHHmmss"
;
022
023
/**
024
*使用预设格式提取字符串日期
025
*@paramstrDate日期字符串
026
*@return
027
*/
028
public

static
Dateparse(StringstrDate){
029
return

parse(strDate,DATE_FULL_STR);
030
}
031
032
/**
033
*使用用户格式提取字符串日期
034
*@paramstrDate日期字符串
035
*@parampattern日期格式
036
*@return
037
*/
038
public

static
Dateparse(StringstrDate,Stringpattern){
039
SimpleDateFormatdf=
new
SimpleDateFormat(pattern);
040
try

{
041
return

df.parse(strDate);
042
}

catch
(ParseExceptione){
043
e.printStackTrace();
044
return

null
;
045
}
046
}
047
048
/**
049
*两个时间比较
050
*@paramdate
051
*@return
052
*/
053
public

static
int
compareDateWithNow(Datedate1){
054
Datedate2=
new
Date();
055
int

rnum=date1.compareTo(date2);
056
return

rnum;
057
}
058
059
/**
060
*两个时间比较(时间戳比较)
061
*@paramdate
062
*@return
063
*/
064
public

static
int
compareDateWithNow(
long
date1){
065
long

date2=dateToUnixTimestamp();
066
if
(date1>date2){
067
return

1
;
068
}
else

if
(date1<date2){
069
return

-
1
;
070
}
else
{
071
return

0
;
072
}
073
}
074
075
076
/**
077
*获取系统当前时间
078
*@return
079
*/
080
public

static
StringgetNowTime(){
081
SimpleDateFormatdf=
new
SimpleDateFormat(DATE_FULL_STR);
082
return

df.format(
new
Date());
083
}
084
085
/**
086
*获取系统当前时间
087
*@return
088
*/
089
public

static
StringgetNowTime(Stringtype){
090
SimpleDateFormatdf=
new
SimpleDateFormat(type);
091
return

df.format(
new
Date());
092
}
093
094
/**
095
*获取系统当前计费期
096
*@return
097
*/
098
public

static
StringgetJFPTime(){
099
SimpleDateFormatdf=
new
SimpleDateFormat(DATE_JFP_STR);
100
return

df.format(
new
Date());
101
}
102
103
/**
104
*将指定的日期转换成Unix时间戳
105
*@paramStringdate需要转换的日期yyyy-MM-ddHH:mm:ss
106
*@returnlong时间戳
107
*/
108
public

static
long

dateToUnixTimestamp(Stringdate){
109
long

timestamp=
0
;
110
try

{
111
timestamp=
new
SimpleDateFormat(DATE_FULL_STR).parse(date).getTime();
112
}

catch
(ParseExceptione){
113
e.printStackTrace();
114
}
115
return

timestamp;
116
}
117
118
/**
119
*将指定的日期转换成Unix时间戳
120
*@paramStringdate需要转换的日期yyyy-MM-dd
121
*@returnlong时间戳
122
*/
123
public

static
long

dateToUnixTimestamp(Stringdate,StringdateFormat){
124
long

timestamp=
0
;
125
try

{
126
timestamp=
new
SimpleDateFormat(dateFormat).parse(date).getTime();
127
}

catch
(ParseExceptione){
128
e.printStackTrace();
129
}
130
return

timestamp;
131
}
132
133
/**
134
*将当前日期转换成Unix时间戳
135
*@returnlong时间戳
136
*/
137
public

static
long

dateToUnixTimestamp(){
138
long

timestamp=
new
Date().getTime();
139
return

timestamp;
140
}
141
142
143
/**
144
*将Unix时间戳转换成日期
145
*@paramlongtimestamp时间戳
146
*@returnString日期字符串
147
*/
148
public

static
StringunixTimestampToDate(
long

timestamp){
149
SimpleDateFormatsd=
new
SimpleDateFormat(DATE_FULL_STR);
150
sd.setTimeZone(TimeZone.getTimeZone(
"GMT+8"
));
151
return

sd.format(
new
Date(timestamp));
152
}
153
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: