Java class to give the long string format of a date? (In words, as if spoken) -
after looking through calendar , date classes, seeems though there no conventional way of turning time word alternative. xample, if time 12:20 pm, converted twelve forty 5 p.m.
is there premade class this? half methods in date class deprecated.
while there no class want, happen have example similar this:
/** * class take integer value, positive or negative, , translate * printed single digit values. * i.e. 142 = 1 4 2 * -12 = negative 1 2 */ public class numbertranslator { final private static string[] txt = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; static int tempnumber; static string finalstring; static int tempnumber1; public static stringbuilder num = new stringbuilder(); public static void main(string[] args) { if(args.length == 0) { system.out.println("you need pass number argument"); return; } numbertranslator nt = new numbertranslator(); system.out.println(nt.translate(args[0])); } public static string translate(int number) { return translate(string.valueof(number)); } public static string translate(string number) { if(!isnumeric(number)) return ""; boolean isnegative = false; stringbuilder num = new stringbuilder(); if(number.charat(0) == '-') { num.append("negative "); isnegative = true; } if(isnegative) { number = number.substring(1,number.length()); (character c : number.tochararray()) { num.append(txt[integer.valueof(c.tostring())]).append(" "); } } else { (character c : number.tochararray()) { num.append(txt[integer.valueof(c.tostring())]).append(" "); } } return num.tostring().trim(); } private static boolean isnumeric(string value) { //ignore negative character if(value.charat(0) == '-') { for(int = 1; < value.length(); i++) { if(character.isdigit(value.charat(i)) == false) return false; } } else { for(int = 0; < value.length(); i++) { if(character.isdigit(value.charat(i)) == false) return false; } } return true; } }
Comments
Post a Comment