Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar dates

 
raj chopra
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi All,
I am working on providing data from yesterday date to jan 1'st of last year sequentially.
eg : from 3/19/2013 to 1/1/2012
from 3/19/2013 to 1/1/2011 so on for 10 years.

From the below code i am getting the date exactly 1 year back date,like 3/19/2012. how can i get the subtration to be dne in such a way that i could get dates from jan 1'st onwards?

sample code:
Calendar cal = Calendar.getInstance();
cal.add(cal.YEAR, -1) ;

please help on this.
 
Shashank Gollapudi
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calendar c1= Calendar.getInstance();
System.out.println(c1.getTime());
c1.set(Calendar.DAY_OF_MONTH, 1);
c1.set(Calendar.MONTH,0);
c1.set(Calendar.YEAR,c1.get(Calendar.YEAR)-1 );
System.out.println(c1.getTime());

Please let me know if this meets your requirement
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic