Pushpam Kumar
Pushpam Kumar

Reputation: 82

how to parse String dateformat to only time

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

Answers (1)

Jordi Castilla
Jordi Castilla

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

Related Questions