fork download
  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) throws Exception {
  4. try (Scanner in = new Scanner(System.in)) {
  5. while (in.hasNext()) {
  6. String[] line = in.nextLine().split(":");
  7. System.out.println(convert(Integer.parseInt(line[0]), Integer.parseInt(line[1])));
  8. }
  9. }
  10. }
  11. public static String convert(int hour, int min) {
  12. boolean am;
  13. if (hour<12) {
  14. am=true;
  15. if (hour==0) hour = 12;
  16. } else {
  17. am=false;
  18. if (hour != 12) hour-=12;
  19. }
  20. if (min == 0) {
  21. if (hour == 12) return String.format("It's %s", am?"midnight":"noon");
  22. else return String.format("It's %s o'clock %s", intToText(hour), am?"am":"pm");
  23. }
  24. else if (min == 30) return String.format("It's half past %s %s", intToText(hour), am?"am":"pm");
  25. else if (min == 15) return String.format("It's quarter past %s %s", intToText(hour), am?"am":"pm");
  26. else if (min == 45) return String.format("It's quarter til %s %s", intToText((hour==12)?1:hour+1), ((hour==11)?!am:am)?"am":"pm");
  27. else return String.format("It's %s %s %s", intToText(hour), (((min<10 && min>0)?("oh "):("")) + intToText(min)),am?"am":"pm");
  28. }
  29. private static String intToText(int number) {
  30. if (number < 20) return onesNames[number];
  31. else {
  32. int tens = number/10;
  33. int ones = number%10;
  34. if (ones==0) return tensNames[tens];
  35. else return tensNames[tens] + " " + onesNames[ones];
  36. }
  37. }
  38. private static final String[] tensNames = { "", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
  39. private static final String[] onesNames = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
  40. }
Success #stdin #stdout 0.05s 4386816KB
stdin
00:00
12:00
01:00
16:15
13:30
10:45
23:45
12:45
01:30
12:05
14:01
20:29
21:00
stdout
It's midnight
It's noon
It's one o'clock am
It's quarter past four pm
It's half past one pm
It's quarter til eleven am
It's quarter til twelve am
It's quarter til one pm
It's half past one am
It's twelve oh five pm
It's two oh one pm
It's eight twenty nine pm
It's nine o'clock pm