Java SimpleDateFormat 总是返回一月 [英] Java SimpleDateFormat always returning January for Month
问题描述
我正在从 Active Directory 获取日期值 (createWhen),并将其转换为 Java 日期,以便获取在两个日期之间创建的帐户列表.一切正常,除了一种方法:我从 AD 日期到 Java 日期的方法.该方法如下所示:
I'm working on taking a date value (createWhen) from Active Directory, and translating it into a Java date, for the purposes of getting a list of accounts created between two dates. Everything is working fine, save for one method: the method where I go from the AD Date to the Java date. The method looks like this:
private Date getParsedDate(String givenString) {
System.out.println("Value from AD is: " + givenString);
Date parsedDate = null;
String formattedString = this.formatDateString(givenString);
System.out.println("Formatted String is: " + formattedString);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/DD");
try {
parsedDate = sdf.parse(formattedString);
System.out.println("Final date string is: " + parsedDate.toString());
} catch (ParseException ex) {
ex.printStackTrace();
}
return parsedDate;
}
而且,对于来自 AD 的单个任意数据:
And, for a single piece of arbitrary data from AD:
来自 AD 的值是:20050912190509.0Z
格式化字符串为:2005/09/12
最终日期字符串是:Wed Jan 12 00:00:00 EST 2005
显然,它正确地获取了日期和年份(如果我选择包括小时/分钟/秒,它也会正确地包括这些),但是每个日期都被放置在 1 月某种原因.
Obviously, it's picking up the day and year correctly (and if I choose to include hours/minutes/seconds it includes those correctly as well), but every single date is being placed in January for some reason.
现在,我确定我的错误很简单,但我已经重新检查了大约十次格式,现在我已经看不到它了.第二双眼睛是否有希望查看我的代码并指出我哪里出错了,月份如此严重不正确?
Now, I'm sure that my error is a pretty simple one, but I've rechecked my formatting about ten times, and I'm at the point where I just can't see it any more. Can a second pair of eyes hopefully look over my code and point out where I'm going wrong to get the month so grossly incorrect?
谢谢.
推荐答案
将模式字符串从yyyy/MM/DD"改为yyyy/MM/dd"
Change the pattern string from "yyyy/MM/DD" to "yyyy/MM/dd"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
这篇关于Java SimpleDateFormat 总是返回一月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!