The moose likes Swing / AWT / SWT and the fly likes date problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "date problem" Watch "date problem" New topic

date problem

Aanchal Tyagi

Joined: Jul 21, 2006
Posts: 5
private String date = new SimpleDateFormat("dd-MM-yy", Locale.getDefault()).format(new java.util.Date();

is there any way to add n days in above variable

for example
private String date=22 aug 2006

if i add 7 days to it ,it becomes
date=29 aug 2006

is ther any way to convert string into date please help by giving example if possible.

thanks in advance.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> is there any way to add n days in above variable

create a new Date out of the current Date's milliseconds + n*(milliseconds in a day).
but this is a real 'hack'.

better to create a calendar object, add 7 days, then format calendar.getTime()
Brian Mozhdehi
Ranch Hand

Joined: Aug 17, 2006
Posts: 81
conversion from String to Date and vice versa is done using the class SimpleDateFormat, which is in the java.text package. You can search on it in google and find Suns javadoc page which is an excellent reference on how to use it.
I agree. Here's the link:
subject: date problem
It's not a secret anymore!