1

I have achieved getting the previous quarter like as shown below:

Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int presentQuarter = (month / 3) + 1;
int lastQuarter = (presentQuarter - 1) > 0 ? presentQuarter - 1 : 4;

I have got the lastQuarter but how I will get the last quarter's year.

Can anyone please help me on this?

Alex Man
  • 4,746
  • 17
  • 93
  • 178
  • 2
    FYI, you are using troublesome old classes that are now legacy, supplanted by the java.time classes. – Basil Bourque Jul 10 '17 at 16:37
  • @BasilBourque do you think those legacy classes gives me wrong values – Alex Man Jul 10 '17 at 17:15
  • 1
    It's not the case of wrong values, but how difficult it is to work with the legacy vs the new API, which make things much easier (and of course, doing the math by yourself instead of letting the API does it for you is much more error prone). Not to mention the [tons of well known problems](https://stackoverflow.com/questions/1571265/why-is-the-java-date-api-java-util-date-calendar-such-a-mess) and [design issues](https://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api) of the old API. –  Jul 10 '17 at 18:03

4 Answers4

5

You can use your own condition for that too,

year = (presentQuarter - 1) > 0 ? year:year-1;
isurujay
  • 1,386
  • 10
  • 15
3

You need to have a separate variable for storing your quarter calculation. I would try something like the following.

int calculatedLastQuarter = presentQuarter - 1;
int lastQuarterYear = (calculatedLastQuater < 0) ? year-1 : year;
int lastQuarter = (presentQuarter < 0) ? 4 : calculatedLastQuater;
dblanken
  • 56
  • 2
  • 5
2

Although you can do the math by yourself (as the other answers are already covering), you could consider using the new date/time API.

If you're using Java 8, consider using the new java.time API. It's easier, less bugged and less error-prone than the old APIs.

If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it here).

The code below works for both. The only difference is the package names (in Java 8 is java.time and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp), but the classes and methods names are the same.

The code is straightforward:

// current date
LocalDate now = LocalDate.now();

// get quarter
int presentQuarter = now.get(IsoFields.QUARTER_OF_YEAR);

// year for last quarter
int year = now.minus(1, IsoFields.QUARTER_YEARS).getYear();

LocalDate is in java.time or org.threeten.bp package, and IsoFields is in java.time.temporal or org.threeten.bp.temporal (depending on whether you're using Java 8 or ThreeTen Backport).


I'm using LocalDate.now(), which gets the current date using the system's default timezone, but it's better to always make explicit what timezone you're using (even it you use the default):

// use system default timezone
LocalDate now = LocalDate.now(ZoneId.systemDefault());

But the system's default timezone can change (even at runtime), so it's even better to explicit one by name:

// current date using an explicit timezone name
LocalDate now = LocalDate.now(ZoneId.of("Europe/London"));

The API uses IANA timezones names (always in the format Continent/City, like America/Sao_Paulo or Europe/Berlin). Avoid using the 3-letter abbreviations (like CST or PST) because they are ambiguous and not standard.

You can get a list of available timezones (and choose the one that fits best your system) by calling ZoneId.getAvailableZoneIds().

2

tl;dr

how I will get the last quarter's year.

YearQuarter.now( ZoneId.of( "Africa/Casablanca" ) ) 
           .minusQuarters( 1 )
           .getYear()

2017

YearQuarter

The ThreeTen-Extra project adds functionality to the java.time classes built into Java 8 and later.

Among the project’s classes are Quarter and YearQuarter. These quarters are based on the ISO 8601 calendar, running January-March and so on.

Get current quarter.

YearQuarter yq = YearQuarter.now( ZoneId.of( "Pacific/Auckland" ) ) ;

Call minusQuarters to do math.

YearQuarter yqPrevious = yq.minusQuarters( 1 ) ;

If needed, you can extract the year and quarter numbers. Better to pass these YearQuarter objects around your code base than to use mere ints, to gain type-safety and endure valid values.

int year = yqPrevious.getYear() ;
int q = yqPrevious.getQuarterValue() ;

To serialize to a string, call toString to generate a string in a format that follows the style of the ISO 8601 formats: yyyy-Qq (though that standard does not actually address quarters).

String output = yq.toString() ;

…and parsing…

YearQuarter yq = YearQuarter.parse( "2017-Q1" ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154