Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Jim Janney Newsgroups: comp.lang.java.programmer Subject: Re: Date parsing always returning the month of January Date: Wed, 29 Feb 2012 15:30:07 -0700 Organization: the cement's just for the weight, dear Lines: 29 Message-ID: <2pwr751de8.fsf@shell.xmission.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx04.eternal-september.org; posting-host="PnllQd880uOddfy6hsxHuQ"; logging-data="19804"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19HDLWgw/NUCKTbkQChRUnp" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:/lLNVd003OdSRW+dsCEswoz0T9w= sha1:lONEHHlVqQBCGfpJT9SAvQ3nG2o= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:12544 laredotornado writes: > Hi, > > I'm using Java 6. Hoping someone can see the simple error I have > missed. I'm trying to parse a date. I have > > final String formattedStr = "2012-03-01 01:30:00"; > final String format = "yyyy-MM-DD HH:mm:ss"; > final SimpleDateFormat sdf = new SimpleDateFormat(format); > try { > ret = sdf.parse(formattedStr); > } catch (ParseException e) { > e.printStackTrace(); > } > String retStr = ret.toString(); > > The value of "retStr" is "Sun Jan 01 01:30:00 CST 2012". In fact, the > month of the date string is always getting converted to January, even > though above it shoudl be March. Where is my date parsing going > wrong? This is only indirectly related to your question, but I recommend calling setLenient(false) any time you use a DateFormat for parsing. Too easy to get bogus results otherwise. -- Jim Janney