Get Season By Month and Date in Java

The following illustrates getting season name by month and date in Java.
import java.util.*;
import java.text.*;
class CheckSeason
    public static void main(String args[])
        Scanner s=new Scanner(;
        System.out.println("Enter the month and day");
        int m=s.nextInt();
        int d=s.nextInt();
        SimpleDateFormat f=new SimpleDateFormat("MM-dd");
            Date date=f.parse(m+"-"+d);
                if(date.after(f.parse("04-21")) && date.before(f.parse("06-21"))){
                else if(date.after(f.parse("06-20")) && (date.before(f.parse("09-23"))))
                else if(date.after(f.parse("09-22")) && date.before(f.parse("12-22")))
                else System.out.println("Winter");
        }catch(Exception e){
            System.out.println("Invalid month/date "+e);
new SimpleDateFormat("MM-dd"): Create an object for the SimpleDateFormat class of the format month-date.
setLenient(false): Allow strict checking of the date, so that month cannot be greater than 12 and February contains no more than < 30 days always etc.
f.parse(m+"-"+d): This creates a java.util.Date object from given month and date in the specified format.
date.after(): This method checks if the parameter date is after the calling date, similiarily date.before() checks if the parameter date is before the calling date with the help of which we determine the season.

Posted by at on

Tags: Java Utilities,