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

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -