ZetCode

Java Month 枚举

最后修改时间:2025 年 4 月 16 日

java.time.Month 枚举表示一年中的十二个月。 它是 Java 8 中引入的 Java Date and Time API 的一部分。每个枚举常量都提供了用于处理月份的有用方法。

Month 是不可变的且线程安全的。 它提供了获取月份名称、长度和值的方法。 该枚举与 LocalDateYearMonth 等其他日期时间类无缝协作。

Month 枚举概述

Month 枚举包含从 JANUARY 到 DECEMBER 的十二个常量。 每个常量都有查询月份属性和执行转换的方法。 该枚举遵循 ISO-8601 日历系统。

public enum Month implements TemporalAccessor, TemporalAdjuster {
    JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
    JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
    
    public int getValue();
    public String getDisplayName(TextStyle style, Locale locale);
    public int length(boolean leapYear);
    public int minLength();
    public int maxLength();
    public Month firstMonthOfQuarter();
    public Month plus(long months);
    public Month minus(long months);
}

上面的代码显示了 Month 枚举的结构和关键方法。 这些方法允许查询月份属性并执行计算。 该枚举提供了月份的数值和文本表示形式。

获取月份值

每个 Month 常量都有一个数值,从 1 (一月) 到 12 (十二月)。 getValue 方法返回此值。 这对于与使用月份数字的旧 API 兼容很有用。

Main.java
package com.zetcode; 

import java.time.Month;

public class Main {

    public static void main(String[] args) {
        
        // Get month values
        System.out.println("January value: " + Month.JANUARY.getValue());
        System.out.println("December value: " + Month.DECEMBER.getValue());
        
        // Get month from value
        Month month = Month.of(6);
        System.out.println("Month 6: " + month);
        
        // Get current month
        Month current = Month.from(java.time.LocalDate.now());
        System.out.println("Current month: " + current);
    }
}

此示例演示了 Month 值的基本操作。 of 方法根据其数值创建一个 Month。 from 方法从其他日期时间对象中提取 Month。

显示月份名称

getDisplayName 方法以各种文本样式提供本地化的月份名称。 这对于需要在不同格式和语言中显示月份名称的用户界面很有用。

Main.java
package com.zetcode; 

import java.time.Month;
import java.time.format.TextStyle;
import java.util.Locale;

public class Main {

    public static void main(String[] args) {
        
        // Full name in English
        String fullName = Month.AUGUST.getDisplayName(
            TextStyle.FULL, Locale.ENGLISH);
        System.out.println("Full name: " + fullName);
        
        // Short name in French
        String shortName = Month.AUGUST.getDisplayName(
            TextStyle.SHORT, Locale.FRENCH);
        System.out.println("Short name in French: " + shortName);
        
        // Narrow name in German
        String narrowName = Month.AUGUST.getDisplayName(
            TextStyle.NARROW, Locale.GERMAN);
        System.out.println("Narrow name in German: " + narrowName);
    }
}

此示例显示了显示月份名称的不同方法。 TextStyle 枚举控制名称格式 (FULL、SHORT、NARROW)。 Locale 参数提供本地化支持。

处理月份长度

Month 枚举提供了查询月份长度的方法。 这些方法对于日期计算和验证至关重要。 二月的长度取决于是否为闰年。

Main.java
package com.zetcode; 

import java.time.Month;

public class Main {

    public static void main(String[] args) {
        
        // Regular month lengths
        System.out.println("January days: " + Month.JANUARY.length(false));
        System.out.println("April days: " + Month.APRIL.length(false));
        
        // February in leap year
        System.out.println("February in leap year: " + 
            Month.FEBRUARY.length(true));
            
        // Min and max lengths
        System.out.println("February min days: " + 
            Month.FEBRUARY.minLength());
        System.out.println("February max days: " + 
            Month.FEBRUARY.maxLength());
    }
}

