View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.core.util.jaxb;
17  
18  import java.util.Date;
19  
20  import javax.xml.bind.annotation.adapters.XmlAdapter;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.rice.core.api.CoreApiServiceLocator;
24  
25  /**
26   * An XML Adapter that relies on the DateTimeService to marshal and unmarshal datetime values in String form.
27   * Converts Strings to java.util.Date instances and vice versa.
28   * 
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class StringToDateTimeAdapter extends XmlAdapter<String,Date> {
32  
33      /**
34       * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
35       */
36      @Override
37      public Date unmarshal(String v) throws Exception {
38          return (v != null) ? CoreApiServiceLocator.getDateTimeService().convertToDateTime(StringUtils.trim(v)) : null;
39      }
40  
41      /**
42       * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
43       */
44      @Override
45      public String marshal(Date v) throws Exception {
46          return (v != null) ? CoreApiServiceLocator.getDateTimeService().toDateTimeString(v) : null;
47      }
48  
49  }