java - check if a string inserted by user input corresponds to a particular format -
i have method ask user input(a date) in specific format -- dd/mm/yyyy , managed partially limit user input way using code
date.matches("[0-9]+[0-9]/[0-9]+[0-9]/[0-9]+[0-9]+[0-9]+[0-9]"));
but still got problem on input because if user insert date 12121/12121212/12121 takes input , goes forward
the input cicle determined portion of code using joptionpane
string date = joptionpane.showinputdialog(frame,"inserisci la data di partenza separata da / (gg/mm/aaaa):"); if (date ==null) { return;} while (!date.matches("[0-9]+[0-9]/[0-9]+[0-9]/[0-9]+[0-9]+[0-9]+[0-9]")) { joptionpane.showmessagedialog(frame, "data inserita errata"); date = joptionpane.showinputdialog(frame,"inserisci la data di partenza separata da / (gg/mm/aaaa):"); }
then split string using
string[] parts = date.split("/"); int year = integer.parseint(parts[2]); int month = integer.parseint(parts[1]); int day = integer.parseint(parts[0]);
how can work limiting numbers of digits of inserted string ?
you should try parse date of given format , catch parse errors. take @ simpledateformat
Comments
Post a Comment