此示例演示了月份长度查询。 大多数月份的长度是固定的,但二月有所不同。 minLengthmaxLength 方法提供了二月长度的边界。

月份算术

Month 枚举通过 plusminus 方法支持算术运算。 这些操作在年份边界处环绕,使其对基于月份的计算很有用。

Main.java
package com.zetcode; 

import java.time.Month;

public class Main {

    public static void main(String[] args) {
        
        // Basic arithmetic
        Month nextMonth = Month.JANUARY.plus(1);
        System.out.println("January + 1: " + nextMonth);
        
        Month prevMonth = Month.JANUARY.minus(1);
        System.out.println("January - 1: " + prevMonth);
        
        // Wrapping around year boundary
        Month decPlus2 = Month.DECEMBER.plus(2);
        System.out.println("December + 2: " + decPlus2);
        
        Month janMinus3 = Month.JANUARY.minus(3);
        System.out.println("January - 3: " + janMinus3);
        
        // Large values
        Month janPlus25 = Month.JANUARY.plus(25);
        System.out.println("January + 25: " + janPlus25);
    }
}

此示例显示了月份算术运算。 计算在年份边界自动环绕。 将 1 加到 12 月得到 1 月,从 1 月减去 1 得到 12 月。

处理季度

firstMonthOfQuarter 方法返回每个季度的第一个月。 这对于按季度组织时间的金融和商业应用程序很有用。

Main.java
package com.zetcode; 

import java.time.Month;

public class Main {

    public static void main(String[] args) {
        
        // Get first month of quarters
        System.out.println("Q1 starts with: " + 
            Month.JANUARY.firstMonthOfQuarter());
        System.out.println("Q2 starts with: " + 
            Month.APRIL.firstMonthOfQuarter());
        System.out.println("Q3 starts with: " + 
            Month.JULY.firstMonthOfQuarter());
        System.out.println("Q4 starts with: " + 
            Month.OCTOBER.firstMonthOfQuarter());
            
        // Any month in quarter returns same result
        System.out.println("May's quarter starts with: " + 
            Month.MAY.firstMonthOfQuarter());
    }
}

此示例演示了与季度相关的操作。 每个季度由三个月组成,该方法返回第一个月,无论用于调用它的季度中的哪个月。

将 Month 与 LocalDate 结合使用

Month 枚举与 LocalDate 无缝协作。 这种组合对于日期操作和查询非常强大。 这些示例显示了这些类之间的常见操作。

Main.java
package com.zetcode; 

import java.time.LocalDate;
import java.time.Month;
import java.time.Year;

public class Main {

    public static void main(String[] args) {
        
        // Create date with Month enum
        LocalDate date1 = LocalDate.of(2025, Month.JANUARY, 15);
        System.out.println("Date with Month enum: " + date1);
        
        // Get month from date
        Month month = date1.getMonth();
        System.out.println("Month from date: " + month);
        
        // Create date at start of month
        LocalDate startOfMonth = Year.now().atMonth(Month.MARCH).atDay(1);
        System.out.println("Start of March: " + startOfMonth);
        
        // Check if month has 31 days
        boolean has31Days = month.maxLength() == 31;
        System.out.println(month + " has 31 days: " + has31Days);
    }
}

此示例显示了 MonthLocalDate 之间的集成。 创建日期时,Month 枚举提供类型安全。 该示例还演示了从日期查询月份属性。

来源

Java Month 枚举文档

在本文中,我们介绍了 Java Month 枚举的基本方法和功能。 了解这些概念对于现代 Java 应用程序中的日期处理至关重要。

作者

我叫 Jan Bodnar,是一名敬业的程序员,在该领域拥有多年的经验。 我于 2007 年开始撰写编程文章,此后撰写了 1,400 多篇文章和八本电子书。 凭借超过八年的教学经验,我致力于分享我的知识并帮助他人掌握编程概念。

列出所有Java教程