Last active
August 29, 2017 08:46
-
-
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
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 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