How to print next date corresponding to the date entered in JAVA

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:

[italic]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;
}
[/italic]


and the code that prints next date:

[italic]

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);
}
}
[/italic]
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.

Comments

  • Hello,
    Modified your code to get user input. You will need to tweak the boundary checking.
    These links may help: [link=http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/]date use example[/link] and [link=http://download.oracle.com/javase/6/docs/api/index.html?java/text/DateFormat.html]Java api for simpleDateFormat[/link].
    [code]import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    /* Get the user input the complete date
    convert the date into the required format,
    also check the boundary value checks.
    */

    public class NextDate {

    static String errorMessage;

    public static 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.";
    System.out.println(errorMessage);
    return false;
    }
    if (!sdf.format(testDate).equals(date)) {
    errorMessage = "The date that you provided is invalid.";
    System.out.println(errorMessage + "
    testDate= " + testDate + ",entered date= " + date);
    return false;
    }
    System.out.println("The date you entered is: " + testDate);
    return true;
    }

    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);

    // Read and parse input, stopping on a blank input line
    BufferedReader reader =
    new BufferedReader(new InputStreamReader(System.in));
    try {
    System.out.print("ENTER DATE STRING: ");
    String dateString = reader.readLine();

    while ((dateString != null) && (dateString.length() > 0)) {
    NextDate.isValidDate(dateString);
    System.out.print("ENTER DATE STRING: ");
    dateString = reader.readLine();
    }
    } catch (IOException ioe) {
    System.out.println("I/O Exception: " + ioe);
    }

    }
    }[/code]

    regards, se52
  • Thanks a lot. :)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories