Jstl: format string to date [duplicate] - string

This question already has answers here:
Convert and format a Date in JSP
(6 answers)
Closed 4 years ago.
I'm just starting using Spring and I'm quite new in this terms.
I'm having a problem, I need to convert one String to a Date in jsp, and I had thought to use jstl.
I know that the variable has a String which is: "17/03/2016" (I know It because without a format It displays ok)
I have tried all kind of things to be able of watching this String in format date but I have no idea why It's not working
${object.myattribute} <-- It displays correctly "17/03/2016"
But I need to have It on a date mode.
So I decided to try the fmt:formatDate option, but I must be doing something wrong 'cause I can't get the desired result. It even does nothing, and if the value is inside a table, It won't display the table.
I have tried many things but no one gives the correct result:
<fmt:formatDate type="both"
dateStyle="short" timeStyle="short"
value="${object.myattribute}" />
${object.myattribute}
<fmt:parseDate pattern="yyyy-MM-dd" value="${object.myattribute}" var="parsedStatusDate" />
${formattedStatusDate}
${parsedStatusDate}
This shows no result
<fmt:formatDate value='${object.myattribute}' pattern='dd/MM/yyyy' var="objMyAttrib" />
<c:set var="strDate" value="${object.myattribute}"/>
${strDate}
<c:out value="${strDate}"></c:out>
The problem is that no one result can be shown. I suposse that there must be some errors so I can't display this info.
I will continue trying but any idea will be really well received :) I assume that I'm doing something wrong but I can't figure what is what I'm doing wrong or if It can't be done (parse from String to Date in jsp)
Thank you in advance. An example will be also well received.

Try this :
<fmt:parseDate pattern="dd/MM/yyyy" value="${object.myattribute}" var="date" />
and use it like : ${date}

The problem was that I had to order a dataTable column by the string 25/06/2015 for example, and I need It on this style and no other.
But the dataTable was only ordering the column by the day.
As I read that I should pass a date from the controller, I found a solution that worked for me.
What I have done is to pass to the model another string which represents the date but in numeric form. I mean if I had "25/06/2015 " now I will have 20150625. dataTable will know how to order It correctly.
As I needed that "numeric String" to order the table I did something like this
<td>
// this one date is used to order the dataTable
p class="noVisible">${myObject.numericDate}/p//20150625
// this is the date that we will see in the table. It means that the dataTable will order the column by the first attribute It has in the td.
${myObject.stringDate} //25/06/2015
</td>
I hope this can help anyone who can have this kind of question ^^ even that is not a real solution It works for me

Related

RichFaces ExtendedDataTable tableState error with generated ID

I am trying to preserve state of a table using RichFaces. RichFaces has this functionality of tableState which is a json string of all information about the table. The tableState string right now looks like this
{"columnsWidthState":{"j_idt433":"140px","j_idt269":"32px","j_idt379":"80px","j_idt424":"200px","j_idt415":"200px","j_idt406":"110px","j_idt375":"115px","j_idt397":"80px","j_idt388":"120px"},"columnsSortState":{"j_idt433":null,"j_idt269":null,"j_idt379":null,"j_idt424":null,"j_idt415":null,"j_idt406":null,"j_idt375":null,"j_idt397":null,"j_idt388":null},"columnsOrderState":["j_idt269","j_idt375","j_idt379","j_idt388","j_idt397","j_idt406","j_idt415","j_idt424","j_idt433"],"columnsFilterState":{"j_idt433":null,"j_idt269":null,"j_idt379":null,"j_idt424":null,"j_idt415":null,"j_idt406":null,"j_idt375":null,"j_idt397":null,"j_idt388":null}}
I am curios to know how this identifiers are generated i.e. j_idt388 etc. I tried giving id to the column and the richfaces tableState string honored that. The tableState json looks like this now
{"columnsWidthState":{"date":"140px","sessions":"110px","application":"115px","activity":"200px","kvalue":"80px","risk":"80px","account":"120px","riskFactor":"200px","status":"32px"},"columnsSortState":{"date":null,"sessions":null,"application":null,"activity":null,"kvalue":null,"risk":null,"account":null,"riskFactor":null,"status":null},"columnsOrderState":["status","application","account","kvalue","sessions","riskFactor","activity","risk","date"],"columnsFilterState":{"date":null,"sessions":null,"application":null,"activity":null,"kvalue":null,"risk":null,"account":null,"riskFactor":null,"status":null}}
I preserve this json string so that in future if I log in, I should see the state of table same as I left.
Problem is that once I log out and log in into my application. RichFaces fails and throws an exception. It makes me feel its not able to recognize the json string anymore but I wonder why. Here's the exception:
java.lang.NullPointerException: null
at org.richfaces.renderkit.ExtendedDataTableRenderer.encodeHeaderOrFooterCell(ExtendedDataTableRenderer.java:275) ~[richfaces-4.5.13.Final.jar:4.5.13.Final]
at org.richfaces.renderkit.ExtendedDataTableRenderer.encodeHeaderOrFooter(ExtendedDataTableRenderer.java:378) ~[richfaces-4.5.13.Final.jar:4.5.13.Final]
at org.richfaces.renderkit.ExtendedDataTableRenderer.encodeHeader(ExtendedDataTableRenderer.java:466) ~[richfaces-4.5.13.Final.jar:4.5.13.Final]
at org.richfaces.renderkit.ExtendedDataTableRenderer.doEncodeChildren(ExtendedDataTableRenderer.java:725) ~[richfaces-4.5.13.Final.jar:4.5.13.Final]
JSF generates IDs for elements, where the developer hasn't set his own ID in the element's attribute. This results in the IDs beginning with j_id you posted in your first code snippet (for example j_idt433). You can read more about the ID generation in this answer.
ID generation depends on counting the number of elements. Thus, if you persist a table state and later try to restore it, and the number of elements before the table changed, the IDs are not correct anymore. This may result in the exception you posted.
To prevent this, just give every <rich:column /> an ID.

