Quantcast

Freemarker Date Format issue

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Freemarker Date Format issue

FOX427
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Freemarker Date Format issue

FOX427
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?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Freemarker Date Format issue

Daniel Dekany
In reply to this post by FOX427
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


Monday, March 2, 2015, 4:30:24 PM, FOX427 wrote:

> 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.
>
> ------------------------------------------------------------------------------
> Dive into the World of Parallel Programming The Go Parallel Website, sponsored
> by Intel and developed in partnership with Slashdot Media, is your hub for all
> things parallel software development, from weekly thought leadership blogs to
> news, videos, case studies, tutorials and more. Take a look and join the
> conversation now. http://goparallel.sourceforge.net/
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>


------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Freemarker Date Format issue

FOX427
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Freemarker Date Format issue

Daniel Dekany
Monday, March 2, 2015, 10:55:12 PM, FOX427 wrote:

> 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."

I have learnt something today too... (:

> I know that I will get "Mon" in my case and if i use EEEEEE I
> will get "Monday",

Or if you use EEEE.

> 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.

> 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.
>
> ------------------------------------------------------------------------------
> Dive into the World of Parallel Programming The Go Parallel Website, sponsored
> by Intel and developed in partnership with Slashdot Media, is your hub for all
> things parallel software development, from weekly thought leadership blogs to
> news, videos, case studies, tutorials and more. Take a look and join the
> conversation now. http://goparallel.sourceforge.net/
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

--
Thanks,
 Daniel Dekany


------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Freemarker Date Format issue

FOX427
Thanks Daniel
Loading...