The working code is here.
You can use the code below to convert from String to Date
String myStrDate = "11/08/2013 08:48:10";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
Date date = format.parse(myStrDate);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
To the contrary means that to convert from Date to String
SimpleDateFormat myFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
Date date = new Date();
String datetime = myFormat.format(date);
System.out.println("Current Date Time in give format: " + datetime);
} catch (ParseException e) {
e.printStackTrace();
}
Additional information on date and time formatting. Visit website
source
share