总结了一下日期转换常用的几种,看源码:
private static final Logger logger = Logger.getLogger(DateUtil.class);
/**
* 日期转换成字符串格式
* @param date
* @param pattern
* @return
*/
public String dateToString(Date date, String pattern){
SimpleDateFormat sf = new SimpleDateFormat(pattern);
return sf.format(date);
}
/**
* 字符串格式转换成日期格式
* @param dateString
* @param pattern
* @return
*/
public Date stringToDate(String dateString, String pattern){
SimpleDateFormat sf = new SimpleDateFormat(pattern);
Date date = null;
try {
date = sf.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 获得两个日期之间的所有日期
* @param startDate
* @param endDate
* @return
*/
public static List getDateStringListByBetweenTwoDates(String beginDate,String endDate){
List dateList = new ArrayList();
int maxRound = 366;//hardcode
int round = 0;
String nextDate = beginDate;
dateList.add(nextDate);
while ((nextDate != null && !nextDate.equals(endDate)) && round++ < maxRound){
nextDate = DateStringAdd(nextDate, 1);
dateList.add(nextDate);
}
return dateList;
}
/**
* 获得日期的上个或下个日期
* @param dateString
* @param add
* @return
*/
public static String DateStringAdd(String dateString, int add){
String sret = "";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
if (dateString == null)
return null;
if (add == 0)
return dateString;
if (!dateString.startsWith("1") && !dateString.startsWith("2"))
return null;
if(add <0)
return null;
try {
Date date = sf.parse(dateString);
int year = date.getYear()+1900;
int month = date.getMonth()+1;
int day = date.getDate();
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
if(day+add>31){
month++;
day=1;
}else{
day=day+add;
}
break;
case 12:
if(month==12 && day+add>31){
year++;
month=1;
day=1;
}else{
day=day+add;
}
break;
case 4:
case 6:
case 9:
case 11:
if(day+add>30){
month++;
day=1;
}else{
day=day+add;
}
break;
case 2:
if(year%400==0||(year%4==0 && year%100!=0)){
if(day+add>29){
month++;
day=1;
}else{
day=day+add;
}
}else{
if(day+add>28){
month++;
day=1;
}else{
day=day+add;
}
}
break;
}
if(month<10){
sret = year+"-0"+month;
}else{
sret = year+"-"+month;
}
if(day<10){
sret = sret+"-0"+day;
}else{
sret = sret+"-"+day;
}
} catch (ParseException e) {
e.printStackTrace();
}
return sret;
}
/**
* 日期字符串转换成Timestamp类型
* @param dateString
* @return
*/
public static Timestamp dateStringConvertToTimestamp(String dateString, String pattern){
dateString = dateString.replaceAll(" ", " ");
SimpleDateFormat sf = new SimpleDateFormat(pattern);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timestampString = null;
try {
Date date = sf.parse(dateString);
timestampString = sdf.format(date);
} catch (ParseException e) {
logger.info("格式转换错误...");
e.printStackTrace();
}
Timestamp timestamp = Timestamp.valueOf(timestampString);
return timestamp;
}
//当前时间加间小时分钟
public static String currentTimeAddMinute(Integer minute){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
// Date now = new Date();
Calendar ca = Calendar.getInstance();
//加小时
ca.add(Calendar.HOUR_OF_DAY, minute);
// ca.add(Calendar.DAY_OF_YEAR, minute);
//加分钟
// ca.add(Calendar.MINUTE, minute);
System.out.println("当前时间加5分钟的时间:"+sdf.format(ca.getTime()));
return sdf.format(ca.getTime());
}
//比较两个时间的大小
public boolean compareDate(String beginDate,String endDate) throws ParseException{
beginDate="2015-03-21 12:23:20";
endDate="2015-03-21 12:23:20";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date nowBegin =sdf.parse(beginDate);
Date nowEnd=sdf.parse(endDate);
if(nowBegin.getTime()
return true;
}else{
return false;
}
}