wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "computing and formatting a date" Watch "computing and formatting a date" New topic
Author

computing and formatting a date

Eric Edwards
Greenhorn

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.
Eric
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
Greenhorn

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
Greenhorn

Joined: Oct 13, 2005
Posts: 28
Michael,
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,
Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: computing and formatting a date
 
Similar Threads
Calendar display code
JConsole csv time format?
Please help with JAVA dates (URGENT!)
Regarding sysdate
Date Calculations