ZetCode

Java FormatStyle 枚举

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

java.time.format.FormatStyle 枚举定义了四种日期和时间的格式化风格。这些风格与 DateTimeFormatter 一起使用,用于控制日期和时间的显示方式。该枚举提供了标准化的格式化选项。

FormatStyle 值表示格式化的不同详细程度。它们范围从 full(最详细)到 short(最紧凑)。该枚举主要用于日期和时间的区域设置敏感格式化。

FormatStyle 枚举概述

FormatStyle 枚举包含四个常量:FULLLONGMEDIUMSHORT。每种风格在格式化输出中提供不同的详细程度。实际格式因区域设置而异。

public enum FormatStyle {
    FULL,
    LONG,
    MEDIUM,
    SHORT
}

上面的代码展示了 FormatStyle 枚举的简单结构。尽管它很简单,但在日期时间格式化中起着至关重要的作用。这些风格旨在满足应用程序中的常见格式化需求。

FormatStyle.FULL 示例

FULL 提供最详细的格式化,包括星期几、月份、日、年和时区信息。它适用于需要最大清晰度的正式场合。

Main.java
package com.zetcode;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

public class Main {

    public static void main(String[] args) {
        
        LocalDateTime now = LocalDateTime.now();
        
        DateTimeFormatter formatter = DateTimeFormatter
            .ofLocalizedDateTime(FormatStyle.FULL)
            .withLocale(Locale.US);
            
        String formatted = now.format(formatter);
        System.out.println("FULL format: " + formatted);
    }
}

此示例演示了使用美国区域设置的 FormatStyle.FULL。输出包括所有可能的日期和时间组件。确切的格式取决于指定的区域设置。

FormatStyle.LONG 示例

LONG 提供的细节少于 FULL,但多于 MEDIUM。它通常包括月份名称、日期、年份和带秒的时间。可能包含或不包含时区。

Main.java
package com.zetcode;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

public class Main {

    public static void main(String[] args) {
        
        LocalDateTime now = LocalDateTime.now();
        
        DateTimeFormatter formatter = DateTimeFormatter
            .ofLocalizedDateTime(FormatStyle.LONG)
            .withLocale(Locale.FRANCE);
            
        String formatted = now.format(formatter);
        System.out.println("LONG format (France): " + formatted);
    }
}

此示例显示了使用法国区域设置的 FormatStyle.LONG。输出比 FULL 更紧凑,但仍然非常可读。注意法语中不同的日期顺序和月份名称。

FormatStyle.MEDIUM 示例

MEDIUM 在细节和紧凑性之间取得平衡。它通常显示缩写的月份名称、数字的日和年以及带秒的时间。这是许多应用程序的常见选择。

Main.java
package com.zetcode;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

public class Main {

    public static void main(String[] args) {
        
        LocalDateTime now = LocalDateTime.now();
        
        DateTimeFormatter formatter = DateTimeFormatter
            .ofLocalizedDateTime(FormatStyle.MEDIUM)
            .withLocale(Locale.GERMANY);
            
        String formatted = now.format(formatter);
        System.out.println("MEDIUM format (Germany): " + formatted);
    }
}

此示例演示了使用德国区域设置的 FormatStyle.MEDIUM。输出比 LONG 更紧凑,但仍然包含关键信息。请注意德国使用的不同日期格式和时间分隔符。

FormatStyle.SHORT 示例

SHORT 提供最紧凑的格式化,主要使用数字值。它适用于空间有限或精确度不关键的情况。

Main.java
package com.zetcode;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

public class Main {

    public static void main(String[] args) {
        
        LocalDateTime now = LocalDateTime.now();
        
        DateTimeFormatter formatter = DateTimeFormatter
            .ofLocalizedDateTime(FormatStyle.SHORT)
            .withLocale(Locale.JAPAN);
            
        String formatted = now.format(formatter);
        System.out.println("SHORT format (Japan): " + formatted);
    }
}

此示例显示了使用日本区域设置的 FormatStyle.SHORT。输出非常紧凑,仅使用数字和最少的分隔符。请注意日本使用的不同日期顺序(年/月/日)。

仅格式化日期

FormatStyle 可用于格式化日期,而无需时间。ofLocalizedDate 方法创建一个忽略时间组件的格式化程序。

Main.java
package com.zetcode;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

public class Main {

    public static void main(String[] args) {
        
        LocalDate today = LocalDate.now();
        
        DateTimeFormatter formatter = DateTimeFormatter
            .ofLocalizedDate(FormatStyle.LONG)
            .withLocale(Locale.UK);
            
        String formatted = today.format(formatter);
        System.out.println("Date only (UK): " + formatted);
    }
}

此示例演示了使用 FormatStyle.LONG 和英国区域设置的仅日期格式化。输出包括完整的月份名称,但不包含时间信息。英国格式使用日/月/年顺序。

仅格式化时间

同样,FormatStyle 可以格式化时间,而无需日期。ofLocalizedTime 方法创建一个仅时间格式化程序。

Main.java
package com.zetcode;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

public class Main {

    public static void main(String[] args) {
        
        LocalTime now = LocalTime.now();
        
        DateTimeFormatter formatter = DateTimeFormatter
            .ofLocalizedTime(FormatStyle.MEDIUM)
            .withLocale(Locale.CANADA_FRENCH);
            
        String formatted = now.format(formatter);
        System.out.println("Time only (French Canada): " + formatted);
    }
}

此示例显示了使用 FormatStyle.MEDIUM 和加拿大法语区域设置的仅时间格式化。输出包括小时、分钟和秒。请注意法语区使用的 24 小时制。

混合 FormatStyles

不同的 FormatStyle 值可以分别用于日期和时间组件。这提供了创建自定义格式的灵活性。

Main.java
package com.zetcode;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

public class Main {

    public static void main(String[] args) {
        
        LocalDateTime now = LocalDateTime.now();
        
        DateTimeFormatter formatter = DateTimeFormatter
            .ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.LONG)
            .withLocale(Locale.ITALY);
            
        String formatted = now.format(formatter);
        System.out.println("Mixed styles (Italy): " + formatted);
    }
}

此示例演示了使用意大利区域设置的日期使用 FormatStyle.SHORT 和时间使用 FormatStyle.LONG 的混合。结果将紧凑的日期与详细的时间格式相结合。

来源

Java FormatStyle 枚举文档

在本文中,我们介绍了 Java FormatStyle 枚举的各个方面。这些格式化选项在 Java 应用程序中提供了灵活的、区域设置感知的日期和时间表示。

作者

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

列出所有Java教程