Formatting user input in Java to date -


i having difficulty understanding way change user input of dates an int compared int. trying user input date of birth, compare zodiac signs dates in switch format if possible. going through of post on how change input int, pars, , simpledateformat not able apply it, shown in code when try implement "dateob" should've been formated int, in switch statement did not recognize ...

my code far:

import java.util.*; import java.text.*;  public class signs {  public static void main(string[] args) {      scanner userinput = new scanner (system.in);  // intro message             system.out.println("hello you, lets know each other");  // user input begin //name              string username;              system.out.println("what name ?");                 username = userinput.nextline();  //date of birth               system.out.println(username + " please enter dob (dd/mm/yyy)");                  string dateob = userinput.next();                 simpledateformat dateformat = new simpledateformat("dd/mm/yyyy");                 try {                     date date = new simpledateformat("dd/mm/yyyy").parse(dateob);                     system.out.println(dateob);                 } catch (parseexception e) {                     e.printstacktrace();                     return;                 }              system.out.println("so date of birth " + dateob);  // choosing zodiac sign             //starting switch statement             int converteddate = dateob;             string zodiacsign;          switch (converteddate){          }           }     } 

i appreciate if explain me how implement in simple way ...

so great suggestion guys, , ended right understanding of things, complication implementing minor suggestion make code function right way,

what got far :

boolean correctformat = false;                       do{                  system.out.println(username + " please enter dob (dd/mm/yyy)");                 string dateob = userinput.next();                  try{                  date date = new simpledateformat("dd/mm/yyyy").parse(dateob);                                     system.out.println(dateob);                 system.out.println("so date of birth " + dateob);                 correctformat = true;                  }catch(parseexception e){                  correctformat = false;                 }                 }while(!correctformat); 

so problem facing " dateob since inside while loop, not recognized out side loop, checking if date format right , when try , conver date number : int daynmonth = integer.parseint(new simpledateformat("ddmm").format(dateob));

                calendar cal = calendar.getinstance();                 cal.settime(date);                 int day = cal.get(calendar.day_of_month);                 //int month = cal.get(calendar.month)+ 1; 

it not accepted ? how tackle issue ? enter image description here

hey far i've been having trouble part of code:

calendar cal = calendar.getinstance(); cal.settime(daynmonth); int day = cal.get(calendar.day_of_month); int month = cal.get(calendar.month)+ 1; 

as in eclips wont let me day , month "dateob" code. enter image description here

could try , me understand problem !?

once have converted input date can information using calendar.

calendar cal = calendar.getinstance(); cal.settime(date); int day = cal.get(calendar.day_of_month); int month = cal.get(calendar.month) + 1; 

edit #1:

to number want have additional calculation, i.e.,

int zodiacnumber = month * 100 + day; 

it result in 1105 input 05/11/1984. vice versa can use day * 100 + month if prefer 511.

or can parse date straight format wish using

int zodiac = integer.valueof(new simpledateformat("ddmm").format(date))

which shorter solution.

edit #2: replied 1 question comment want loop make sure date entered in correct format, this:

boolean correctformat = false; {    // ask user input    // read input    try {        // try format input        correctformat = true;    } catch (parseexception e) {        correctformat = false;    } } while (!correctformat); 

you can sure date not entered in correct way if exception thrown , thus, setting helper variable can make sure ask date until convertable , hence, correct.

edit #3: need use date if working simpledateformat not string.

int daynmonth = integer.parseint(new simpledateformat("ddmm").format(dateob)); 

will not work! have use

int daynmonth = integer.parseint(new simpledateformat("ddmm").format(date)); 

so program has this:

date date = new date(); string dateob = ""; boolean correctformat = false; {     (...)     date = new simpledateformat("dd/mm/yyyy").parse(dateob);     (...) } while (...); int daynmonth = integer.parseint(new simpledateformat("ddmm").format(date); (...) cal.settime(date); (...) 

it important know date real date whereas dateob user entered - string cannot used real date!


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -