File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes computing and formatting a date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "computing and formatting a date" Watch "computing and formatting a date" New topic

computing and formatting a date

Eric Edwards

Joined: Oct 13, 2005
Posts: 28
I am teching myself Java 5.
This date thing has got me stumped. The best thing to do is to show some code, but I have been struggling with this for 2 days and none of it works.
Thae exercise in the book I am using says to take todays date and use as the invoice date. Then format this date into a SHORT date and display it. No problem.
The other part is to take the current date and add 30 days to it and display that as a SHORT date also. I am missing something along the way.
I have impoted java.util.*;
I have tried DateUtil, GregorianCalendar, etc. Can't get this to work.
Any help would be appreciated.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if you already have a calendar object set to today's date,
all you do is use calendar's method add(field, number)
where field would be Calendar.DATE and number would be 30
Eric Edwards

Joined: Oct 13, 2005
Posts: 28
Hey Michael,
Thanks for the reply. I finally got it. Think I was making it too hard.
Here is what I did:
Eric Edwards

Joined: Oct 13, 2005
Posts: 28
I went back to my program and messed with the date some more.
In checking I found that the way I had done it causes two problems:
1. In using "roll" it only changes the month field. It does not change the year field if it takes you into the next year.
Using the word "add" instead of "roll" takes care of these things.
2. Using "Calendar.DATE" is very accurate compared to adding a month on.

This has been a real learning experience for me.
Thanks again,
I agree. Here's the link:
subject: computing and formatting a date
It's not a secret anymore!