Some good answers are here, but they are out of date. The java.time classes make this work a lot easier.
java.time
The inconvenient old classes associated with the earliest versions of Java have been superseded by the java.time classes built into Java 8 and later. See Oracle Tutorial . Most of the functionality was ported to Java 6 and 7 in ThreeTen-Backport and further adapted for Android in ThreeTenABP .
Month
Given that seasons are determined here for whole months, we can use the convenient Month enum . Such enumeration values ββare better than simple integer values ββ(1-12), because they are type safe and you are guaranteed valid values.
EnumSet
An EnumSet is a fast and compact way to track a subset of enum values.
EnumSet<Month> spring = EnumSet.of( Month.MARCH , Month.APRIL ); EnumSet<Month> summer = EnumSet.of( Month.MAY , Month.JUNE , Month.JULY , Month.AUGUST ); EnumSet<Month> fall = EnumSet.of( Month.SEPTEMBER , Month.OCTOBER ); EnumSet<Month> winter = EnumSet.of( Month.NOVEMBER , Month.DECEMBER , Month.JANUARY , Month.FEBRUARY );
As an example, we get the current moment for a specific time zone.
ZoneId zoneId = ZoneId.of( "America/Montreal" ); ZonedDateTime zdt = ZonedDateTime.now( zoneId );
Set this date value for your Month .
Month month = Month.from( zdt );
See which season the EnumSet has this particular month value by calling contains .
if ( spring.contains( month ) ) { β¦ } else if ( summer.contains( month ) ) { β¦ } else if ( fall.contains( month ) ) { β¦ } else if ( winter.contains( month ) ) { β¦ } else { // FIXME: Handle reaching impossible point as error condition. }
Define Your Own Season List
If you use the idea of ββthis season around your code base, I suggest defining your own enum, "Season".
The main listing is simple: public enum Season { SPRING, SUMMER, FALL, WINTER; } public enum Season { SPRING, SUMMER, FALL, WINTER; } . But we also add a static of method to find which cards of the month are in which season.
package com.example.javatimestuff; import java.time.Month; public enum Season { SPRING, SUMMER, FALL, WINTER; static public Season of ( Month month ) { switch ( month ) { // Spring. case MARCH: // Java quirk: An enum switch case label must be the unqualified name of an enum. So cannot use `Month.MARCH` here, only `MARCH`. return Season.SPRING; case APRIL: return Season.SPRING; // Summer. case MAY: return Season.SUMMER; case JUNE: return Season.SUMMER; case JULY: return Season.SUMMER; case AUGUST: return Season.SUMMER; // Fall. case SEPTEMBER: return Season.FALL; case OCTOBER: return Season.FALL; // Winter. case NOVEMBER: return Season.WINTER; case DECEMBER: return Season.WINTER; case JANUARY: return Season.WINTER; case FEBRUARY: return Season.WINTER; default: System.out.println ( "ERROR." ); // FIXME: Handle reaching impossible point as error condition. return null; } } }
Here's how to use this listing.
ZoneId zoneId = ZoneId.of ( "America/Montreal" ); ZonedDateTime zdt = ZonedDateTime.now ( zoneId ); Month month = Month.from ( zdt ); Season season = Season.of ( month );
Dump for the console.
System.out.println ( "zdt: " + zdt + " | month: " + month + " | season: " + season );
zdt: 2016-06-25T18: 23: 14.695-04: 00 [America / Montreal] | month: JUNE | season: SUMMER