Advertisement
Guest User

Untitled

a guest
Jan 29th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. package converter;
  2.  
  3. import java.util.Map;
  4. import org.apache.struts2.util.StrutsTypeConverter;
  5. import org.joda.time.DateTime;
  6. import org.joda.time.DateTimeZone;
  7. import org.joda.time.format.DateTimeFormat;
  8.  
  9. public final class JodaTimeConverter extends StrutsTypeConverter
  10. {
  11.     @Override
  12.     public String convertToString(Map map, Object dateTime)
  13.     {
  14.         if (dateTime instanceof DateTime)
  15.         {
  16.             DateTime time = (DateTime) dateTime;
  17.             if (time.getHourOfDay() + time.getMinuteOfHour() + time.getSecondOfMinute() == 0)
  18.             {
  19.                 return time.toString("dd-MMM-yyyy");
  20.             }
  21.             else
  22.             {
  23.                 return time.toString("dd-MMM-yyyy HH:mm:ss");
  24.             }
  25.         }
  26.         return null;
  27.     }
  28.  
  29.     @Override
  30.     public Object convertFromString(Map map, String[] values, Class clazz)
  31.     {
  32.         if (values != null && values.length > 0)
  33.         {
  34.             return parseValue(values[0]);
  35.         }
  36.         return null;
  37.     }
  38.  
  39.     private DateTime parseValue(String value)
  40.     {
  41.         value = value.trim();
  42.         try
  43.         {
  44.             if (!value.contains(" ")) //date contained
  45.             {
  46.                 return DateTimeFormat.forPattern("MM/dd/yyyy").parseDateTime(value).withZone(DateTimeZone.UTC);
  47.             }
  48.             else //time only
  49.             {
  50.                 return DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss").parseDateTime(value).withZone(DateTimeZone.UTC);
  51.             }
  52.         } catch (Exception e) {
  53.             return null;
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement