/**
* 将指定格式的字符串转为Date
* @param sDate
* @param format
* 遵循 simpleDateFormat 规范
* @return
*/
public static Date stringToDate(String sDate, String format) {
DateFormat df = new SimpleDateFormat(format);
try {
return df.parse(sDate);
} catch (ParseException e) {
throw new RuntimeException("date format error");
}
}
/**
* 把一个Date 按照指定格式转换为String
*
* @param date
* @return
*/
public static String dateToString(Date date, String format) {
DateFormat df = new SimpleDateFormat(format);
return df.format(date);
}
/**
* 计算时间差值
* @param beginDate
* @param endDate
* @return
*/
public static int computeDate(Date beginDate,Date endDate){
Calendar calBegin=Calendar.getInstance();
Calendar calEnd=Calendar.getInstance();
calBegin.setTime(beginDate);
calEnd.setTime(endDate);
return (int)(calEnd.getTimeInMillis()-calBegin.getTimeInMillis())/1000/60/60/24;
}
/**
* 计算一日期加上X天之后的日期
* @param date
* @param inteval
* @return
*/
public static String resetDay(String date,int inteval){
String dateTimeFrom = "";
try {
Calendar calendar = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
calendar.clear();
calendar.set(Integer.parseInt(date.substring(0,4)), Integer.parseInt(date.substring(5,7)) - 1, Integer
.parseInt(date.substring(8, 10)));
calendar.add(Calendar.DAY_OF_MONTH, inteval);
dateTimeFrom = sdf.format(calendar.getTime());
return dateTimeFrom;
} catch (Exception e){
e.getMessage();
return "";
}
}
/**
* 根据日期获得对应的星期几
* @param argDate
* @return
*/
public static String getDayOfWeek(String argDate){
if (argDate.length() != 10){
throw new IllegalArgumentException("argDate length != 8");
}
int intArgY = Integer.parseInt(argDate.substring(0, 4));
int intArgM = Integer.parseInt(argDate.substring(5, 7));
int intArgD = Integer.parseInt(argDate.substring(8, 10));
Calendar calendar = GregorianCalendar.getInstance();
calendar.set(intArgY, intArgM - 1, intArgD);
int returnDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
String showDate = "";
switch (returnDayOfWeek){
case 1:
showDate = "日";
break;
case 2:
showDate = "一";
break;
case 3:
showDate = "二";
break;
case 4:
showDate = "三";
break;
case 5:
showDate = "四";
break;
case 6:
showDate = "五";
break;
default:
showDate = "六";
break;
}
return showDate;
}