fork download
  1. import java.io.*;
  2.  
  3. public class Main{
  4.  
  5. public static void main(String[] args) {
  6. testcases();
  7. }
  8.  
  9. public static void converttime(int hours, int minutes) {
  10. String[] singles = {"twelve ", "one ", "two ", "three ", "four ", "five " , "six ", "seven ", "eight ", "nine ", "ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "};
  11. String[] tens = {"", "oh " , "twenty ", "thirty ", "fourty ", "fifty "};
  12. int t_tens = ((minutes +9 )/10);
  13. String min_rdigit = (minutes % 10 == 0 && minutes != 10 ) ? "" : singles[minutes%20];
  14. String min_ldigit = (t_tens == 1 ) ? tens[t_tens] : tens[(minutes/10)];
  15. String am_pm = (hours > 12) ? "pm" : "am";
  16.  
  17.  
  18. System.out.println("It's " + singles[(hours % 12)] + min_ldigit + min_rdigit + am_pm);
  19.  
  20. }
  21.  
  22. public static void testcases() {
  23. String [] test_values = { "00:00", "01:30", "12:05", "14:01", "20:29", "21:00"};
  24. for(String value : test_values) {
  25. String[] time = value.split(":");
  26. int i_hour = Integer.parseInt(time[0]);
  27. int i_minute = Integer.parseInt(time[1]);
  28. converttime(i_hour, i_minute);
  29. }
  30.  
  31. }
  32. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
It's twelve am
It's one thirty am
It's twelve oh five am
It's two oh one pm
It's eight twenty nine pm
It's nine pm