Reputation: 82
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:ms");
Calendar cal = Calendar.getInstance();
Date finalTime=dateFormat1.parse(dateFormat.format(cal1.getTime()));
System.out.println(finalTime1);
Actual output
Thu Jan 01 17:01:37 IST 1970
Expected Output
17:01:37
Upvotes: 0
Views: 738
Reputation: 26981
Your pattern in SimpleDateFormat
is not correct... Also note you are not printing the String
with the date formatted, instead of this you are printing the Date
object, so you will see always this format because you call Date.toString()
method implicit when you call System.out.println(finalTime1);
.
Use:
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Calendar cal = Calendar.getInstance();
String dateFormatted = dateFormat.format(cal.getTime())
System.out.println(dateFormatted);
And you will get correct output...
Upvotes: 1