This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

JSF converter

By: a guest on Oct 24th, 2013  |  syntax: Java  |  size: 2.24 KB  |  views: 42  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package converter;
  2.  
  3. import java.util.TimeZone;
  4. import javax.faces.application.FacesMessage;
  5. import javax.faces.bean.ManagedBean;
  6. import javax.faces.bean.RequestScoped;
  7. import javax.faces.component.UIComponent;
  8. import javax.faces.context.FacesContext;
  9. import javax.faces.convert.Converter;
  10. import javax.faces.convert.ConverterException;
  11. import org.joda.time.DateTime;
  12. import org.joda.time.DateTimeZone;
  13. import org.joda.time.format.DateTimeFormat;
  14. import org.joda.time.format.DateTimeFormatter;
  15. import util.Utility;
  16.  
  17. @ManagedBean
  18. @RequestScoped
  19. public final class DateTimeConverter implements Converter
  20. {
  21.     @Override
  22.     public Object getAsObject(FacesContext context, UIComponent component, String value)
  23.     {
  24.         DateTime dateTime=null;
  25.         try
  26.         {
  27.             dateTime = DateTimeFormat.forPattern("dd-MMM-yyyy hh:mm:ss aa").withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("IST"))).parseDateTime(value);
  28.             //DateTimeFormatter dateTimeFormatter=DateTimeFormat.forPattern("dd-MMM-yyyy hh:mm:ss aa");
  29.             //dateTime = dateTimeFormatter.parseDateTime(value).withZone(DateTimeZone.UTC);
  30.         }
  31.         catch (IllegalArgumentException e)
  32.         {
  33.             throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "", Utility.getMessage("datetime.converter.error", DateTimeFormat.forPattern("dd-MMM-yyyy hh:mm:ss aa").print(DateTime.now().withZone(DateTimeZone.forID("Asia/Kolkata"))))), e);
  34.         }
  35.         catch(UnsupportedOperationException e)
  36.         {
  37.             throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "", Utility.getMessage("datetime.converter.error", DateTimeFormat.forPattern("dd-MMM-yyyy hh:mm:ss aa").print(DateTime.now().withZone(DateTimeZone.forID("Asia/Kolkata"))))), e);
  38.         }
  39.         return dateTime;
  40.     }
  41.  
  42.     @Override
  43.     public String getAsString(FacesContext context, UIComponent component, Object value)
  44.     {
  45.         DateTimeFormatter dateTimeFormatter=DateTimeFormat.forPattern("dd-MMM-yyyy hh:mm:ss aa").withZone(DateTimeZone.forID("Asia/Kolkata"));  //This zone will be tackled later from the database to display.      
  46.         return value instanceof DateTime?dateTimeFormatter.print((DateTime)value):null;
  47.     }
  48. }
clone this paste RAW Paste Data