Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Date object from String not working properly

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:
like image 816
rc1 Avatar asked Feb 07 '13 02:02

rc1


2 Answers

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.

like image 148
AlexWien Avatar answered Sep 23 '22 02:09

AlexWien


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);
like image 44
StarPinkER Avatar answered Sep 20 '22 02:09

StarPinkER