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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Mick BarryJava DeveloperCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.