In Java, an enum is a special type of data type that allows a programmer to define a variable with a set of predefined constants. An enum can be thought of as a class that has a fixed set of constants.
To create an enum representing the months of the year in Java, you can define an enum type like this:
public enum Month { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER }
This enum defines the 12 months of the year as constants. You can use these constants in your Java code like this:
Month currentMonth = Month.MAY;
This creates a variable called currentMonth
that is set to the Month.MAY
constant. You can then use this variable in your code to perform different actions based on the current month:
if (currentMonth == Month.JANUARY) { System.out.println("Happy New Year!"); } else if (currentMonth == Month.DECEMBER) { System.out.println("Merry Christmas!"); } else { System.out.println("Have a great month!"); }
This code checks the value of the currentMonth
variable and prints a different message depending on which month it is.
Java Month enum Declaration:
In Java, an enum
is a special type of data type that is used to define a set of constants. To declare an enum
representing the months of the year, you can use the following syntax:
public enum Month { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER }
This creates an enum
called Month
with 12 constants, one for each month of the year. Each constant is declared on a separate line and is given a name in uppercase letters. In this case, the names correspond to the names of the months they represent.
You can then use this enum
in your Java code by referring to the constants by their names, like this:
Month currentMonth = Month.MAY;
This creates a variable called currentMonth
of type Month
and initializes it to the MAY
constant.
You can also use the enum
in switch statements, like this:
switch(currentMonth) { case JANUARY: // Do something for January break; case FEBRUARY: // Do something for February break; // ... case DECEMBER: // Do something for December break; }
This code checks the value of the currentMonth
variable and performs different actions based on which constant it is equal to.
enum Constants:
In Java, an enum
is a special type of data type that represents a set of predefined constants. The constants in an enum
are declared as follows:
public enum MyEnum { CONSTANT1, CONSTANT2, CONSTANT3, // ... }
Each constant is declared on a separate line, and is given a name in uppercase letters by convention. The constants are implicitly declared as static and final, meaning that they cannot be reassigned once they are initialized.
You can use the constants in an enum
in the same way you would use any other constant in Java. For example, you can assign a constant to a variable like this:
MyEnum myConstant = MyEnum.CONSTANT1;
You can compare constants for equality using the ==
operator, like this:
if (myConstant == MyEnum.CONSTANT1) { // Do something }
You can also use an enum
in a switch statement, like this:
switch(myConstant) { case CONSTANT1: // Do something for CONSTANT1 break; case CONSTANT2: // Do something for CONSTANT2 break; // ... }
This code checks the value of the myConstant
variable and performs different actions based on which constant it is equal to.
Methods of Java Month:
In Java, the Month
class is an enum that represents the months of the year. It provides a set of methods that you can use to work with months in your Java code. Some of the methods of the Month
class are:
valueOf(String name)
– Returns the enum constant of theMonth
class with the specified name.values()
– Returns an array of the enum constants of theMonth
class.getDisplayName(TextStyle style, Locale locale)
– Returns the display name of the month for the specified locale and style. Thestyle
parameter specifies how to format the display name, and thelocale
parameter specifies the locale to use for formatting.plus(long monthsToAdd)
– Returns aMonth
object that is a specified number of months after the current month.minus(long monthsToSubtract)
– Returns aMonth
object that is a specified number of months before the current month.
Here is an example that demonstrates some of these methods:
import java.time.Month; import java.util.Locale; public class MonthExample { public static void main(String[] args) { // Get the value of the FEBRUARY constant Month february = Month.valueOf("FEBRUARY"); // Get an array of all the Month constants Month[] allMonths = Month.values(); // Get the display name of the current month in the "FULL" style String displayName = february.getDisplayName(TextStyle.FULL, Locale.getDefault()); System.out.println(displayName); // Get the month that is 6 months after the current month Month august = february.plus(6); System.out.println(august); // Get the month that is 3 months before the current month Month november = february.minus(3); System.out.println(november); } }
This code gets the value of the FEBRUARY
constant, gets an array of all the Month
constants, gets the display name of the current month in the “FULL” style, gets the month that is 6 months after the current month, and gets the month that is 3 months before the current month. It then prints the results to the console.
Java Month enum Example: getValue()
In Java, the Month
class is an enum that represents the months of the year. Here is an example of how you can define a getValue()
method in a custom MonthEnum
class to get the integer value of a Month
object:
public enum MonthEnum { JANUARY(1), FEBRUARY(2), MARCH(3), APRIL(4), MAY(5), JUNE(6), JULY(7), AUGUST(8), SEPTEMBER(9), OCTOBER(10), NOVEMBER(11), DECEMBER(12); private final int value; private MonthEnum(int value) { this.value = value; } public int getValue() { return value; } }
In this example, we define an enum
called MonthEnum
that represents the months of the year, with each constant having an integer value associated with it. We also define a getValue()
method that returns the integer value of a MonthEnum
object.
To use this enum
and the getValue()
method, you can write code like this:
MonthEnum month = MonthEnum.FEBRUARY; int monthValue = month.getValue(); System.out.println("Month value: " + monthValue);
This code creates a MonthEnum
object for the month of February and then calls the getValue()
method to get the integer value associated with that month. The code then prints the value to the console, which in this case would be 2
, since February is the second month of the year.
Java Month enum Example: minus()
In Java, the Month
class is an enum that represents the months of the year. One of the methods available in the Month
class is minus()
, which can be used to subtract a specified number of months from a Month
object.
Here’s an example of how you can use the minus()
method to calculate the month that is three months before a given month:
import java.time.Month; public class MonthExample { public static void main(String[] args) { Month myMonth = Month.JUNE; Month threeMonthsAgo = myMonth.minus(3); System.out.println("My month: " + myMonth); System.out.println("Three months ago: " + threeMonthsAgo); } }
In this example, we create a Month
object representing the month of June, and then use the minus()
method to calculate the month that is three months before June. We store the result in a new Month
object called threeMonthsAgo
.
We then print the original month and the calculated month to the console. The output of this code would be:
My month: JUNE Three months ago: MARCH
So in this example, we’ve used the minus()
method to calculate the month that is three months before June, which is March.
Java Month enum Example: plus()
In Java, the Month
class is an enum that represents the months of the year. One of the methods available in the Month
class is plus()
, which can be used to add a specified number of months to a Month
object.
Here’s an example of how you can use the plus()
method to calculate the month that is three months after a given month:
import java.time.Month; public class MonthExample { public static void main(String[] args) { Month myMonth = Month.JUNE; Month threeMonthsLater = myMonth.plus(3); System.out.println("My month: " + myMonth); System.out.println("Three months later: " + threeMonthsLater); } }
In this example, we create a Month
object representing the month of June, and then use the plus()
method to calculate the month that is three months after June. We store the result in a new Month
object called threeMonthsLater
.
We then print the original month and the calculated month to the console. The output of this code would be:
My month: JUNE Three months later: SEPTEMBER
So in this example, we’ve used the plus()
method to calculate the month that is three months after June, which is September.
Java Month enum Example: length()
In Java, the Month
class is an enum that represents the months of the year. One of the methods available in the Month
class is length()
, which can be used to get the number of days in a month.
Here’s an example of how you can use the length()
method to get the number of days in a given month:
import java.time.Month; public class MonthExample { public static void main(String[] args) { Month myMonth = Month.JUNE; int daysInMonth = myMonth.length(); System.out.println("My month: " + myMonth); System.out.println("Days in month: " + daysInMonth); } }
In this example, we create a Month
object representing the month of June, and then use the length()
method to get the number of days in June. We store the result in an int
variable called daysInMonth
.
We then print the original month and the number of days in the month to the console. The output of this code would be:
My month: JUNE Days in month: 30
So in this example, we’ve used the length()
method to get the number of days in June, which is 30.