public class Test {
/**
* @param args
*/
public static void main(String[] args) {
String date1 = "2012-11-07 23:59:59";
String format = "yyyy-MM-dd HH:mm:ss";
Date date_1 = formatStrToDate(date1, format);
String date_2 = formatDateToStr(date_1, format);
}
/**
* 转换指定格式的时间为Date
*
* @param dateStr
* @param format
* @return
*/
public static Date formatStrToDate(String dateStr, String format) {
if (dateStr == null || format == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(dateStr, new ParsePosition(0));
}
/**
* 转换指定格式的时间为String
*
* @param date
* @param format
* @return
*/
public static String formatDateToStr(Date date, String format) {
if (date == null || format == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
}
/**
* @param args
*/
public static void main(String[] args) {
String date1 = "2012-11-07 23:59:59";
String format = "yyyy-MM-dd HH:mm:ss";
Date date_1 = formatStrToDate(date1, format);
String date_2 = formatDateToStr(date_1, format);
}
/**
* 转换指定格式的时间为Date
*
* @param dateStr
* @param format
* @return
*/
public static Date formatStrToDate(String dateStr, String format) {
if (dateStr == null || format == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(dateStr, new ParsePosition(0));
}
/**
* 转换指定格式的时间为String
*
* @param date
* @param format
* @return
*/
public static String formatDateToStr(Date date, String format) {
if (date == null || format == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
}