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

Date functionality URGENT!!

 
Chhaya Dhanani
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Senthil B Kumar
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
}
}
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic