GregorianCalendar and how to subtract MONTH from a date.

M

Manoj Nair

I have my Gregorian Cal set to 28-FEB-2002. I basically want to
subtract a month from this date to get 31-Jan-2002. I do the
following..

GC.add(Calendar.MONTH,(-1)*Integer.parseInt(Long.toString(1) ).

This prints out 28-jan-2002 instead of 31-jan-2002. Similary if
original date was 30-APR-2002 and i subtract 1 month I get 30-MAR-2002
instead of 31-MAR-2002.

It seems like when you subtract months from a calender date it always
takes away 30 days from date passed ???
 
S

Shripathi Kamath

Manoj Nair said:
I have my Gregorian Cal set to 28-FEB-2002. I basically want to
subtract a month from this date to get 31-Jan-2002. I do the
following..

GC.add(Calendar.MONTH,(-1)*Integer.parseInt(Long.toString(1) ).

Isn't that equivalent to GC.add(Calendar.MONTH, -1);? Or are we missing
some subtlety.
This prints out 28-jan-2002 instead of 31-jan-2002. Similary if
original date was 30-APR-2002 and i subtract 1 month I get 30-MAR-2002
instead of 31-MAR-2002.

That is correct, you are retaining the date, just going back a month. The
effect you seek requires some intelligence that you need to provide, i.e.
create the algorithm yourself.

For example, if you had a date of 18-FEB-2002, and you subtracted a month
from it, would you expect the software to yield 18-JAN-2002 or
31-JAN-2002?


It seems like when you subtract months from a calender date it always
takes away 30 days from date passed ???

No, it does not. It simply retracts the date by a month, exactly as it
should.

If you want to go to the last day of the previous month, *a* way is to do
the following:

1. set the day on the month in the date to 1;
2. subtract a day from the date by getting the time in milliseconds,
subtracting 86400000L, and storing away the result

HTH,
 
M

Michael Dunn

: I have my Gregorian Cal set to 28-FEB-2002. I basically want to
: subtract a month from this date to get 31-Jan-2002. I do the
: following..
:
: GC.add(Calendar.MONTH,(-1)*Integer.parseInt(Long.toString(1) ).
:
: This prints out 28-jan-2002 instead of 31-jan-2002. Similary if
: original date was 30-APR-2002 and i subtract 1 month I get 30-MAR-2002
: instead of 31-MAR-2002.
:
: It seems like when you subtract months from a calender date it always
: takes away 30 days from date passed ???


If you always have the end of the month and you want the end of the prior month,
try something like this


import java.util.*;
import java.text.SimpleDateFormat;

class GregorianCalendarEX
{
public static void main(String args[])
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = new GregorianCalendar();
cal.set(2002,1,28);
System.out.println(sdf.format(cal.getTime()));
cal.add(Calendar.DATE,-cal.get(Calendar.DAY_OF_MONTH));
System.out.println(sdf.format(cal.getTime()));
System.exit(0);
}
}
 
Y

Yar Hwee Boon

I have my Gregorian Cal set to 28-FEB-2002. I basically want to
subtract a month from this date to get 31-Jan-2002. I do the
following..

GC.add(Calendar.MONTH,(-1)*Integer.parseInt(Long.toString(1) ).

This prints out 28-jan-2002 instead of 31-jan-2002. Similary if
original date was 30-APR-2002 and i subtract 1 month I get 30-MAR-2002
instead of 31-MAR-2002.

It seems like when you subtract months from a calender date it always
takes away 30 days from date passed ???

Yes, unless the resultant date is invalid, then it will take the last
valid date before the result. Eg. 2003-March-30 - 1 month. will yield
2003-Feb-28, and 2000-March-30 - 1 month will return 2000-March-29,
taking leap year into account.

For your case, just do this after you call add().
GC.set(Calendar.DATE, GC.getActualMaximum(Calendar.DATE));

which is >= ver 1.2.


Hwee Boon
 
M

Manoj Nair

I have my Gregorian Cal set to 28-FEB-2002. I basically want to
subtract a month from this date to get 31-Jan-2002. I do the
following..

GC.add(Calendar.MONTH,(-1)*Integer.parseInt(Long.toString(1) ).

This prints out 28-jan-2002 instead of 31-jan-2002. Similary if
original date was 30-APR-2002 and i subtract 1 month I get 30-MAR-2002
instead of 31-MAR-2002.

It seems like when you subtract months from a calender date it always
takes away 30 days from date passed ???

Thanks for your replies !

I could solve the problem by just adding a day before and then
subtracting a day after the operation.

eg:
GC.add(Calendar.DATE,1 ).
GC.add(Calendar.MONTH,-1 )
GC.add(Calendar.DATE,-1 ).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top