date - Java - Better way then using Ifs and repeating functions - DateRelated -
i created function check date string month contains can use multiple date formats. i'm using lot of ifs , repeating .contains function. there better way of doing this?
also related dates in format weekday(string), month(string) day(int- xx), year(int - xxxx). e.g. sunday, june 27, 1965. i've had no luck far built in functions.
public static int checkmonth(string s){ string month = s.tolowercase(); if(month.contains("january")){return 1;} else if(month.contains("febuary")){return 2;} else if(month.contains("march")){return 3;} else if(month.contains("april")){return 4;} else if(month.contains("may")){return 5;} else if(month.contains("june")){return 6;} else if(month.contains("july")){return 7;} else if(month.contains("august")){return 8;} else if(month.contains("september")){return 9;} else if(month.contains("october")){return 10;} else if(month.contains("november")){return 11;} else if(month.contains("december")){return 12;} else {return -1;} }
you could use loop. example,
// 'lookup' static string[] monthnames = {"january", .., "december"}; // in method (var = 0; < months.length; i++) { if (monthname.contains(monthnames[i])) { return + 1; // because 0..11 indices in array } } // if no matching month found
although if trying "nlp" date, i'm sure there existing libraries ..
Comments
Post a Comment