Java SignStyle 枚举
最后修改时间:2025 年 4 月 16 日
java.time.format.SignStyle
枚举控制格式化数字的符号输出。它与 DateTimeFormatter
一起使用,用于指定年或偏移量等字段的符号处理。该枚举提供了五种不同的符号表示样式。
SignStyle
是 Java 8 日期和时间 API 的一部分。它有助于一致地格式化数字字段。这些样式从始终显示符号到仅在必要时显示负号不等。
SignStyle 枚举概述
SignStyle
枚举定义了五个常量,用于控制符号格式化行为。这些样式决定了格式化输出中正号和负号何时出现。该枚举与字段格式化程序一起使用。
public enum SignStyle { NORMAL, ALWAYS, NEVER, NOT_NEGATIVE, EXCEEDS_PAD }
上面的代码显示了 SignStyle
的五个常量。每个常量代表一种不同的策略,用于在格式化期间处理符号。这些样式在严格性和用例上有所不同。
NORMAL 样式示例
SignStyle.NORMAL
仅为负数输出符号。这是常规数字格式化的最常见样式。它提供干净的输出,而无需不必要的正号。
import java.time.Year; import java.time.format.DateTimeFormatter; import java.time.format.SignStyle; public class Main { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu") .withSignStyle(SignStyle.NORMAL); System.out.println("Positive year: " + formatter.format(Year.of(2025))); System.out.println("Negative year: " + formatter.format(Year.of(-2025))); System.out.println("Zero year: " + formatter.format(Year.of(0))); } }
此示例显示了 NORMAL
符号样式的行为。正年份和零年份没有符号,而负年份显示一个负号。这与典型的数字格式化约定相符。
ALWAYS 样式示例
SignStyle.ALWAYS
强制为正数和负数都显示符号。当需要明确的符号指示时,无论数字的值如何,此样式都很有用。
import java.time.Year; import java.time.format.DateTimeFormatter; import java.time.format.SignStyle; public class Main { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu") .withSignStyle(SignStyle.ALWAYS); System.out.println("Positive year: " + formatter.format(Year.of(2025))); System.out.println("Negative year: " + formatter.format(Year.of(-2025))); System.out.println("Zero year: " + formatter.format(Year.of(0))); } }
此示例演示了 ALWAYS
样式的行为。所有年份都显示其符号,包括正值和零值。正数出现加号,保持完整的符号信息。
NEVER 样式示例
SignStyle.NEVER
阻止显示任何符号,即使对于负数也是如此。当输出中不需要符号时,无论值的符号如何,此样式都很有用。
import java.time.Year; import java.time.format.DateTimeFormatter; import java.time.format.SignStyle; public class Main { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu") .withSignStyle(SignStyle.NEVER); System.out.println("Positive year: " + formatter.format(Year.of(2025))); System.out.println("Negative year: " + formatter.format(Year.of(-2025))); System.out.println("Zero year: " + formatter.format(Year.of(0))); } }
此示例显示了 NEVER
样式的行为。输出中不显示任何符号,即使对于负年份也是如此。显示绝对值,没有任何符号指示。
NOT_NEGATIVE 样式示例
如果值为负数,则 SignStyle.NOT_NEGATIVE
抛出异常。它只允许格式化零或正值。这在格式化期间充当验证检查。
import java.time.Year; import java.time.format.DateTimeFormatter; import java.time.format.SignStyle; public class Main { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu") .withSignStyle(SignStyle.NOT_NEGATIVE); try { System.out.println("Positive year: " + formatter.format(Year.of(2025))); System.out.println("Zero year: " + formatter.format(Year.of(0))); System.out.println("Negative year: " + formatter.format(Year.of(-2025))); } catch (Exception e) { System.out.println("Error formatting negative year: " + e); } } }
此示例演示了 NOT_NEGATIVE
样式的行为。正年份和零年份正常格式化,但负年份抛出异常。此样式在格式化输出中强制使用非负值。
EXCEEDS_PAD 样式示例
SignStyle.EXCEEDS_PAD
仅在数字超过填充宽度时显示符号。此样式与填充一起使用,并为较小的数字提供紧凑的输出。
import java.time.Year; import java.time.format.DateTimeFormatter; import java.time.format.SignStyle; public class Main { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu") .withSignStyle(SignStyle.EXCEEDS_PAD); System.out.println("Year 999: " + formatter.format(Year.of(999))); System.out.println("Year 1000: " + formatter.format(Year.of(1000))); System.out.println("Year -100: " + formatter.format(Year.of(-100))); System.out.println("Year -1000: " + formatter.format(Year.of(-1000))); } }
此示例显示了 EXCEEDS_PAD
样式的行为。符号仅在数字的位数超过格式的宽度时才出现。对于 4 位数的年份,匹配默认宽度,因此会显示符号。
与填充结合的示例
SignStyle
经常与格式模式中的填充一起使用。填充和符号样式之间的交互会影响最终输出。此示例演示了这种组合。
import java.time.Year; import java.time.format.DateTimeFormatter; import java.time.format.SignStyle; public class Main { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("+uuuu") .withSignStyle(SignStyle.EXCEEDS_PAD); System.out.println("Padded year 25: " + formatter.format(Year.of(25))); System.out.println("Padded year 2025: " + formatter.format(Year.of(2025))); System.out.println("Padded negative year: " + formatter.format(Year.of(-25))); } }
此示例将填充与符号样式结合使用。模式中的 +
表示填充,而 EXCEEDS_PAD
控制符号行为。输出显示了符号如何与填充的数字交互。
与偏移量字段一起使用示例
SignStyle
在格式化偏移量字段时特别有用。时区偏移需要一致的符号处理。此示例显示了具有不同符号样式的偏移量格式化。
import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.time.format.SignStyle; public class Main { public static void main(String[] args) { DateTimeFormatter normalFormatter = DateTimeFormatter.ofPattern("xxx") .withSignStyle(SignStyle.NORMAL); DateTimeFormatter alwaysFormatter = DateTimeFormatter.ofPattern("xxx") .withSignStyle(SignStyle.ALWAYS); ZoneOffset utc = ZoneOffset.UTC; ZoneOffset plusFive = ZoneOffset.ofHours(5); ZoneOffset minusFive = ZoneOffset.ofHours(-5); System.out.println("UTC (normal): " + normalFormatter.format(utc)); System.out.println("+05:00 (normal): " + normalFormatter.format(plusFive)); System.out.println("-05:00 (normal): " + normalFormatter.format(minusFive)); System.out.println("UTC (always): " + alwaysFormatter.format(utc)); System.out.println("+05:00 (always): " + alwaysFormatter.format(plusFive)); System.out.println("-05:00 (always): " + alwaysFormatter.format(minusFive)); } }
此示例演示了具有时区偏移量的符号样式。UTC(零偏移量)根据符号样式显示不同的行为。ALWAYS
样式即使对于零偏移量也会强制显示符号。
来源
在本文中,我们涵盖了 Java SignStyle
枚举的所有五个常量。了解这些样式对于精确控制日期和时间操作中的数字格式化至关重要。
作者
列出所有Java教程。