001    /**
002     * Copyright 2010 The Kuali Foundation Licensed under the
003     * Educational Community License, Version 2.0 (the "License"); you may
004     * not use this file except in compliance with the License. You may
005     * obtain a copy of the License at
006     *
007     * http://www.osedu.org/licenses/ECL-2.0
008     *
009     * Unless required by applicable law or agreed to in writing,
010     * software distributed under the License is distributed on an "AS IS"
011     * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012     * or implied. See the License for the specific language governing
013     * permissions and limitations under the License.
014     */
015    
016    package org.kuali.student.common.validator;
017    
018    import java.text.SimpleDateFormat;
019    import java.util.Date;
020    
021    public class ServerDateParser implements DateParser {
022        SimpleDateFormat[] formats = {
023                    new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"), 
024                    new SimpleDateFormat("yyyy-MM-dd"), 
025                    new SimpleDateFormat("yyyy-MMM-dd"),
026                    new SimpleDateFormat("dd-MM-yyyy"),
027                    new SimpleDateFormat("dd-MMM-yyyy")
028        };
029        
030        public Date parseDate(String input) {
031            Date result = null;
032            
033            for (SimpleDateFormat format : formats) {
034                    try {
035                        result = format.parse(input);
036                    } catch (Exception e) {
037                        // just eat it
038                    }
039                    if (result != null) {
040                        break;
041                    }
042                
043            }
044            
045            if (result == null) {
046                throw new DateParseException("Invalid date value: " + input);
047            }
048            
049            return result;
050        }
051    
052        /**
053         * @see org.kuali.student.common.validator.old.DateParser#toString(java.util.Date)
054         */
055        @Override
056        public String toString(Date date) {
057            String result = null;
058            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss,SSS");
059            result = format.format(date);
060    
061            return result;        
062        }
063    }