Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
0 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
package gov.va.security.util;
import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.util.StringUtils;
import gov.va.oneconsult.seoc.api.util.ApiUtil;
/**
* Custom PropertyEditorSupport to convert from String to Date using custom
* format.
*
* @author Ablevets
*
*/
public class DateEditor extends PropertyEditorSupport
{
private final String dateFormat;
private final boolean allowEmpty;
private DateFormat formatter;
/**
* Create a new DateTimeEditor instance, using the given format for parsing and
* rendering.
*
* @param dateFormat DateFormat to use for parsing and rendering
* @param allowEmpty if empty strings should be allowed
*/
public DateEditor(String dateFormat, boolean allowEmpty)
{
this.dateFormat = dateFormat;
this.allowEmpty = allowEmpty;
this.formatter = new SimpleDateFormat(dateFormat);
}
/**
* Convert Date to Text
*/
public String getAsText()
{
if (getValue() == null)
return "";
Date value = ((Date) getValue());
return value != null ? value.toString() : "";
}
/**
* Convert Text to Date
*/
public void setAsText(String text) throws IllegalArgumentException
{
if (allowEmpty && !StringUtils.hasText(text))
{
setValue(null);
} else
{
Date val;
text = DataSanitizer.applyHtmlDecoder(text);
val = (Date) ApiUtil.parseStringToDate(formatter, text);
setValue(val);
}
}
}