Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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