Error when parse date in FreeMarker - liferay

I got error when formart vietnam time. Language setting with US run fine, but change to VN so got error.
Error: on line 20, column 11 in 10157#10197#22325
The string doesn't match the expected date/time format. The string to parse was: "CN, 15 thg 7 2018 18:54:00 +0700". The expected format was: "EEE, dd MMM yyyy hh:mm:ss zzz".
I using liferay 6.2, display web content use Freemarker template. Here my code.
<#assign articleDisplayDate = .vars['reserved-article-display-date'].data />
<#assign AssetEntryLocalService = serviceLocator.findService("com.liferay.portlet.asset.service.AssetEntryLocalService" )>
<#assign JournalArticleLocalService = serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService")>
<#assign currentArticle = JournalArticleLocalService.getArticle(getterUtil.getLong(groupId),.vars['reserved-article-id'].data)>
<#assign currentArticleResourcePrimKey = currentArticle.getResourcePrimKey()>
<#assign currentArticleAssetEntry = AssetEntryLocalService.getEntry("com.liferay.portlet.journal.model.JournalArticle", currentArticleResourcePrimKey)/>
<div class="wrapcap">
<h3 class="title">
${.vars['reserved-article-title'].data}
</h3>
</div>
<div class="datecomm">
<span>
<i class="fa fa-calendar" aria-hidden="true"></i>
<span>${articleDisplayDate?date("EEE, dd MMM yyyy hh:mm:ss zzz")?string("MMMM, dd yyyy")}</span>
</span>
<span><i class="fa fa-eye" aria-hidden="true"></i> Số lượt xem: <span>${currentArticleAssetEntry.viewCount}</span></span>
</div>
<div class="desccomm">
<p></p>
</div>
<div class="contentcomm">
${content.getData()}
</div>
Have no idea with this date format.

It is reporting the error correctly.
Let's verify together:
The string doesn't match the expected date/time format. The string to
parse was: "CN, 15 thg 7 2018 18:54:00 +0700". The expected format
was: "EEE, dd MMM yyyy hh:mm:ss zzz".
"CN, 15 thg 7 2018 18:54:00 +0700"
so EEE is CN, check.
dd is 15, check.
thg is MMM. Don't know about that, but check.
yyyy is 7. Doesn't seem right to me.
hh is 2018. Definitely not right.
etc, etc, etc.

https://gsmblog.net/date-objects-liferay-freemarker-web-content-templates/
Seem like this bug is by handled very poorly by liferay.
I solved it with code here:
<#-- Retrieve the published date meta data field of the web content -->
<#assign articleDisplayDate = .vars['reserved-article-display-date'].data />
<#-- Save the original page locale for later -->
<#assign originalLocale = locale>
<#-- Set the page locale to the portals default locale -->
<#setting locale = 'en_US'>
<#-- Parse the date to a date object use locale en-US -->
<#assign date = articleDisplayDate?date("EEE, dd MMM yyyy hh:mm:ss zzz")>
<#-- Create date time formart use originalLocale (Your site localle) -->
<#assign dateTimeFormat = languageUtil.get(originalLocale, "MMMM, dd yyyy")>
<#-- Result -->
<#assign date = date?string(dateTimeFormat)>

This is test result.
English:
${articleDisplayDate}:Sun, 15 Jul 2018 18:54:00 +0700
${.now?string("EEE, dd MMM yyyy hh:mm:ss zzz")}: Mon, 20 Aug 2018 10:46:48 GMT+07:00
Vietnamese:
${articleDisplayDate}: CN, 15 thg 7 2018 18:54:00 +0700
${.now?string("EEE, dd MMM yyyy hh:mm:ss zzz")}: Mon, 20 Aug 2018 10:47:46 GMT+07:00

From the sample you have provided the problem seems to be that your locale FreeMarker configuration setting is some sort of English (try ${.locale} to see). When parsing a date/time/datetime, or even numbers, the current locale matters, because the name of days and months, etc. depends on the locale.
So, you should set the locale to Vietnamese. Normally, the locale FreeMarker configuration setting is set outside the template. If the locale can be different for individual template processings, which all use the same common Configuration object, then it can be set on the freemarker.core.Environment object for each independently. If for some reason you must set the locale inside the template, that's possible too:
<#setting locale="vi_VN">
${"CN, 15 thg 7 2018 18:54:00 +0700"?datetime("EEE, dd MMM yyyy hh:mm:ss zzz")}
Also note that I'm using ?datetime, not ?date. The last means date only, which can lead to some glitches elsewhere, since you also have time there.

Related

using format_datetime filter in twig

I show on a page a list of dates in french format.
I want to show date in french like this : "Janvier 2022".
The date stored in database is "2022-01-01".
Twig code :
{{ someDate | format_datetime(locale='fr', pattern="MMMM Y") | capitalize }}
Problem : it's showing "Janvier 2021", not "2022".
But the next date is correct, "Février 2022".
I don't understand why the year is 2021 and not 2022.
Please help !!
The week does not start the same day in France as in other countries
For year, use yyyy instead YYYY.
More information : Date format return wrong year in PHP with DateFmt in French

Freemarker ISO string to datetime with timezone

