date parsing problem

47 views
Skip to first unread message

senderj

unread,
Apr 18, 2012, 6:47:10 AM4/18/12
to Google App Engine
I know it doesn't sounds like a GAE problem, but it is a very simple
coding and I only encounter this problem in GAE, so I post here for
help.

My java servlet in GAE reads a web site and the problem happens when
extracting a date from html page and convert into Date object using
SimpleDateFormat.

In servlet static
static final SimpleDateFormat dff = new SimpleDateFormat("dd/MM/yyyy
HH:mm");
In servlet method
.... logic to extract date from html as text and stores to String
ss.....
try {
result = dff.parse(ss);
log.info("ss="+ss);
log.info("result="+result);
} catch (ParseException pe) {
log.warning("parse date exception");
}//end try

I've checked many times the pattern in dff is correct, and I never get
"parse date exception" in my log. Instead, here are some examples from
different runs in my log:
ss=18/04/2012 10:27
result=Fri Apr 27 10:27:00 UTC 2012
ss=18/04/2012 09:57
result=Sun May 27 09:57:00 UTC 2012
ss=18/04/2012 09:55
result=Fri May 25 09:55:00 UTC 2012
ss=18/04/2012 09:45
result=Fri Sep 18 09:45:00 UTC 2015

First of all, it is not always like that. It happens about 10% of my
runs. Secondly, I have another program reading another web page but
also parse the date with same logic in GAE, and also encountered the
same problem with about the same chances. If anybody can figure out
what's wrong with my code please help.

Simon Knott

unread,
Apr 18, 2012, 7:16:12 AM4/18/12
to google-a...@googlegroups.com
Are you aware that SimpleDateFormat is not threadsafe, so you shouldn't have it as a static variable?

senderj

unread,
Apr 18, 2012, 9:32:15 PM4/18/12
to Google App Engine
Simon, thank you so much. You resolved my problem.

On Apr 18, 7:16 pm, Simon Knott <knott.si...@gmail.com> wrote:
> Are you aware that SimpleDateFormat is not threadsafe, so you shouldn't
> have it as a static variable?
>
> You might be hitting this issue -  http://www.codefutures.com/weblog/andygrove/2007/10/simpledateformat-...
> > what's wrong with my code please help.- Hide quoted text -
>
> - Show quoted text -

Simon Knott

unread,
Apr 19, 2012, 2:30:11 AM4/19/12
to google-a...@googlegroups.com
Glad to hear it!
Reply all
Reply to author
Forward
0 new messages