Last active
August 29, 2015 14:16
-
-
Save frmz/0eeac13051e050ce0982 to your computer and use it in GitHub Desktop.
Time to text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TimeToText { | |
private final static String sTimeWords[] = {"", "One", "Two", "Three", "Four", "Five", "Six", | |
"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", | |
"Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty one", "Twenty two", | |
"Twenty three", "Twenty four", "Twenty five", "Twenty six", "Twenty seven", | |
"Twenty eight", "Twenty nine"}; | |
public static String timeToTextEnglish(int h, int m) { | |
if (m == 0) return sTimeWords[h] + " o'clock"; | |
if (m == 15) return "quarter past " + sTimeWords[h]; | |
if (m == 30) return "half past " + sTimeWords[h]; | |
if (m == 45) return "quarter to " + (h == 12 ? sTimeWords[1] : sTimeWords[h + 1]); | |
if (m < 30) return sTimeWords[m] + " past " + sTimeWords[h]; | |
else return sTimeWords[60 - m] + " to " + (h == 12 ? sTimeWords[1] : sTimeWords[h + 1]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment