• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date functionality URGENT!!

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
}
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic