7

I have run into a stubborn problem I cannot seem to solve. I have looked for solutions at stackoverflow and have found a lot of posts about Java date formatting, but nothing specific to the problem I have.

Basically, I have a situation where I need to convert date strings into java.util.Date objects. I am using Date and SimpleDateFormat classes. For most dates that I am encountering, it works just fine. But for some dates, it works but changes the actual date. Two example dates that are :

Fri Feb 24 16:45:40 PST 2012 --> gets changed to --> Fri Jan 06 16:45:40 PST 2012

Wed Jun 13 10:00:42 PDT 2012 --> gets changed to --> Wed Jan 04 09:00:42 PST 2012

Any idea why the dates are getting changed? Any way to easily avoid this or do it in a different way? My code is copied below. You can try it to see what I am talking about.

Thanks in advance!

You can try this with the following JSP code:

<%@ page import="java.util.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.text.*" %>

<%
String dateStr = "";
Date tmpDate = null;
DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z YYYY");

System.out.println("First Test ---------------");
dateStr = "Fri Feb 24 16:45:40 PST 2012";
tmpDate = (Date) formatter.parse(dateStr);
System.out.println("Original:"+dateStr+":");
System.out.println("Date Obj:"+tmpDate.toString()+":");

System.out.println("Second Test --------------");
dateStr = "Wed Jun 13 10:00:42 PDT 2012";
tmpDate = (Date) formatter.parse(dateStr);
System.out.println("Original:"+dateStr+":");
System.out.println("Date Obj:"+tmpDate.toString()+":");

%>

I am getting the following output:

First Test ------------
Original:Fri Feb 24 16:45:40 PST 2012:
Date Obj:Fri Jan 06 16:45:40 PST 2012:
Second Test -----------
Original:Wed Jun 13 10:00:42 PDT 2012:
Date Obj:Wed Jan 04 09:00:42 PST 2012:
rc1
  • 483
  • 4
  • 19
  • 1
    It's as though Y and y are different characters or something. It shouldn't even compile the date pattern, though, at least it won't for me. – Dave Newton Feb 07 '13 at 02:46
  • +1 For a well written question. – Paul Bellora Feb 07 '13 at 02:48
  • I should have preferred the JSP left out from the code in the question if I am correct that the problem can readily be reproduced in pure Java (also @PaulBellora). Otherwise the question is fine, MCVE and everything. – Ole V.V. May 31 '17 at 14:35
  • Related: [How to convert “Fri Sep 21 15:23:59 CEST 2012” to “2012-09-21T15:23:59” in Java?](https://stackoverflow.com/questions/12532736/how-to-convert-fri-sep-21-152359-cest-2012-to-2012-09-21t152359-in-java) – Ole V.V. May 31 '17 at 15:03

2 Answers2

8

Use yyyy not YYYY in your format string.

YYYY is a very special thing, the calendar week year.

See the SimpleDateFormat documentation for more info.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • Ah, brilliant observation. I had looked at the documentation, but not sure why I used YYYY, though the uppercase option is not even listed in the SimpleDateFormat Javadoc. For anyone else that comes across this, the following formatting worked for me: new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); – rc1 Feb 07 '13 at 06:55
  • @Mohammad yes, it is a pitfall, bad design from the ISO Norm that defines these formatting symbols. This really is counter intiuitve. That's the reason why every 5 days such a question comes up here at SO. – AlexWien Feb 07 '13 at 16:34
3

You're using YYYY in your format specifier, which is week year (See SimpleDateFormat). You need yyyy, which is just "year".

I suspect the wrong result was out because you also specified the month and day, which aren't really "features" in the week year. When you use week year, you'd specify the "week of week year" and "day of week", it might have given some more sensible results, but obviously you didn't really meant to use week years.

I recommend you to specify your Locale in your code. Of course it will run perfectly on your machine, but it may cause an unparsable date exception somewhere else like China.

DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
Community
  • 1
  • 1
StarPinkER
  • 14,081
  • 7
  • 55
  • 81
  • 1
    Thanks a lot for the explanation. I realize I had overlooked the Locale importance. – rc1 Feb 07 '13 at 07:00