Edit:: Disregard this post I didn't read the bottom of your post after the code snippet. What I said has nothing to aid you.
Are you wanting to do this via java specific date-time methods, or do you have to do this yourself?
I would write it something along these lines:
Code
hours, minutes = "13:41".split(":")
raise TimeFormatException if hours.to_i > 24 || hours.to_i < 0
raise TimeFormatException if minutes.to_i > 59 || minutes.to_i < 0
if hours.to_i > 12
suffix = "pm"
hours = (hours.to_i - 12).to_s
else
suffix = "am"
end
puts "#{hours}:#{minutes} #{suffix}"
#1:41 pm
Be aware that this assumes the input time is in the correct format and does not include garbage data. More error handling is needed.
I would just use the date-time methods available in Java, why reinvent the wheel.
Edit:: My ruby is rusty and type conversions should of been handled during the split. Oh well.
This post was edited by AbDuCt on Feb 23 2017 06:24pm