f:convertDateTime with LocalDateTime throws "Cannot format given Object as a Date" - jsf

I have a local date time property:
private LocalDatetime date;
I want to show it in this format dd-MM-yyyy HH:mm:ss:
<h:outputText value="#{transaction.date}">
<f:convertDateTime pattern="dd-MM-yyyy HH:mm:ss"/>
</h:outputText>
But I got this exception:
java.lang.IllegalArgumentException: Cannot format given Object as a Date
How is this caused and how can I solve it?

The <f:convertDateTime> defaults currently still to java.util.Date as expected date type and is currently not (yet?) capable of automatically detecting the date type. It will under the covers incorrectly continue to use java.text.SimpleDateFormat instead of java.time.format.DateTimeFormatter to format the LocalDateTime. This specific exception is coming from SimpleDateFormat.
You need to explicitly set the type attribute of the <f:convertDateTime> tag to the desired date type if your actual date value is not a java.util.Date. You can find a list of supported types in in tag documentation:
Name
Required
Type
Description
type
false
javax.el.ValueExpression(must evaluate to java.lang.String)
Specifies what contents the string value will be formatted to include, or parsed expecting. Valid values are "date", "time", "both", "localDate", "localDateTime", "localTime", "offsetTime", "offsetDateTime", and "zonedDateTime". The values starting with "local", "offset" and "zoned" correspond to Java SE 8 Date Time API classes in package java.time with the name derived by upper casing the first letter. For example, java.time.LocalDate for the value "localDate". Default value is "date".
So, in your specific case, setting it to localDateTime should fix it:
<h:outputText value="#{transaction.date}">
<f:convertDateTime type="localDateTime" pattern="dd-MM-yyyy HH:mm:ss"/>
</h:outputText>
In case it is output-only and you happen to use the utility library OmniFaces of version 3.6 or newer, then you can also use the #{of:formatDate()} function instead. It is capable of automatically detecting the date type and it currently supports all known instances of java.util.Date, java.util.Calendar and java.time.Temporal.
#{of:formatDate(transaction.date, 'dd-MM-yyyy HH:mm:ss')}

Related

Jhipster jdl adding fake data LocalDate field is null in the database, Date format issue

I generate an entity with
jhipster import-jdl hello.jdl
entity Hello {
title String,
myDate LocalDate
}
Default fake data is generated
hello.csv
id;title;myDate
1;mobile Fish;2019-11-17
2;Savings Account Dam;2019-11-18
Problem
However my date format is Estonian and is DD.MM.YYYY, so if I change it to following then the data is not being imported into the database. myDate field is null for both entries. Is there any way to import dates in other format than YYYY-MM-DD ?
hello.csv
id;title;myDate
1;mobile Fish;17.11.2019
2;Savings Account Dam;18.11.2019
Unfortunately you cannot change this format because Liquibase expects dates to be in "yyyy-MM-dd" format as stated in its doc:
Date/Time values included in the CSV file should be in ISO format http://en.wikipedia.org/wiki/ISO_8601 in order to be parsed correctly by Liquibase.
It's hard-coded in liquibase.util.ISODateFormat.java, so there's nothing JHipster can do about it.

Checking for readonly in Geb

