wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Date functionality URGENT!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date functionality URGENT!!" Watch "Date functionality URGENT!!" New topic
Author

Date functionality URGENT!!

Chhaya Dhanani
Ranch Hand

Joined: Apr 11, 2001
Posts: 70
hi all,
i am having a slight problem manipulating dates
The code requires a inpout odf date in string format (yyyymmdd) eg 20021218
what i need is a function which will calculate one day less from the given day i.e 20021217
if it is 20030101 .. it should give me 20021231
also take care of feb and leap year..
i need this asap ..
deadlines to meet consider this as an SOS
Thanx in advance
Chhaya
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Check out java.text.SimpleDateFormat for a way to output dates in a particular format.... simply.
Using java.util.Calendar you can add -1 to the day field to get the previous day.
Dave
girish rateshwar
Ranch Hand

Joined: Mar 04, 2001
Posts: 97
Hi,
Well as david suggested, using the calendar class in the util package.
-----
calendar c1= new calendar();
Date d1= new Date();
And passing a parameter to the setTime() method--object of the date class.
while using the get() function:
c1.get((Calendar.Date)-1)<<----
Regards,
[ December 19, 2002: Message edited by: girish rateshwar ]

Girish
Senthil B Kumar
Ranch Hand

Joined: Feb 09, 2004
Posts: 140
import java.util.Date;
import java.util.Calendar;
public class Prev
{
public static void main(String args[])
{
Calendar xx = Calendar.getInstance();
Date dt = null;
xx.add(Calendar.DATE,-1);
dt = xx.getTime();
System.out.println("Yesterday Is : " + dt.getDate());
}
}


Work like you don't need the money. Love like you've never been hated. Dance like nobody's watching. Sing like nobody's listening. Live like it's Heaven on Earth.
Currently I Reside Here WEBlog
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
Originally posted by girish rateshwar:
Hi,
Well as david suggested, using the calendar class in the util package.
-----
calendar c1= new calendar();
Date d1= new Date();

And passing a parameter to the setTime() method--object of the date class.

while using the get() function:
c1.get((Calendar.Date)-1)<<----

Regards,

[ December 19, 2002: Message edited by: girish rateshwar ]


I think you have the get() and add() methods mixed up. The get() method simply returns one of the fields of a Calendar object, it does not alter the date like the add() method does.


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Date functionality URGENT!!