*
The moose likes Beginning Java and the fly likes calendar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calendar " Watch "calendar " New topic
Author

calendar

Manoj Raghuwanshi
Ranch Hand

Joined: Jun 20, 2004
Posts: 75
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

Joined: Aug 05, 2005
Posts: 3426
    
  12
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 ]

Joanne
Manoj Raghuwanshi
Ranch Hand

Joined: Jun 20, 2004
Posts: 75
thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: calendar
 
Similar Threads
Help Adding Months to Gregorian Calendar
Calendar() / Date() bug ???
Incrementing Date By 6 Months
Help, lunarcal.js
Calender class and its components