I'm trying to write a test in Geb that has a bunch of different text fields. The scenario is essentially that there are five or six html inputs and the user should only be allowed to type in one of them at a time. That is, if the first textbox has a value, the rest of the boxes should be immutable.
Instead of the element having an input type="disabled", they have an attribute (I'm assuming it's an attribute?) of readonly.
Here is a generic example:
<input aria-labelledby="Date Input Value 1" id="Date Input 1" name="Date Input 1" class="input-small DateOrEmpty dateInput" value="" style="display: inline-block;" readonly="">
I've tried
${"input", id: "Date Input 1"}.#readonly
as well as
${"input", id: "Date Input 1"}.has("readonly")
and I haven't had much luck..
Instead of having to deal with the fact that even an empty readonly attribute will cause the element to be read only but $("input[id='Date Input 1']").#readonly evaluates to a falsey value (as #readonly returns the value of the attribute which is an empty string in your case) why not turn the navigator into an instance of FormElement module and use the isReadOnly() property method it provides?
$("input[id='Date Input 1']").module(FormElement).readOnly
EDIT
As pointed out by kriegaex, $("input[id='Date Input 1']").#readonly actually evaluates to a truthy value (because WebDriver always returns a string which contains true when obtaining the value of readonly attribute as long as the attribute is defined on the element and regardless of the actual value of that attribute).
The above means that:
$("input[id='Date Input 1']").module(FormElement).readOnly
and:
$("input[id='Date Input 1']").#readonly
are equivalent in that they evaluate to a truthy value.
You have several problems here:
You use HTML IDs with inline spaces. This are actually not legal names and make it more difficult to write simple CSS selectors like #my-id for them.
Geb syntax for selectors uses parentheses, not curly braces. Curly braces are used for closures, which are a completely different kind of beast.
Please note that according to HTML specification the readonly attribute is boolean and always true if it exists at all, i.e. usually you do not write readonly="something" but just readonly. Even if you write readonly="false" it will evaluate to true because its value is ignored, only its existence is checked.
So if you have this HTML code:
<input id="Date Input 1" readonly>
<input id="Date Input 2">
<input id="date-input-3" readonly="false">
<input id="date-input-4">
<input id="date-input-5" readonly="">
You can write these selectors in your Geb test (please note how not using spaces in IDs makes the selectors simpler):
$("input[id='Date Input 1']").#readonly
!$("input[id='Date Input 2']").#readonly
$("input#date-input-3").#readonly
!$("input#date-input-4").#readonly
$("input#date-input-5").#readonly

Removing time format in a date

I am passing a date as a parameter where daydate is the parameter
string daydate
The content of daydate is 3/3/2017 12:00:00 AM
I am doing this to remove the time and still the time does not go away
string strCleanDate = String.Format("{0:M/d/yyyy}", daydate);
How can I remove the time?
That is because the formatter is ignored as the instance you are passing to string.Format is a string so the placeholder is filled with the value you passed in and the format is ignored completely. If you want to apply DateTime formatting you need to pass in an instance of a DateTime type.
Your first fix should be to change your parameter to be of type DateTime and not string. Otherwise a caller could pass "HI THERE" and your method/application would break, or worse push that value to a store and now your store is polluted with invalid value(s).
public void SomeMethod(DateTime daydate)
{
var dateOnly = dayDate.Date;
}
You should pass dayDate as a DateTime value instead of as a string. Then the format will work correctly.

Why does Date serialize for a JSON key differently than it does for a JSON value?

With the following code:
def date = new Date()
println new groovy.json.JsonBuilder([(date): date]).toString()
The result is something like
{"Fri Oct 28 15:00:45 ART 2016":"2016-10-28T18:00:45+0000"}
I was expecting the same representation as key and as value for the same date.
Can I force the JsonBuilder to output the keys with the same format as the values?
Thing is, JsonBuilder will format dates using by default a new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") and I understand this is not what you wish to change. Since the "key" part is serialized with the toString() method, you have two solutions: either use [date.format("yyyy-MM-dd'T'HH:mm:ssZ"): date] or use metaProgramming to overload Date.toString() (it will be used for every Date object, though, so you might not want that).

JSP: String to Boolean conversion

I get "true" or "false" as strings in JSP, but to use it in the rest of the code, I need to convert it to Boolean.
How to do the conversion in the JSP and do I need to include something for the same.
As per JSP documentation, literal "true" will work for boolean test, it will be converted to a type Boolean.TRUE, so you don't need to convert it to boolean explicitly. So if your attribute is 'booleanVar' which contains values "true" or "false", just use it directly in boolean test, such as
<c:if test="${booleanVar}">
</c:if>

Resources