Amira
Amira

Reputation: 3270

Returning the date that is a specified number of months before or after a given Date or Date/Time expression

I just don't wanna reinvent the wheel : i need something in java that do this (because 'm migrating a vfox pro project to java):

From GOMONTH( ) Function Visual fox pro documentation :

Returns the date that is a specified number of months before or after a given Date or Date/Time expression.

GOMONTH(dExpression | tExpression, nNumberOfMonths)

Parameters

dExpression

Specifies a date expression for which GOMONTH( ) returns the date.

tExpression

Specifies a date/time expression for which GOMONTH( ) returns the date.

nNumberOfMonths

Specifies the number of months from the date or date/time.If nNumberOfMonths is positive, GOMONTH( ) returns a date that is nNumberOfMonths months after the date or date/time. If nNumberOfMonths is negative, GOMONTH( ) returns a date that is nNumberOfMonths months before the date or date/time. For example, -1 means -31 days.

Here's a simple example of execution result using visual fox pro :

? GOMONTH({^1998-12-31}, 2)  && Displays 02/28/1999
? GOMONTH({^1998-12-31}, -2)  && Displays 10/31/1998

Upvotes: 0

Views: 113

Answers (1)

mwhidden
mwhidden

Reputation: 555

java.util.Calendar's add() method will do what you want.

Eg., 2 months ago:

Calendar c = Calendar.getInstance()
c.add(Calendar.MONTH, -2);

Upvotes: 1

Related Questions