package date_transfor;
import java.text.SimpleDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
* @author henry.xiao
*
*/
public class Date_transfor {
private static Log log = LogFactory.getLog(Date_transfor.class);
public static java.sql.Date string_to_sqlDate(String date) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date dDate = sdf.parse(date.trim());
java.sql.Date sDate = new java.sql.Date(dDate.getYear(), dDate
.getMonth(), dDate.getDate());
log.debug("from String_to_util.Date :" + sDate);
return sDate;
}
public static java.sql.Date uitlDate_to_sqlDate(java.util.Date dDate)
throws Exception {
java.sql.Date sDate = new java.sql.Date(dDate.getYear(), dDate
.getMonth(), dDate.getDate());
log.debug("from util.Date_to_sql.Date :" + sDate);
return sDate;
}
public static String sqlDate_to_String(java.sql.Date sDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date dSt = (java.util.Date) sDate;
String date = sdf.format(dSt);
log.debug("from sql.Date_to_string :" + date);
return date;
}
public static void main(String[] args) throws Exception {
/**
* test from util.Date to sql.Date or string
*/
java.util.Date dDate = new java.util.Date();
java.sql.Date sDate = Date_transfor.uitlDate_to_sqlDate(dDate);
String date = Date_transfor.sqlDate_to_String(sDate);
/**
* test from string to sql.Date
*/
// String date = "2009-03-18";
// java.sql.Date sDate = Date_transfor.string_to_sqlDate(date);
}
}