Possible Duplicate:
String to Date in Different Format in Java
Hi,
I m receiving date as String data type in MM/dd/yyyy format and need to convert in data Datatype in format yyyy-MM-dd to store in DB.
Possible Duplicate:
String to Date in Different Format in Java
Hi,
I m receiving date as String data type in MM/dd/yyyy format and need to convert in data Datatype in format yyyy-MM-dd to store in DB.
You can use SimpleDateFormat:
SimpleDateFormat sourceFormat = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat destinationFormat = new SimpleDateFormat("yyyy-MM-dd");
String result = sourceFormat.format(destinationFormat.parse(input));
Use formatter for parsing the date to particular type
String date="07/25/2011";
SimpleDateFormat dateFormatter=new SimpleDateFormat("yyyy-MM-dd");
try {
Date d=dateFormatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
You can also do it like this
String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
//Instance of the calender class in the utill package
Calendar cal = Calendar.getInstance();
//A class that was used to get the date time stamp
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
To print out the time you say
System.out.println(sdf.format(cal.getTime()) );
Cheers