string - How to write Java code to take any date user inputs, and list the next 40 days? -
i trying ensure nextday method can add 1 day date user entered , ensure adds 40 days correctly, taking account correct days per month , leap years
public nextday() { (int count = 1; count < 40; count++) ; } public string todaydatestring() // todaydatestring method { int countdays = 0; (int = 1; < getmonth(); i++) { if (i == 2 && checkleapyr(getyear())) countdays += 29; else countdays += dayspermonth[i]; } countdays += date; string message = string.format("\n", countdays, getyear()); return message; } private boolean checkleapyr(int inyear) { // check leap year method. if (getyear() % 400 == 0 || (getyear() % 4 == 0 && getyear() % 100 != 0)) return true; else return false; }
below menu supposed allow user choose enter date or quit, date not being accepted correctly.
{ public static void main ( string [] args) // user selects how enter dates { +"(1) enter date mm/dd/yyyy\n" +"(any key) quit\n"; int input=0; date newdate; do{ string userinput = joptionpane.showinputdialog(null, menu); input = integer.parseint( userinput); string outputmessage=""; if ( input = 1) { userinput =joptionpane.showinputdialog(null, "please enter date 02/28/2011"); switch ( input ) // here menu choice evaluated { case 1 : token = userinput.split("/"); if (token.length == 3 ) { newdate = new date( integer.parseint( token[0]), integer.parseint( token[1]), integer.parseint( token[2]) ); outputmessage = newdate.tostring(); joptionpane.showmessagedialog(null, outputmessage); } break; case 2: } while ( input <>1); // quit program when user selects key other 1 @ menu.
- you should use
java.text.dateformat
parse date strings - writing own date arithmetic error prone (leap years 1 exception among many), better use
java.util.calendar
implementation instead.
as date arithmetic, here's how it's done calendar
:
//todo: parse input string dateformat date startdate = ... // parsed date calendar cal = new gregoriancalendar(); cal.settime(startdate); (int = 0; < 40; i++) { // add day cal.add(calendar.date, 1); // , new result date date date = cal.gettime(); system.out.println(date); }
if need count backwards, add negative time amounts (days, hours etc).
Comments
Post a Comment