FORUM PLAN UPDATE
Date revision: This forum will remain open to new posts and responses until December 1, 2018. (After that date, you will still be able to view and search the forum.) Also, we're taking a second look at the best place to host future conversation. For now, keep using this forum, and stay tuned for more news.



Mar 28, 2012, 6:22 PM
13 Posts

Java bean and DateTime

  • Category: Managed Beans in NSF
  • Platform: Linux
  • Release: 8.5.3
  • Role: Developer
  • Tags:
  • Replies: 4
 Just starting to look into beans, and I could not find the correct datatype for date/time fields. The problem arises when connecting the date to a date picker.
 
Johnny 
Mar 28, 2012, 9:32 PM
64 Posts
Re: Java bean and DateTime
Hello Johnny!
 
I have not yet used the datetime picker but I did have issues with dates to the back-end.  This probably will not help you, as I did get the date over as a String. 
 
(1) From my JavaBean 

private String Date_Format = "MM/dd/yyyy";

SimpleDateFormat simpledateformat = new SimpleDateFormat(Date_Format);

Calendar calendar = Calendar.getInstance();

private String ExpireDate = simpledateformat.format(calendar.getTime());

  • add getters and setters for ExpireDate
  • then get it in the back-end as such doc.appendItemValue("expireDate", ExpireDate);
     

(2) call it from your Xpage as such
 

<xp:inputText value="#{SendBulletinBean.expireDate}" id="inputText1" style="font-size:9pt" />

 
(3) in the field's properties, reflect the field as String and Not date.
 
Again, you may need the dateTime picker, perhaps this can spring some ideas.
 
L8ter!
Mar 29, 2012, 5:00 AM
261 Posts
Re: Java bean and DateTime
Johnny,
 
I haven't tested this with a bean but if you add the date/time picker to an XPage/ Custom control, a convertDateTime converter is added to the input control. You can probably bind the value to a bean property with a java.util.Date type.
 
To store a java.util.Date in a date/time field of a Notes document you'll need to convert it to a DateTime object. You can do that using the createDateTime method of the Session object:
 
doc.replaceItemValue("yourDateField", session.createDateTime( <your java.util.Date object> ) );
 
Mark
Mar 29, 2012, 8:11 AM
13 Posts
Re: Java bean and DateTime
 Thank you Mark - got it working!
 
This getter will allways set the initial date to "today". 
 
 Bean variable declaration:
  private Date Dato;
 
Getter and setter:
public Date getDato() {
try {
if (Dato == null) {
Dato = new Date();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Dato;
}

public void setDato(Date dato) {
Dato = dato;
}
 
Relevant part of the save method: 
public void save() {
try {
DateTime tmpDato = (DateTime)session.createDateTime(Dato);
docAktivitet.replaceItemValue("DatoDT", tmpDato);
docAktivitet.save();
} catch (NotesException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
 
 And relevant part of the load method:
  public void load(String unid) {
try {
if (docAktivitet != null) {
setDato(docAktivitet.getFirstItem("DatoDT").getDateTimeValue().toJavaDate()); // Date
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Sep 3, 2013, 7:16 PM
1 Posts
worked for me too Thank You!!!

Its never too late :)

This worked for me too.  I just wanted to add.  On the control in the UI, you want to bind the value property through expression language, I did mine like this value = #{transferBean.requestedDate}.  Server side similar to what the other guys posted.


FORUM PLAN UPDATE
Date revision: This forum will remain open to new posts and responses until December 1, 2018. (After that date, you will still be able to view and search the forum.) Also, we're taking a second look at the best place to host future conversation. For now, keep using this forum, and stay tuned for more news.