Some good answers here, but they are outdated. The java.time classes make this work much easier.
java.time
The troublesome old classes bundled with the earliest versions of Java have been supplanted by the java.time classes built into Java 8 and later. See Oracle Tutorial. Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
Month
Given that seasons are defined here using whole months, we can make use of the handy Month
enum. Such enum values are better than mere integer values (1-12) because they are type-safe and you are guaranteed of valid values.
EnumSet
An EnumSet
is a fast-performing and compact-memory 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 particular time zone.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.now( zoneId );
Ask that date-time value for its Month
.
Month month = Month.from( zdt );
Look for which season EnumSet
has that 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” enum
If you are using this season idea around your code base, I suggest defining your own enum, “Season”.
The basic enum is simple: public enum Season { SPRING, SUMMER, FALL, WINTER; }
. But we also add a static method of
to do that lookup of which month maps to which season.
package work.basil.example;
import java.time.Month;
public enum Season {
SPRING, SUMMER, FALL, WINTER;
static public Season of ( final 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;
}
}
}
Or use the switch expressions feature (JEP 361) of Java 14.
package work.basil.example;
import java.time.Month;
import java.util.Objects;
public enum Season
{
SPRING, SUMMER, FALL, WINTER;
static public Season of ( final Month month )
{
Objects.requireNonNull( month , "ERROR - Received null where a `Month` is expected. Message # 0ac03df9-1c5a-4c2d-a22d-14c40e25c58b." );
return
switch ( Objects.requireNonNull( month ) )
{
// Spring.
case MARCH , APRIL -> Season.SPRING;
// Summer.
case MAY , JUNE , JULY , AUGUST -> Season.SUMMER;
// Fall.
case SEPTEMBER , OCTOBER -> Season.FALL;
// Winter.
case NOVEMBER , DECEMBER , JANUARY , FEBRUARY -> Season.WINTER;
}
;
}
}
Here is how to use that enum.
ZoneId zoneId = ZoneId.of ( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.now ( zoneId );
Month month = Month.from ( zdt );
Season season = Season.of ( month );
Dump to 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