1 16 package org.directwebremoting.convert; 17 18 import java.sql.Time ; 19 import java.sql.Timestamp ; 20 import java.util.Calendar ; 21 import java.util.Date ; 22 23 import org.directwebremoting.dwrp.ProtocolConstants; 24 import org.directwebremoting.dwrp.SimpleOutboundVariable; 25 import org.directwebremoting.extend.Converter; 26 import org.directwebremoting.extend.InboundContext; 27 import org.directwebremoting.extend.InboundVariable; 28 import org.directwebremoting.extend.MarshallException; 29 import org.directwebremoting.extend.OutboundContext; 30 import org.directwebremoting.extend.OutboundVariable; 31 32 37 public class DateConverter extends BaseV20Converter implements Converter 38 { 39 42 public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException 43 { 44 String value = iv.getValue(); 45 46 if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) 48 { 49 return null; 50 } 51 52 try 53 { 54 long millis = 0; 55 if (value.length() > 0) 56 { 57 millis = Long.parseLong(value); 58 } 59 60 Date date = new Date (millis); 61 if (paramType == Date .class) 62 { 63 return date; 64 } 65 else if (paramType == java.sql.Date .class) 66 { 67 return new java.sql.Date (date.getTime()); 68 } 69 else if (paramType == Time .class) 70 { 71 return new Time (date.getTime()); 72 } 73 else if (paramType == Timestamp .class) 74 { 75 return new Timestamp (date.getTime()); 76 } 77 else if (paramType == Calendar .class) 78 { 79 Calendar cal = Calendar.getInstance(); 80 cal.setTime(date); 81 return cal; 82 } 83 else 84 { 85 throw new MarshallException(paramType); 86 } 87 } 88 catch (MarshallException ex) 89 { 90 throw ex; 91 } 92 catch (Exception ex) 93 { 94 throw new MarshallException(paramType, ex); 95 } 96 } 97 98 101 public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException 102 { 103 long millis; 104 105 if (data instanceof Calendar ) 106 { 107 Calendar cal = (Calendar ) data; 108 millis = cal.getTime().getTime(); 109 } 110 else if (data instanceof Date ) 111 { 112 Date date = (Date ) data; 113 millis = date.getTime(); 114 } 115 else 116 { 117 throw new MarshallException(data.getClass()); 118 } 119 120 return new SimpleOutboundVariable("new Date(" + millis + ")", outctx, true); 121 } 122 } 123
| Popular Tags
|