Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Last active August 29, 2017 08:46
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 ssaurel/44f1983a937009ae21068665a8b41f1f to your computer and use it in GitHub Desktop.
Save ssaurel/44f1983a937009ae21068665a8b41f1f to your computer and use it in GitHub Desktop.
Time to English Words Algorithm for a tutorial on the SSaurel's Blog
public static String words[] = { "", "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 currentTimeInWords() {
Date d = new Date();
Calendar c = Calendar.getInstance();
c.setTime(d);
int h = c.get(Calendar.HOUR);
int m = c.get(Calendar.MINUTE);
String plu, a;
if (m == 1 || m == 59)
plu = "Minute";
else
plu = "Minutes";
if (h == 12)
a = words[1]; // storing 'one' when hour is 12
else
a = words[h + 1]; // if hour is not 12, then storing in words,
if (m == 0)
return words[h] + " O' clock";
else if (m == 15)
return "Quarter past " + words[h];
else if (m == 30)
return "Half past " + words[h];
else if (m == 45)
return "Quarter to " + a;
else if (m < 30) // condition for minutes between 1-29
return words[m] + " " + plu + " past " + words[h];
else
// condition for minutes between 31-59
return words[60 - m] + " " + plu + " to " + a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment