Add Date API methods to FormField values

This commit is contained in:
Florian Schmaus 2018-06-03 17:28:49 +02:00
parent 298bcc8097
commit d26baeb66f
1 changed files with 31 additions and 0 deletions

View File

@ -17,8 +17,10 @@
package org.jivesoftware.smackx.xdata;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.jivesoftware.smack.packet.NamedElement;
@ -27,6 +29,8 @@ import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement;
import org.jxmpp.util.XmppDateTime;
/**
* Represents a field of a form. The field could be used to represent a question to complete,
* a completed question or a data returned from a search. The exact interpretation of the field
@ -266,6 +270,21 @@ public class FormField implements NamedElement {
return firstValue.toString();
}
/**
* Parses the first value of this form field as XEP-0082 date/time format and returns a date instance or {@code null}.
*
* @return a Date instance representing the date/time information of the first value of this field.
* @throws ParseException if parsing fails.
* @since 4.3.0
*/
public Date getFirstValueAsDate() throws ParseException {
String valueString = getFirstValue();
if (valueString == null) {
return null;
}
return XmppDateTime.parseXEP0082Date(valueString);
}
/**
* Returns the variable name that the question is filling out.
* <p>
@ -361,6 +380,18 @@ public class FormField implements NamedElement {
}
}
/**
* Adds the given Date as XEP-0082 formated string by invoking {@link #addValue(CharSequence)} after the date
* instance was formated.
*
* @param date the date instance to add as XEP-0082 formated string.
* @since 4.3.0
*/
public void addValue(Date date) {
String dateString = XmppDateTime.formatXEP0082Date(date);
addValue(dateString);
}
/**
* Adds a default values to the question if the question is part of a form to fill out.
* Otherwise, adds an answered values to the question.