SimpleDateFormatter无法识别月份 [英] SimpleDateFormatter does not recognize months

查看:111
本文介绍了SimpleDateFormatter无法识别月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析一个日期字符串,但失败了. 为了说明我的问题,我编写了一个简单的JUnit测试:

I want to parse a date string but I fail miserably. To illustrate my problem I wrote this simple JUnit test:

@Test
public void testParseJavaDate() throws ParseException {     
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD_HH-mm-ss", Locale.GERMAN);

    String inputtime = "2011-04-21_16-01-08";
    Date parse = sdf.parse(inputtime);

    assertEquals(inputtime,sdf.format(parse));
}

此测试失败,并显示以下消息:

This test fails with this message:

org.junit.ComparisonFailure: 预期:< 2011-0 [4] -21_16-01-08> 但是是:< 2011-0 [1] -21_16-01-08>

org.junit.ComparisonFailure: expected:<2011-0[4]-21_16-01-08> but was:<2011-0[1]-21_16-01-08>

我不明白为什么格式化程序无法正确解析日期.你有什么主意吗?

I don't get why the formatter cannot parse the date correctly. Do you have any ideas?

推荐答案

您要的是"dd"(每月的天数),而不是"DD"(每年的天数):

You want "dd" (day in month), not "DD" (day in year):

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.GERMAN);

这篇关于SimpleDateFormatter无法识别月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