工具类:
package
com.power.date;
import
java.text.ParseException;
import
java.text.SimpleDateFormat;
import
java.util.Calendar;
import
java.util.Date;
public
class
DateUtils {
/**
* 格式化日期
*
@param
date
*
@param
pattern
*
@return
*/
public
static
String format(Date date,String pattern){
SimpleDateFormat format
=
new
SimpleDateFormat(pattern);
return
format.format(date);
}
/**
* 根据字符串生成日期
*
@param
dateStr
*
@param
pattern
*
@return
*
@throws
ParseException
*/
public
static
Date parse(String dateStr,String pattern)
throws
ParseException{
SimpleDateFormat format
=
new
SimpleDateFormat(pattern);
return
format.parse(dateStr);
}
/**
* 比较日期前后
*
@param
date
*
@param
when
*
@return
date在when之前,返回false,否则返回true
*/
public
static
boolean
after(Date date,Date when){
return
date.after(when);
}
/**
* 添加(或减小)时间
*
@param
date
*
@param
field 要添加(或减小)的字段(年或月或日或)
*
@param
amount 要添加(或减小)的数量,amount为正数时,是添加,为负数时是减小
*
@return
日期
*/
public
static
Date add(Date date,
int
field,
int
amount){
Calendar ca
=
Calendar.getInstance();
ca.setTime(date);
ca.add(field, amount);
return
ca.getTime();
}
/**
* 添加(或减小)时间
*
@param
date
*
@param
field 要添加(或减小)的字段(年或月或日或)
*
@param
amount 要添加(或减小)的数量,amount为正数时,是添加,为负数时是减小
*
@param
pattern 格式化模式
*
@return
格式化后的日期字符串
*/
public
static
String add(Date date,
int
field,
int
amount,String pattern){
Calendar ca
=
Calendar.getInstance();
ca.setTime(date);
ca.add(field, amount);
return
format(ca.getTime(),pattern);
}
public
class
DatePattern{
public
static
final
String Y_M_D
=
"
yyyy-MM-dd
"
;
public
static
final
String Y_M_D_H_M_S
=
"
yyyy-MM-dd HH:mm:ss
"
;
}
}
测试类:
/**
*
*/
package
com.power.date;
import
java.text.ParseException;
import
java.util.Calendar;
import
java.util.Date;
import
org.junit.Test;
import
static
junit.framework.Assert.
*
;
/**
*
@author
liwei
*
*/
public
class
TestDateUtils {
@Test
public
void
testFormat(){
Date now
=
new
Date();
System.out.println(DateUtils.format(now, DateUtils.DatePattern.Y_M_D));
System.out.println(DateUtils.format(now, DateUtils.DatePattern.Y_M_D_H_M_S));
}
@Test
public
void
testParse()
throws
ParseException{
String dateStr
=
"
2009-06-09
"
;
Date date
=
DateUtils.parse(dateStr, DateUtils.DatePattern.Y_M_D);
System.out.println(DateUtils.format(date, DateUtils.DatePattern.Y_M_D));
dateStr
=
"
2009-06-09 10:14:57
"
;
Date date2
=
DateUtils.parse(dateStr, DateUtils.DatePattern.Y_M_D_H_M_S);
System.out.println(DateUtils.format(date2, DateUtils.DatePattern.Y_M_D_H_M_S));
}
@Test
public
void
testAfter()
throws
ParseException{
String dateStr
=
"
2009-06-09
"
;
Date date
=
DateUtils.parse(dateStr, DateUtils.DatePattern.Y_M_D);
Date now
=
new
Date();
assertTrue(DateUtils.after(now, date));
}
@Test
public
void
testAdd()
throws
ParseException{
String dateStr
=
"
2009-06-09
"
;
Date date
=
DateUtils.parse(dateStr, DateUtils.DatePattern.Y_M_D);
Date date2
=
DateUtils.add(date, Calendar.MONDAY,
6
);
assertEquals(
"
2009-12-09
"
, DateUtils.format(date2, DateUtils.DatePattern.Y_M_D));
assertEquals(
"
2009-12-09
"
, DateUtils.add(date,Calendar.MONDAY,
6
,DateUtils.DatePattern.Y_M_D));
date2
=
DateUtils.add(date, Calendar.MONTH,
-
2
);
assertEquals(
"
2009-04-09
"
, DateUtils.format(date2, DateUtils.DatePattern.Y_M_D));
assertEquals(
"
2009-04-09
"
, DateUtils.add(date,Calendar.MONDAY,
-
2
,DateUtils.DatePattern.Y_M_D));
}
}
|