This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calendar

 
Manoj Raghuwanshi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to print date :-
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, -1);
System.out.println(cal.getTime());
cal.set(Calendar.DAY_OF_MONTH, -1);
System.out.println(cal.getTime());
cal.set(Calendar.DAY_OF_MONTH, -1);
System.out.println(cal.getTime());
cal.set(Calendar.DAY_OF_MONTH, -1);
System.out.println(cal.getTime());


it is giving output:-

Tue Oct 30 02:17:20 PDT 2007
Sat Sep 29 02:17:20 PDT 2007
Thu Aug 30 02:17:20 PDT 2007
Mon Jul 30 02:17:20 PDT 2007

can any body please tell me what should be the code so that output will be:-
Tue Oct 30 02:17:20 PDT 2007
Mon Oct 29
Sun Oct 28
Sat Oct 27
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Calendar.set() method sets a field. You are trying to set the day of the month to -1, which obviously produces an unexpected result. To decrement the day use Calendar.add(Calendar.DAY_OF_MONTH, -1).
[ November 02, 2007: Message edited by: Joanne Neal ]
 
Manoj Raghuwanshi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic