Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package converter;
- import java.util.Map;
- import org.apache.struts2.util.StrutsTypeConverter;
- import org.joda.time.DateTime;
- import org.joda.time.DateTimeZone;
- import org.joda.time.format.DateTimeFormat;
- public final class JodaTimeConverter extends StrutsTypeConverter
- {
- @Override
- public String convertToString(Map map, Object dateTime)
- {
- if (dateTime instanceof DateTime)
- {
- DateTime time = (DateTime) dateTime;
- if (time.getHourOfDay() + time.getMinuteOfHour() + time.getSecondOfMinute() == 0)
- {
- return time.toString("dd-MMM-yyyy");
- }
- else
- {
- return time.toString("dd-MMM-yyyy HH:mm:ss");
- }
- }
- return null;
- }
- @Override
- public Object convertFromString(Map map, String[] values, Class clazz)
- {
- if (values != null && values.length > 0)
- {
- return parseValue(values[0]);
- }
- return null;
- }
- private DateTime parseValue(String value)
- {
- value = value.trim();
- try
- {
- if (!value.contains(" ")) //date contained
- {
- return DateTimeFormat.forPattern("MM/dd/yyyy").parseDateTime(value).withZone(DateTimeZone.UTC);
- }
- else //time only
- {
- return DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss").parseDateTime(value).withZone(DateTimeZone.UTC);
- }
- } catch (Exception e) {
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement