Udayakumar Udayakumar - 5 months ago 37
Scala Question

How to change 12 Hours to 24 Hours using Scala

I want 12 Hours Time format convert into 24 Hours time format, Here I attached my code and I checked this link1,link2 but it return as same time format.


val inTime = "12:15 PM"
val newTimeFormat = new SimpleDateFormat("hh:mm a")
val timeWithDateFormat = newTimeFormat.parse(inTime)
val outputTime = newTimeFormat.format(timeWithDateFormat)

Output is:

(Output===========>,12:15 PM)

How can I resolved it.


As you want your output to be in a different format than your input, you will need to use a different formatters for input and output.

Also... 12:15 PM of 12-hour-format is 12:15 of 24-hour-format. So may be you should use a different time for this example (lets use 03:15 PM or 15:15),

val inTime = "03:15 PM"

val inputTimeFormat = new SimpleDateFormat("hh:mm a")

val timeWithDateFormat = inputTimeFormat.parse(inTime)

val outputTimeFormat = new SimpleDateFormat("HH:mm")

val outputTime = outputTimeFormat.format(timeWithDateFormat)

println("Output===========>", outputTime)