Java Dates Question

Hello,

I am a beginning Java programmer that is working on a basic program for invoicing. This program must print todays date on the invoice, and then print a ship date of 7 days later. There are two classes, invoiceDriver and invoiceGUI. The dates are printed in the GUI.

I need to create two methods in my invoiceDriver class as follows:

1) currentDate()
        - Returns the current date as type string
2) shipDate()
        - Returns the date 7 days after the current date as type string

I have been working on this for awhile, experimenting with the Date and Calendar java.utils, but I have not had any luck with adding the 7 days to the current date because in order to add the 7 days, the current date must be of type date, but it needs to return a string type, so my IDE returns errors.

Can anyone show me how to do this?

Thanks,

Kody
LVL 6
Kody-BurgAsked:
Who is Participating?
 
for_yanCommented:
You can also use newer way with Calendar:

SimpleDateFormat f00 = new SimpleDateFormat("MM/dd/yyyy");

Calendar todaysDate = Calendar.getInstance();
       
System.out.println(f00.format(todaysDate.getTime()));
       todaysDate.add(Calendar.DATE, +7);
System.out.println(f00.format(todaysDate.getTime()));
0
 
for_yanCommented:

That is how I do it :

Date now = new Date();

Date nowPlus7 = new Date(now.getTime() + 7L*24L*3600L*1000L);
0
 
for_yanCommented:
If you want to format them to print, the  you use SimpleDateFormat

SimpleDateFormat f = new SimpleDateFormat("MM/dd/YYYY");

System.out.println(f.format(now));

System.out.println(f.format(nowPlus7));

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
for_yanCommented:
It lloks like year in pattern should be lower case, so it should rather be like that:


SimpleDateFormat f = new SimpleDateFormat("MM/dd/yyyy");

System.out.println(f.format(now));

System.out.println(f.format(nowPlus7));


0
 
CEHJCommented:
Here's a way to do it returning an array of today and next week. main show a way of calling it. Your code can just say

String[] todayAndNextWeek = DateFetcher.getTodayAndNextWeek();
import java.util.Calendar;
import java.sql.Date;

public class DateFetcher {
    private static Calendar cal = Calendar.getInstance();

    // Testing only
    public static void main(String[] args) {
	System.out.println(java.util.Arrays.toString(DateFetcher.getTodayAndNextWeek()));
    }

    public static String[] getTodayAndNextWeek() {
	String[] result = new String[2];
	Date now = new Date(System.currentTimeMillis());
	result[0] = now.toString();
	cal.setTime(now);
	cal.add(Calendar.DATE, 7);
	result[1] = new Date(cal.getTimeInMillis()).toString();
	return result;
    }
}

Open in new window

0
 
CEHJCommented:
That uses ISO format - imo the only rational one, but you can use a DateFormat if you don't want that
0
 
Kody-BurgAuthor Commented:
@for_yan

In your first comment, how do I convert that Date data type to a string data type so that I can return it?
0
 
objectsCommented:
for _yan showed how to do that in the second comment

some more details here
http://helpdesk.objects.com.au/java/how-do-i-format-a-date-as-a-string
0
 
Kody-BurgAuthor Commented:
I ended up figuring out how to do it for what I needed it for based on the information that for_yan posted for me.

Thank you for your help!

Kody
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.