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: