Fórum

Handling Dates in Liferay

Prateeksha Mandloi, modificado 9 Anos atrás.

Handling Dates in Liferay

Regular Member Postagens: 112 Data de Entrada: 05/02/14 Postagens Recentes
Hey all,

I want to insert create date, modified date and expiry date in the DB from my custom portlet.

When I saw liferay's jsp to add Expiry date I found this field :

 <aui:input dateTogglerCheckboxLabel="never-expire" disabled="<%= neverExpire %>" name="expirationDate" />


Now I am not clear as how to fetch the date and time from this field. Also, how liferay adds create and modified dates in the db table.

Can anyone help ?

Thanks in advance emoticon
thumbnail
Tina Agrawal, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Expert Postagens: 297 Data de Entrada: 03/01/08 Postagens Recentes
In case you have your custom portlet do you have custom tables as well? If yes you need to define these fields and then store values in them.
The code you have pasted will just draw a date picker on the UI. You will need to fetch the date month year in your controller class and store it in DB.

Tina
Prateeksha Mandloi, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Regular Member Postagens: 112 Data de Entrada: 05/02/14 Postagens Recentes
Hi Tina,

Yes I do have my custom tables. And I Have all these columns : create date, modified date and expiry date in my custom tables as well.

But while I am trying to fetch : expiry date through Date format ; try to save it in controller class and store it in DB.. It does not give me any proper value.

I have tried SimpleDateFormat and DateFormat to do the same. But it gives "expirationDate is unparsebale date" error
thumbnail
Tina Agrawal, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Expert Postagens: 297 Data de Entrada: 03/01/08 Postagens Recentes
Can you paste your code of how you are trying to fetch?

Tina
Prateeksha Mandloi, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Regular Member Postagens: 112 Data de Entrada: 05/02/14 Postagens Recentes

DateFormat df = DateFormat.getDateTimeInstance();
java.sql.Date expDate=  (Date) ParamUtil.getDate(actionRequest, "expirationDate", df);


and


SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy");  
Date expDate;
expDate= (Date) formatter.parse("expirationDate");
thumbnail
Jitendra Rajput, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Liferay Master Postagens: 875 Data de Entrada: 07/01/11 Postagens Recentes
Prateeksha Mandloi:

DateFormat df = DateFormat.getDateTimeInstance();
java.sql.Date expDate=  (Date) ParamUtil.getDate(actionRequest, "expirationDate", df);



ParamUtil.getDate() returns java.util.Date not java.sql.Date
thumbnail
Tina Agrawal, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Expert Postagens: 297 Data de Entrada: 03/01/08 Postagens Recentes
Jitendra is correct.

At the same time did you check whether you are getting an expiration date in your action class.
Because if you use the aui:input tag you will get month date year separately and then you combine it into date. Something like this -


int displayDateMonth = ParamUtil.getInteger(
			uploadPortletRequest, "displayDateMonth");
		int displayDateDay = ParamUtil.getInteger(
			uploadPortletRequest, "displayDateDay");
		int displayDateYear = ParamUtil.getInteger(
			uploadPortletRequest, "displayDateYear");
		int displayDateHour = ParamUtil.getInteger(
			uploadPortletRequest, "displayDateHour");
		int displayDateMinute = ParamUtil.getInteger(
			uploadPortletRequest, "displayDateMinute");
		int displayDateAmPm = ParamUtil.getInteger(
			uploadPortletRequest, "displayDateAmPm");

		if (displayDateAmPm == Calendar.PM) {
			displayDateHour += 12;
		}
Date displayDate = PortalUtil.getDate(
			displayDateMonth, displayDateDay, displayDateYear, displayDateHour,
			displayDateMinute, user.getTimeZone(),
			ArticleDisplayDateException.class);
Prateeksha Mandloi, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Regular Member Postagens: 112 Data de Entrada: 05/02/14 Postagens Recentes
Thanks Tina,

Expiration Date I did the same and it worked emoticon

But what about create and modified date ..? How to fetch them ?
thumbnail
Tina Agrawal, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Expert Postagens: 297 Data de Entrada: 03/01/08 Postagens Recentes
If the user is entering the create date and modified date from your form you need to retrieve it in the similar way you did for expiration.
Generally create date/Modified Date is the date on when you are creating/modifying the content and you can simply get it by -
Date now = new Date();


Tina
Prateeksha Mandloi, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Regular Member Postagens: 112 Data de Entrada: 05/02/14 Postagens Recentes
No, user is not entering create and modified dates,I tried :
Date now = new Date();

This is not working.

I read we can get create and modified dates from servicecontext, as

serviceContext.getCreateDate(); But It has a conditon :

getCreateDate

public Date getCreateDate()
Returns the date when an entity was created if this service context is being passed as a parameter to a method which creates an entity.
Returns:
the creation date


And I am not passing service context as a parameter to a method.
thumbnail
David H Nebinger, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Liferay Legend Postagens: 14919 Data de Entrada: 02/09/06 Postagens Recentes
Wow, never seen such a long thread with no answer in sight...

So here it is: use a model listener. In the onBeforeUpdate() method, you manually assign new dates to the appropriate columns (create date if not set, modified date always gets set).

Works against every single update, you don't have to track it manually in your other code, ...
thumbnail
Tina Agrawal, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Expert Postagens: 297 Data de Entrada: 03/01/08 Postagens Recentes
David,

Why does he need to use a model listener when he is updating his own tables.
He has a form and he wants to store values in his tables. I dont see a need for model listener.
Please correct me if I am wrong.

When you say new Date is not working what is not working?

You need to use java.util.Date.

Tina
thumbnail
David H Nebinger, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Liferay Legend Postagens: 14919 Data de Entrada: 02/09/06 Postagens Recentes
Because the minute his SB stuff is done, someone will want to use the service directly. You use a model listener to ensure that the data is accurate at the data level, where it applies. Just like you'd do this sort of thing normally with a database trigger even when it is just as easy to do in the code, you do it in the trigger to enforce data integrity.
Prateeksha Mandloi, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Regular Member Postagens: 112 Data de Entrada: 05/02/14 Postagens Recentes
Hi David,

Can you please explain how to use model listener for date. I have never used them
Prateeksha Mandloi, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Regular Member Postagens: 112 Data de Entrada: 05/02/14 Postagens Recentes
This worked :
Calendar date = new GregorianCalendar();
polls_custom.setCreateDate(date.getTime());


I just missed populating an object emoticon
thumbnail
David H Nebinger, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Liferay Legend Postagens: 14919 Data de Entrada: 02/09/06 Postagens Recentes
Prateeksha Mandloi:
Can you please explain how to use model listener for date.


I could, but it's easier just to point you at documentation: https://www.liferay.com/community/wiki/-/wiki/Main/Portal+Hook+Plugins#section-Portal+Hook+Plugins-Model+Listeners
Prateeksha Mandloi, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Regular Member Postagens: 112 Data de Entrada: 05/02/14 Postagens Recentes
Hi Tina,

It shows error. I am attaching the image.(datePolls.jpg)

What I finally did is :

Calendar date = new GregorianCalendar();
polls_custom.setCreateDate(date.getTime());


Which gives me todays date. But it is not getting entered into database. Though it is present in the object(polls_custom) I am sending to the table.
I dont know whats wrong ?
thumbnail
David H Nebinger, modificado 9 Anos atrás.

RE: Handling Dates in Liferay

Liferay Legend Postagens: 14919 Data de Entrada: 02/09/06 Postagens Recentes
You have to namespace both sides since it appears that the javax.sql.Date class is already in there:

java.util.Date date = new java.util.Date();