views:

97

answers:

3

I am trying to get the program to call up the current date, add 30 days to it, and then out put that date as a string.

        // Set calendar for due date on invoice gui
    Calendar cal = Calendar.getInstance();

    // Add 30 days to the calendar for the due date
    cal.add(Calendar.DATE, 30);
    Date dueDate = cal.getTime();
    dueDatestr = Calendar.toString(dueDate);
+2  A: 

And the question is?

If you want to format your date, I suggest looking at java.text.SimpleDateFormat instead of using toString(). You can do something like:

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
dueDateStr = dateFormat.format(dueDate); // renders as 11/29/2009
ChssPly76
+1  A: 

You almost have it:

Date dueDate = cal.getTime();
String dueDateAsString = dueDate.toString();

or

String dueDateAsFormattedString = DateFormat.format(dueDate);
A: 

You might want to consider using FastDateFormat from Apache commons, instead of SimpleDateFormat, because SimpleDateFormat is not thread safe.

FastDateFormat dateFormat = FastDateFormat.getInstance("MM/dd/yyyy");
dueDateStr = dateFormat.format(dueDate);

This is especially true if you wanted to use a static instance of the date formatter, which is a common temptation.

Spike Williams