Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help with JAVA dates (URGENT!)

 
Ronald Whalen
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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?
Thanks,

Ronald.
 
Yogen Vadnere
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ronald Whalen
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the Help !
 
Tim Blommerde
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Tim

Code Example - DateTest.java

[This message has been edited by Tim Blommerde (edited September 28, 2001).]
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://www.webappcabaret.com/javachina/faq/04.htm#util_Q2
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic