Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to print next date corresponding to the date entered in JAVA Posted by smarsam28 on 9 Sept 2010 at 1:24 PM
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.
Report
Re: How to print next date corresponding to the date entered in JAVA Posted by silveredge52 on 10 Sept 2010 at 5:40 AM
Hello,
Modified your code to get user input. You will need to tweak the boundary checking.
These links may help: date use example and Java api for simpleDateFormat.
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 + "\n\ttestDate= " + 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);
        }

    }
}


regards, se52
Report
Re: How to print next date corresponding to the date entered in JAVA Posted by smarsam28 on 17 Sept 2010 at 7:43 AM
Thanks a lot. :)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.