i want to write a program which takes valid date as input in format mm/dd/yyyy and outputs the next days date, considering all boundary conditions.
Example: If I input 02/18/2010 the output of program should be 02/19/2010.
i wrote the code to validate the date:
public boolean isValidDate(String date)
{
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date testDate = null;
try
{ testDate = sdf.parse(date);
}
catch (ParseException e)
{ errorMessage = "the date you provided is in an invalid date" + " format.";
return false;
}
if (!sdf.format(testDate).equals(date))
{ errorMessage = "The date that you provided is invalid.";
return false;
}
return true;
}
and the code that prints next date:
public class NextDate
{ public static void main(String[] args)
{
int oneDay = 1000 * 60 * 60 * 24;
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
String currDate = dateFormat.format(date.getTime());
String nextDate = dateFormat.format(date.getTime() + oneDay);
System.out.println("Currnent date: " + currDate);
System.out.println("Next date: " + nextDate);
}
}
now the problem i'm facing is how to make the user input the complete date and how to convert the date into the required format. Also i'm confused about the boundary value checks.