1

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.

Code

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

Output is:

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

How can I resolved it.

Community
  • 1
  • 1
Udayakumar
  • 307
  • 5
  • 14
  • As you want your output to be in adifferent format thab your input, when you are creating the `outputTime` String, use a different `DateTimeFormat`, `val outputDateTimeFormat = new SimpleDateFormat("HH:mm")`. Also please learn to read the documentation, as it is clearly explained here - https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – sarveshseri Nov 28 '16 at 07:40

2 Answers2

3

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)
sarveshseri
  • 13,738
  • 28
  • 47
0

Just create new SimpleDateFormat and use it to format your date;

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

For details you can check documentation;

Customizing Formats

Fatih Donmez
  • 4,319
  • 3
  • 33
  • 45