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 Please help with JAVA dates  (URGENT!) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Please help with JAVA dates  (URGENT!)" Watch "Please help with JAVA dates  (URGENT!)" New topic

Please help with JAVA dates (URGENT!)

Ronald Whalen
Ranch Hand

Joined: Mar 27, 2001
Posts: 38
I am trying to take a date in the format 061876 and increment it by a certain number of days, ie. 50 days. I need to retain the format so that it returns like 08??76.
I would then have to take the result 08??76 and the original date (061876) to get the number of days it was incremented.
Any tips that could help with this?

Yogen Vadnere
Ranch Hand

Joined: Sep 20, 2001
Posts: 58
Hope following statement will help you,
java.sql.Date myDate = new Date(System.currentTimeMillis() +(50*24*60*60*1000)) ;
now u can format myDate as u want.

Yogen Vadnere
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Take a look at java.text.SimpleDateFormat for the parsing and formating part, at java.util.GregorianCalendar for the calculation part. You will have to convert between java.util.Date and java.util.GregorianCalendar for this, which is a bit unwieldy, but possible.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ronald Whalen
Ranch Hand

Joined: Mar 27, 2001
Posts: 38
Thanks for the Help !
Tim Blommerde

Joined: Sep 24, 2001
Posts: 21
Dear Ronald,
I've written a small application for you that will do what you wanted. I've not added comments, because I think that you will be able to follow the flow quite easily and that together with the API's javadocs you will be able to understand the example without any difficulties.
Note that you can call the run the application with two different argument sets. Either by giving two dates:

This will computes the difference in days between the two days. If there's a difference in years, the answer will not be correct anymore. You could of course add a check in difference in years too, but that's up to you. The second parameter set should consist of a date and a number, with the character 'd' in front of it:

This will give the date, # days after the given date. Hopefully this will help you a bit further. Date calculations are a bit tedious, but with the above example and the previous posts, you should be able to write your application with one hand on your back.
Good luck,

Code Example -

[This message has been edited by Tim Blommerde (edited September 28, 2001).]
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
It can be a lot easier!!!
I've a very very simple code online, which does exactly what you want to do. All you need to do is change the date format from "MM/dd/yyyy" to whatever you want.
Here you are:
I agree. Here's the link:
subject: Please help with JAVA dates (URGENT!)
It's not a secret anymore!