Convert java.util.Date value to string using EL - jsf

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

Related

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}

Can not parse time string with pd.to_datetime

Not able to parse a date string into a Pandas date.
I have tried various formats for the date, however, I can't figure out a way to easily debug it as opposed to guessing.
test = pd.to_datetime('Thu Mar 21 18:24:35 +0000 2019', format=('%A %B %d %H:%M:%S','+0000', '%Y'))
ValueError: time data 'Thu Mar 21 18:24:35 +0000 2019' doesn't match format specified
For certain versions of pandas (I tested with 0.24.1), it should work without specifying format:
test = pd.to_datetime('Thu Mar 21 18:24:35 +0000 2019')
There are, however, issues with your format string. For one, I'm not sure why you're provided a tuple rather than one continuous string. For two, you use '%A' and %B' - in datetime formatting, these provide the weekday and month as the full name in both cases (source). In your case, you have the abbreviated names - '%a' and '%b'. So the following should work:
test = pd.to_datetime('Thu Mar 21 18:24:35 +0000 2019', format='%a %b %d %H:%M:%S +0000 %Y')

Error when parse date in FreeMarker

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.

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.

DateField getting value

I have problem with getting value of com.gwtext.client.widgets.form.DateField component. I would like to parse date on the server side so I'm using method getText() instead of getValue(). Problem is that DateField component has format YYYY-MM-DD but if I print date value I get result "Thu Sep 17 2009 00:00:00 GMT+0200", so the format is different. I thought that only getValueAsString() result will be like that "Thu Sep 17 2009 00:00:00 GMT+0200", but getText() should return "2009-09-17" :(, but it doesn't. How can I get the value in the correct format?
You can use formatters, they are part of GWT framework
DateField date = new DateField();
String result = DateTimeFormat.getFormat("MM/dd/yyyy").format(date.getValue());
I hope this will work.
use getRawValue()

Resources