0

I'm writing a class that is dependent on months and how many days each month has. At the beginning of my class, I've written:

static final int    JAN = 1;
static final int    FEB = 2;
...
static final int    DEC = 12;

It looks like a messy way of doing this, and I feel that enums would be more efficient. I need to be able to "increment" the month (where DEC + 1 = JAN), and need to be able to know have many days there are in the month (with FEB having 28).

I see two ways of doing this:

  1. My current way, defining static final ints and using a switch statement to find the number of days, and when incrementing I always checks for whether the value would be greater than 12.

  2. Create a set of enums, each with inputs for which month of the year it is and how many days it contains, and defining an "increment" method.

Is there a better way of doing this, or is (presumably) 2. the best way of doing this?

5
  • 3
    Don't. Simply don't do that. Please. Use the built-in classes for date-time handling. Unless you like to hurt yourself. Commented Feb 28, 2016 at 17:12
  • 1
    Look at the new Java Time API. Don't read the answers pointing to the old Calendar class... Commented Feb 28, 2016 at 17:19
  • Are there any particularly advantages/disadvantages to one or the other, or in what way do they differ in how they function? Commented Feb 28, 2016 at 17:51
  • Calendar is clumsy to work with and is replaced by Java Time beginning with Java 8, which is a lot easier to work with. Commented Feb 28, 2016 at 18:03
  • @AlexW The legacy date-time classes such as Date, Calendar, SimpleDateFormat, etc. are bloody awful, a train wreck of poor design, written by people who did not understand the subtleties nor complexities of date-time handling. Use only their replacement, the modern java.time classes built into Java 8 and later. Commented Nov 18 at 3:26

3 Answers 3

0

I recommend using java.util.Calendar for this sort of situation. It already has constants defined for virtually every date- and time-related concept that you'll ever have to use; plus, it is smart enough to know about things like February's wishy-washy length (29 days 25% of the time).

Sign up to request clarification or add additional context in comments.

Comments

0

Java has a Class that does what you need. See the Calendar class, which also has constants for Month and Day of Week:

https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html

Comments

0

java.time

I feel that enums would be more efficient

Indeed. Java 8+ comes with just such an enum: java.time.Month.

able to "increment" the month

Access any month Enum objects by its month number, 1-12 for January-December.

Month month = Month.of( 5 ) ;  // May

… (where DEC + 1 = JAN)

You can move from one month to another with plus or minus. Use positive or negative numbers. Wraps around December-January.

Month month = Month.DECEMBER.plus( 2 ) ;  // February. 

how many days each month has

Interrogate each enum object. Because of Leap Year, you must specify a year.

int daysInFebruary = Month.FEBRUARY.length( 2025 ) ;

Or ask for the min/max number of days.

int min = Month.FEBRUARY.minLength() ;  // 28
int max = Month.FEBRUARY.maxLength() ;  // 29

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.