I need to display the below "String" in the desired format
String str = 1979-01-24T00:00:00.000-08:00
Desired format: Jan 24, 1979 00:00:00 AM PST
Note: The tz in the str could be any tz not limited to PST.
Tried the below but none worked:
str?datetime.iso - Output is Jan 24, 1979 2:00:00 AM CST - This displays the date time in the format I need but the time is being converted from PST to CST.
str?string("MMM dd, yyyy hh:mm:ss a zzz") - Error: Expected a method, but this has evaluated to a string
str?datetime?string("MMM dd, yyyy hh:mm:ss a zzz") - Error: Unparseable date: "1979-01-24T00:00:00.000-08:00"
<#setting datetime_format="iso"> str?datetime - 1979-01-24T02:00:00-06:00 - The timezone is changed.
The problem here is that FreeMarker parses date/time values to java.util.Date (and its subclasses), which don't store a time zone anymore, as it always stores the value in UTC. So that information is lost after parsing. As of 2.3.30, the only solution I see to do this in Java (with Java 8 ZonedDateTime).
The timezone can be configured by the following setting, as refer to their documentation https://freemarker.apache.org/docs/ref_directive_setting.html
<#setting time_zone ="PST">
<#assign str = "1979-01-24T00:00:00.000-08:00">
${str?datetime.iso}

Excel VBA to web form date format

Doing my first VBA to fill in a web form in IE. Having some issue with date from excel changing format when it is put in the web field.
Set doc = ie.document
doc.getElementById("txtTrackNotes").Value = ThisWorkbook.Sheets("Sheet1").Range("B2").Value
doc.getElementById("txtEntryTrackConnote").Value = ThisWorkbook.Sheets("Sheet1").Range("A2").Value
doc.getElementById("txtTrackDate").Value = ThisWorkbook.Sheets("Sheet1").Range("C2").Value
The last line there is the one causing problem. The Value in C2 is a date in the format dd/mm/yyyy
When it goes into the webform it comes up in the format dd MONTH (in text) yyyy 00:00 (for example 1 January 2018 00:00)
Source HTML is
<input name="txtTrackDate" class="Size9" id="txtTrackDate" style="width: 100px;" onclick="javascript:displayDatePicker('txtTrackDate');" onblur="javascript:CheckDate(this.value);" type="text" value="25/10/2018">
Any suggestions would be greatly appreciated
Try
=Format$(ThisWorkbook.Sheets("Sheet1").Range("C2").Value,"dd/mm/yyyy")
Is the field expecting this input format? If the above still changes then verify what the expected format is for the field.

Format Crypto API date to dateString

Am making a request to CryptoCompare API, and the data is parsed to my ejs template, i want the last update object key value to be shown in my template all i get is a number(62837329), and i used new Date() function to parse the date and it gave me a very wrong date.
How I parsed the date in my ejs template
<p> <%= new Date(display.RAW.ETH.USD.LASTUPDATE) %></p>
The date format that is shown in my template, if possible i wish it is in short date format
Sun Jan 18 1970 15:27:56 GMT+0000 (UTC)
I believe the CryptoCompare API is returning a timestamp in seconds from Jan 1, 1970 where the JavaScript Date expects a timestamp in milliseconds from Jan 1, 1970. Therefore you should multiply the number fetched by 1000.
<p> <%= new Date(display.RAW.ETH.USD.LASTUPDATE * 1000) %></p>
As far as formatting goes I would recommend Moment.js as a great way to easily format dates to your liking.

Convert java.util.Date value to string using EL

How to compare java.util.Date value with String using JSTL and EL?
This is the model:
private Date myDate; // Wed Jan 01 00:00:00 CET 1001
This is the view:
<c:if test="#{bean.myDate eq 'Wed Jan 01 00:00:00 CET 1001'}">
<h:outputText value="#{bean.myDate}">
<f:convertDateTime pattern="EEEE dd MMM yyyy # HH:mm:ss" />
</h:outputText>
</c:if>
However the condition always evaluates false. How is how can I solve it?
If your environment supports EL 2.2, just call its toString() method directly.
<c:if test="#{bean.myDate.toString() eq 'Wed Jan 01 00:00:00 CET 1001'}">
If yours doesn't, convert it to String first by inlining it as body of <c:set>.
<c:set var="myDate">#{bean.myDate}</c:set>
<c:if test="#{myDate eq 'Wed Jan 01 00:00:00 CET 1001'}">
Unrelated to the concrete problem, this is a rather strange way of comparing dates. It's very sensitive to changes in locale and timezone (if the webpage visitor uses a non-English locale and your webapp supports non-English locales, then the test on Wed Jan would fail and if the webserver is moved to non-CET timezone, then the test on CET would fail). Don't you rather want to compare it on year 1001 directly or a fixed pattern such as 1001-01-01 or so? Further I also wonder why you don't do the test in the rendered attribute of the JSF <h:outputText> component.
in jsf 1.2
this code works for me :
<webuijsf:button
actionExpression="#{guiRettificaBilPuntuale_2.button4_action}"
id="button4" text="#{bundle.TastoModificaRiga}"
rendered="#{ bilDetailsTable.value.settStatus eq 'N' or bilDetailsTable.value.starttime eq aLGUISessionBean_01.monthCurrentPublBilDate ? false : true}">
and starttime and monthCurrentPublBilDate are both dates (java.Date) so you can transform string in Date and compare them

Resources