JSF hour-minute input

I would like to know what is the prefered way to handle hour-minute input fields in JSF.
I am using JSF2.2 with primefaces 5.2
I would like to have one input field for hours and one for minutes and have the value saved to dabase in decimal and fraction format.
Then it would be nice alo so have a converter for this, for example if you have the need to store in minute or second precision. Is there already something availabe or do I realy have to write one of my own?

Double.toString without the exponential notation for large values

In my JSF2 application, I want my double value to be displayed without exponential notation. Is it possible?
I cannot use NumberFormat or DecimalFormat since its going to change my data type to String.
I understand from Java documentation, if my double value is less than 10^-3 or greater than or equal to 10^7, then it is represented in so-called "computerized scientific notation"(Ref: http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#toString()).
And, In JSF while displaying a double value it will internally convert any data type to string.
For example, if my double value is 9999999999 it is getting displayed as 9.9e^9. I want it to be displayed as it is. i.e 9999999999
But, it there any possibility to override this character of Double or is there any other approach to solve this issue?
The correct solution for problems like this is to pass the internal data type around for as long as possible. Just at the moment when you display it to the user, you format it properly using DecimalFormat.
So you have to find all the places in your JSF2 UI where you display doubles and use the proper formatters there.
Note that DecimalFormat isn't threat safe, so you have to create a new formatter for each request (at least).
If your code provides a double or Double, you can display that value directly in the page using a converter:
<h:outputText value="#{myBean.someDoubleValue}">
<f:convertNumber pattern="0"/>
</h:outputText>

Masking for a Calendar in JSF, Primefaces to Accept only Dates in format of MM/dd/yyyy?

I am using JSF,Primefaces.
I have a search Criteria Functionality in my App, there i am facing a problem with calendar date.
I am using this piece of code for my application.
<p:calendar id="Date" value="#{bean.fromDate}" pattern="MM/dd/yyyy" showOn="button"/>
I faced this sort of issue that it is accepting even i had given the date 214/03/2013
as and then converting it into 10/03/2030 and performing the action!
But,What exactly i need to do is either to display a message or else to mask that input field as 99/99/9999.
I referred this Link
In my scenario i should not use readonlyInput="true" because i may or may not give a date.
If i am using that then i am unable to clear the field back to empty,because i am in use with #SessionScoped.
Having a look to how Primefaces Calendar works, it turns to use by default Java's SimpleDateFormat to do the parsing work. If you have a look at the docs, Java uses heuristics to force any given input to convert to an specific date.
Specify whether or not date/time parsing is to be lenient. With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match this object's format. With strict parsing, inputs must match this object's format.
That's why your calendar input is accepting that weird dates when they don't even exist. The key is to provide the calendar your custom converter which doesn't accept a lenient date format. JSF already provides one:
<p:calendar ...>
<f:convertDateTime pattern="MM/dd/yyyy" />
</p:calendar>
That would throw a conversion error if the end user provides a non-existing date.
See also:
Incorrect date parsing using SimpleDateFormat, Java
What is the use of "lenient "?

Set value of datetime field to null

So, I´ve got an issue that someone might have solved (or so I hope). I have a datetime field that I use in a contenttype (on a listtemplate) that has its own editform.
Here´s a walkthrough of what happens:
Adding a value to the datetime column and saving the updated value shows up (as expected).
Updating the datetime column and adding null (emptying out the value) via the editform. The value isn´t updated but instead showing the old value.
I did some experimenting with this and I came to the conclusion that if I add an eventreceiver and try to update the value there it doesn´t even enter the eventreceiver as a blank string (I had an idea that the value is as a blank string and sharepoint can´t parse that to a null datetime) which leads me to think that there´s an issue with the fieldcontrol that causes this. I also found this article on MSDN which seems to be around the same issue. And also, the datetime field in the contenttype isn´t required.
Any ideas or suggestions?
UPDATE:
Apparently this only happens when I use a custom editform. When I use one of built in listtemplates this works like a charm.
I've had similar issues with this in the past. It is indeed a known bug. One work around is to set the field to the min or max value of the type and check for this value wherever needed and do the appropriate conversions. Depending on your setup and use, you could create a trigger to convert min/max values to null and thus minimize the code required to handle such a work around.
After some deep investiongation I have found that the root of this problem is related to the fact that I was using XML node propagation between the columns in the list and the actual xml file. I had prevoiusly been struggling with this regarding stardard text fields and thought that I had taken care of the issue..apparently not. DateTime fields apparently will not get sent to the eventreceiver afterproperties if it is null as thus my code will not pick it up as a null value. I had to take case of that and add them to afterproperties if I can´t find them in afterproperties. I will make a blogpost series about these issues and post the links to this post shortly.
UPDATE:
I have now completed a blogpost about how I solved this issue. Feel free to check it out:
http://johanleino.wordpress.com/2009/08/24/node-demotion-does-not-work-with-blank-empty-values/

Resources