How to Convert MM/DD/YYYY format Date to DB Format which is YYYY-MM-DD HH:MM:SS using Java
//Required Imports import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Date; //Define outputDate variable Timestamp outputDate = null;
//input datestring in MM/DD/YYYY format String inputDate = "05/16/2013";try {//create a simpledateformat object to read the input date
SimpleDateFormat fromDate = new SimpleDateFormat("MM/dd/yyyy");//create one more simpledateformat object to write output date
SimpleDateFormat toDBFormat = new SimpleDateFormat("yyyy-MM-dd");//Parse Input Date to Output format as a String
String reformattedStr = toDBFormat.format(fromDate.parse(inputDate));//Now Convert the string to Date
Date parsedDate = toDBFormat.parse(reformattedStr);//Now finally convert the Date to Timestamp format which is equivalent to MySQL Format
transactionDate = new java.sql.Timestamp(parsedDate.getTime());
}catch(Exception e) {
e.printStackTrace();
}
No comments:
Post a Comment