Reputation: 297
For a given date, how to get the end date of last quarter? I need to run a job, which takes this into account. EDIT: 1st quarter is Jan, Feb, Mar; 2nd is Apr, May, June, so on;
Any help is appreciated. Thanks
Upvotes: 3
Views: 13678
Reputation: 15190
Basically:
So to figure out which is the current quarter:
int quarter = (myDate.getMonth() / 3) + 1;
(Note that getMonth() is deprecated in favor of Calendar.get(Calendar.MONTH)
.)
Then match the previous quarter to a date.
int prevQuarter = (myDate.getMonth() / 3);
switch(prevQuarter) {
case 3 :
// return September 30
case 2 :
// return June 30
case 1 :
// return March 31
case 0 : default :
// return December 31
}
Upvotes: 8
Reputation: 3497
well quarters only end in months 12,9,6 and 3. all of these have a static length. so there are only 4 valid end dates. compare your month and if it is 1 or 2 or 3, answer is 31.12 of year-1 , if it is 4,5,6 it is 31.3 of this year, if it is 7,8,9 it is 30.6 of this year. if it is 10.11.12 it is 30.9 this year.
Upvotes: 1
Reputation: 4273
http://www.taggercat.com/docs/api/TaggerCat/com/taggercat/util/DateUtils.html#last90Days%28%29
public static java.util.Date[] last90Days()
Returns the two dates that represent the start and end of the last quarter
Returns: the two dates that represent this period
This would help, using these dates and the date given, you can determine the date of the last quarter. If I'm understanding your question correctly that is.
Upvotes: 0