Skip to content

Instantly share code, notes, and snippets.

@frmz
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frmz/0eeac13051e050ce0982 to your computer and use it in GitHub Desktop.
Save frmz/0eeac13051e050ce0982 to your computer and use it in GitHub Desktop.
Time to text
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