gpt4 book ai didi

java - 使用 MMMM 和 MMMMM 使用 SimpleDateFormat 提取月份名称有什么区别

转载 作者:行者123 更新时间:2023-12-02 01:19:08 25 4
gpt4 key购买 nike

我想答案是否定的。如果我使用 4 个或更多“M”,结果是相同的。

public static void main (String[] args) 
{
for (int monthNum = 1; monthNum <= 12; monthNum++)
{
String month = new SimpleDateFormat("MMMM", Locale.US)
.format (new Date (1, monthNum, 2000));

String month2 = new SimpleDateFormat("MMMMM", Locale.US)
.format (new Date (1, monthNum, 2000));

if (month.equals(month2))
continue;

throw new RuntimeException ("");
}
}

成功结束。

但是我不明白为什么 examplesSimpleDateFormat特别使用 5 'M' 而不是 4 或任何其他 >=4 的数字。关于模式字符串中给出的数字“M”的结果的唯一解释是:

Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.

那有什么区别吗?为什么不更好地记录它呢?

最佳答案

再看一下您引用的 javadoc 片段:

Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.

单击文档中“文本”后面的链接。它让您回到上一点:

Text: For formatting, if the number of pattern letters is 4 or more, the full form is used; otherwise a short or abbreviated form is used if available. For parsing, both forms are accepted, independent of the number of pattern letters.

因此,如果它是 4 个字母或更多,则使用完整形式,否则使用缩写形式。包含 5 个字母的示例属于“4 个或更多字母”类别。

关于java - 使用 MMMM 和 MMMMM 使用 SimpleDateFormat 提取月份名称有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16400907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com