Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java SimpleDateFormat always returning January for Month

Tags:

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; } 

And, for a single piece of arbitrary data from AD:

Value from AD is: 20050912190509.0Z

Formatted String is: 2005/09/12

Final date string is: 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?

Thanks.

like image 874
EricBoersma Avatar asked Aug 24 '10 19:08

EricBoersma


People also ask

What does SimpleDateFormat return?

SimpleDateFormat format() Method in Java with Examples Return Value: The method returns Date or time in string format of mm/dd/yyyy.

How do you change date format to MM DD YYYY in Java?

String date_s = " 2011-01-18 00:00:00.0"; SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss"); Date date = dt. parse(date_s); SimpleDateFormat dt1 = new SimpleDateFormat("yyyyy-mm-dd"); System. out. println(dt1.

What is the difference between DateFormat and SimpleDateFormat?

The java SimpleDateFormat allows construction of arbitrary non-localized formats. The java DateFormat allows construction of three localized formats each for dates and times, via its factory methods.

What timezone does SimpleDateFormat use?

Since it does not hold any timezone information, its toString function applies the JVM's timezone to return a String in the format, EEE MMM dd HH:mm:ss zzz yyyy , derived from this milliseconds value.


2 Answers

Change the pattern string from "yyyy/MM/DD" to "yyyy/MM/dd"

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); 
like image 55
Suhas Phartale Avatar answered Nov 07 '22 06:11

Suhas Phartale


Make sure you don't use 'mm' instead of 'MM' or 'MMM'. As small m denotes minutes and caps M denotes month.

like image 36
Ridhuvarshan Avatar answered Nov 07 '22 05:11

Ridhuvarshan