Java SimpleDateFormat总是返回1月份的月份 [英] Java SimpleDateFormat always returning January for Month
问题描述
我正在从Active Directory获取日期值(createWhen),并将其转换为Java日期,以获取在两个日期之间创建的帐户列表。一切正常,除了一种方法:从AD Date到Java date的方法。该方法如下所示:
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
显然,它正确地提升了日期和年份(如果我选择包括小时/分钟/秒)那些正确的),但每个日期由于某种原因被放置在一月。
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总是返回1月份的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!