【问题标题】:Month in simple date formatter always return JANUARY [duplicate]简单日期格式化程序中的月份总是返回 JANUARY [重复]
【发布时间】:2018-01-10 09:50:58
【问题描述】:

我使用简单的日期格式将字符串转换为日期,但日期的月份总是返回 JANUARY 这是我的代码:

  public static void main(String args[]){

    String date = "2017-12-29";
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
    SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM dd, YYYY");
    try {
        Date startEntryDate = formatter.parse(date);
        System.out.println(dateFormatter.format(startEntryDate));

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

【问题讨论】:

标签: java date simpledateformat


【解决方案1】:

如前所述,您必须使用MM 而不是mm,但是?
为什么不使用java.time 库:

String month = LocalDate.parse("2017-12-29").getMonth().name();

【讨论】:

  • @OleV.V.检查the ideone demo 名称来自Enum class
  • 啊,Enum.name(),当然,我的错。
  • 正是@OleV.V.
  • 当然好消息是,在这种情况下,1-arg LocalDate.parse() 让我们完全不用使用显式格式化程序进行解析。由于这些格式化程序似乎很容易出错,因此这比人们最初想象的更有价值。
  • 这是正确的@OleV.V。 :)
【解决方案2】:

尝试使用

new SimpleDateFormat("yyyy-MM-dd");

mm 表示分钟。 MM - 个月。

Letter   Date or Time Component   Presentation       Examples
G        Era designator             Text                AD
y        Year                       Year                1996;    96
M        Month in year              Month               July; Jul; 07
w        Week in year               Number               27
W        Week in month              Number               2
D        Day in year                Number               189
d        Day in month               Number               10
F        Day of week in month       Number               2
E        Day in week                Text               Tuesday; Tue
a        Am/pm marker               Text               PM
H        Hour in day (0-23)         Number               0
k        Hour in day (1-24)         Number               24
K        Hour in am/pm (0-11)       Number               0
h        Hour in am/pm (1-12)       Number               12
m        Minute in hour             Number               30
s        Second in minute           Number               55
S        Millisecond                Number               978
z        Time zone                  General time zone   Pacific Standard Time; PST; GMT-08:00
Z        Time zone                  RFC 822 time zone   -0800

【讨论】:

    【解决方案3】:

    试试这个:

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM dd, yyyy");
    

    【讨论】:

      【解决方案4】:
         Try this:
      
              new SimpleDateFormat("MM/dd/yyyy")
              MM is "month" (not mm)
              dd is "day" (not DD)
              It's all in the javadoc for SimpleDateFormat
      
      
              FYI, the reason your format is still a valid date format is that:
      
              mm is "minutes"
              DD is "day in year"
              Also, you don't need the cast to Date... it already is a Date (or it 
              explodes):
      
                  public static void main(String[] args) throws ParseException {
               System.out.println(new 
                  SimpleDateFormat("MM/dd/yyyy").parse("08/16/2011"));
                  }
              Output:
      

      2011 年 8 月 16 日星期二 00:00:00 EST

      【讨论】:

        【解决方案5】:

        试试这个:

            String date = "2017-8-28";
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM dd, YYYY");
            try {
                Date startEntryDate = formatter.parse(date);
                System.out.println(dateFormatter.format(startEntryDate));
        
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        

        【讨论】:

        • 纯代码答案很少有帮助。请解释您与 OP 的代码相比所做的更改,特别是:它如何提供帮助,
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-02
        • 1970-01-01
        • 2019-08-19
        • 1970-01-01
        相关资源
        最近更新 更多