Discussion:
[FreeMarker-user] Freemarker Date Format issue
FOX427
10 years ago
Permalink
Hi All, I am getting date from xml node in this format: Mon, 02 Mar 2015
14:35:47 +0000.
I need to present this as March 2, 2015. So I tried this:

<#assign publishedDate=item.pubDate?datetime("a, d MMM yyyy hh:mm:ss zzz")>
${publishedDate?string("yyyy")}<br>

So as you can see, I just wanted to extract year first and if it works I
would add day and month. But it is giving me error message: Unparseable
date: "Mon, 02 Mar 2015 14:35:47 +0000". It seems I am actually having the
date value but unable to parse it, can someone give me some advice how to do
it correctly? Thank you in advance.



--
View this message in context: http://freemarker.624813.n4.nabble.com/Freemarker-Date-Format-issue-tp4655381.html
Sent from the freemarker-user mailing list archive at Nabble.com.
FOX427
10 years ago
Permalink
Ok, so I found solution, here is what I did:

<#assign starting_point = item.pubDate?index_of(",")>
<#assign date="${item.pubDate?substring(starting_point + 1)}" />
${date?datetime("dd MMM yyyy hh:mm:ss z")?date}<br>

So now I am getting result as this: Mar 2, 2015.

I still have question, is there any way we can change value from Mar to
March?



--
View this message in context: http://freemarker.624813.n4.nabble.com/Freemarker-Date-Format-issue-tp4655381p4655382.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
10 years ago
Permalink
There are a few mistakes in that pattern... should be:

EEE, d MMM yyyy HH:mm:ss Z

Also, because EEE is language dependent, be sure your locale setting
is English. If you aren't sure about that, better start the template
with something like:

<#setting locale="en_US">

Last not least, if there's any remote chance, tell whoever generates
that XML to use ISO 8601, or better yet, xs:dateTime format (in which
case you will use ?datetime.iso or ?datetime.xs, respectively).
--
Thanks,
Daniel Dekany
...
FOX427
10 years ago
Permalink
Thank you Daniel! i will definitely use your advice! One question though,
what is EEE? I know that I will get "Mon" in my case and if i use EEEEEE I
will get "Monday", so does that mean I can use E value for each character
that I don't need to use but have to declare so freemarker won't complain?

Overall, I like your response because i am learning new stuff here. Also, I
still don't know how to change Mar to March, I could use if/elseif
statements and assign full month names to short three letter month names but
I think there are better ways to do it. Anyway, thanks for your help again.



--
View this message in context: http://freemarker.624813.n4.nabble.com/Freemarker-Date-Format-issue-tp4655381p4655384.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
10 years ago
Permalink
Post by FOX427
Thank you Daniel! i will definitely use your advice! One question though,
what is EEE?
Um... right, one E is enough, and for parting it works both for Mon
and Monday. See:
http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

"For formatting, if the number of pattern letters is 4 or more, the
full form is used; otherwise a short or abbreviated form is used if
available. For parsing, both forms are accepted, independent of the
number of pattern letters."
Post by FOX427
I know that I will get "Mon" in my case and if i use EEEEEE I
will get "Monday",
Or if you use EEEE.
Post by FOX427
so does that mean I can use E value for each character that I don't
need to use but have to declare so freemarker won't complain?
FreeMarker has no idea what those E-s mean. As the Manual says, it's
just Java's standard SimpleDateFormat. If SimpleDateFormat throws
exception, FreeMarker will complain.
...
--
Thanks,
Daniel Dekany
FOX427
10 years ago
Permalink
Thanks Daniel



--
View this message in context: http://freemarker.624813.n4.nabble.com/Freemarker-Date-Format-issue-tp4655381p4655386.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Loading...