Don't hassle your mgmt (unless yours are better than mine!)
Connor
-----------------------------
package net...;
import java.beans.PropertyEditorSupport;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
/**
* Custom PropertyEditorSupport to convert from String to DateTime using
JODA.
*/
public class CustomDateTimeEditor extends PropertyEditorSupport {
private final String pattern;
private final DateTimeZone timeZone;
private final boolean allowEmpty;
/**
* Create a new CustomDateTimeEditor instance.
* <p/>
* The "allowEmpty" parameter states if an empty String should be
allowed for
* parsing, i.e. get interpreted as null value. Otherwise, an
* IllegalArgumentException gets thrown.
*
* @param dateTimeZone
*
* @param allowEmpty
* if empty strings should be allowed
*/
public CustomDateTimeEditor(String pattern, DateTimeZone dateTimeZone,
boolean allowEmpty) {
this.pattern = pattern;
this.timeZone = dateTimeZone;
this.allowEmpty = allowEmpty;
}
@Override
public String getAsText() {
if (getValue() == null) return "";
DateTime value = (DateTime) getValue();
return value != null ?
value.withZone(timeZone).toString(pattern) : "";
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (StringUtils.isBlank(text)) {
if (allowEmpty) {
setValue(null);
} else {
throw new IllegalArgumentException(
"The specified DateTime string cannot be null");
}
} else {
setValue(DateTimeFormat.forPattern(pattern).withZone(timeZone)
.parseDateTime(text));
}
}
}
-------------------------------------------------
package net...
import java.beans.PropertyEditorSupport;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTimeZone;
/**
* Custom PropertyEditorSupport to convert from String to LocalDate
using JODA.
*/
public class CustomDateTimeZoneEditor extends PropertyEditorSupport {
private final boolean allowEmpty;
/**
* Create a new CustomLocalDateEditor instance.
* <p/>
* The "allowEmpty" parameter states if an empty String should be
allowed for
* parsing, i.e. get interpreted as null value. Otherwise, an
* IllegalArgumentException gets thrown.
*
* @param allowEmpty
* if empty strings should be allowed
*/
public CustomDateTimeZoneEditor(boolean allowEmpty) {
this.allowEmpty = allowEmpty;
}
@Override
public String getAsText() {
if (getValue() == null) return "";
DateTimeZone value = (DateTimeZone) getValue();
return value != null ? value.getID() : "";
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (StringUtils.isBlank(text)) {
if (allowEmpty) {
setValue(null);
} else {
throw new IllegalArgumentException(
"The specified DateTimeZone ID cannot be null");
}
} else {
setValue(DateTimeZone.forID(text));
}
}
}
------------------------------------------------------------
package net..;
import java.beans.PropertyEditorSupport;
import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
/**
* Custom PropertyEditorSupport to convert from String to LocalDate
using JODA.
*/
public class CustomLocalDateEditor extends PropertyEditorSupport {
private final boolean allowEmpty;
/**
* Create a new CustomLocalDateEditor instance.
* <p/>
* The "allowEmpty" parameter states if an empty String should be
allowed for
* parsing, i.e. get interpreted as null value. Otherwise, an
* IllegalArgumentException gets thrown.
*
* @param allowEmpty
* if empty strings should be allowed
*/
public CustomLocalDateEditor(boolean allowEmpty) {
this.allowEmpty = allowEmpty;
}
@Override
public String getAsText() {
if (getValue() == null) return "";
LocalDate value = (LocalDate) getValue();
return value != null ? value
.toString("M/d/yyyy") : "";
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (StringUtils.isBlank(text)) {
if (allowEmpty) {
setValue(null);
} else {
throw new IllegalArgumentException(
"The specified LocalDate string cannot be null");
}
} else {
setValue(DateTimeFormat.forPattern("M/d/yyyy").parseDateTime(text).toLocalDate());
}
}
}
------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Joda-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/joda-interest