• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Subtracting from java.util.Date

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am looking for date functions which will allow me to subract 1 day, 1 week, 1 month, 3 mths, 1 year.

Date now = new Date();
From now, I want to subtract 1 day, 1 week, 1 month, 3 mths, 1 year.


I searched the the java.util classes.
java.uil.Calendar has a add() method but it doesn't return a java.util.Date object.

Can anyone tell me how i could do this subtraction and the return value is a java.util.Date object.

Thanks,
Gayatri
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's been a while ... but I think you want something along the lines of:

Date startingDate;
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(startingDate);
gc.add(Calendar.DAY_OF_YEAR, -1);
Date result = gc.getTime();
 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave.
It works...
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes...
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if i want to subtract 21years ..

please don't say gc.add(Calendar.DAY_OF_YEAR, -21); which is not working :P


Well it wouldn't work, that is subtracting 21 days and not 21 years. If you want to subtract years you need to use:
 
Bhaskar boddupalli
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.. i had found my mistake
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't edit your posts after they have been replied to. It makes the replies look odd. If Tony hadn't quoted your post I wouldn't have understood what he was going on about.
 
A wop bop a lu bop a womp bam boom! Tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic