stepanian stepanian - 1 month ago 12
Java Question

JSP - Date formatting and Google cache

I want to display a formatted date on my JSP page, so I use:

<fmt:formatDate pattern="MMM d" value="${myEvent.date}"/>


It works perfectly. It is displayed on the page as

Nov 28


However, a strange thing happens when it is cached by Google - the date on the cached page is displayed like this:

2016-11-28 20:00:00.0


Can anyone explain this? Shouldn't the formatting happen on the server? Doesn't my application server (Tomcat) send only the HTML to the page? How does Google know that this was in fact a Java date in the first place?

Just to clarify, to get to the cached version of the page, I perform a search on Google that displays my page in the results and I click on the down arrow and get to the cached version of my page.

Answer

Apparently, when a JSP is requested without the "Accept-Language" HTTP header, the JSTL formatDate tag does not format the date and the result of toString() of the Date object is returned instead.

You can verify this by using cUrl to get the page source (instead of viewing the page source in Chrome).

Bottom line: If you you are using the JSTL formatDate tag and you want your dates to be formatted in Google's cache, you should explicitly set the locale. For example:

<fmt:setLocale value="en_US" />

I hope this helps someone.

Comments