字符串转换成日期格式:
/**
* 字符串转换到日期格式
* @param dateStr 需要转换的字符串
* @param formatStr 需要格式的目标字符串 举例 yyyyMMdd
* @return Date 返回转换后的时间
* @throws ParseException 转换异常
*/
public static java.sql.Date StringToDate(String dateStr,String formatStr){
DateFormat sdf=new SimpleDateFormat(formatStr);
java.sql.Date date=null;
try {
date = new java.sql.Date(sdf.parse(dateStr).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
字符串转换成时间格式:
/**
* 字符串转换到时间格式
* @param dateStr 需要转换的字符串
* @param formatStr 需要格式的目标字符串 举例HH:mm:ss
* @return Date 返回转换后的时间
* @throws ParseException 转换异常
*/
public static Time StringToTime(String dateStr,String formatStr){
DateFormat sdf=new SimpleDateFormat(formatStr);
Time time=null;
try {
time = new Time(sdf.parse(dateStr).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}