001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.student.contract.model.util;
017    
018    import java.text.DateFormat;
019    import java.text.ParseException;
020    import java.text.SimpleDateFormat;
021    import java.util.Date;
022    import java.util.TimeZone;
023    
024    import org.joda.time.DateTime;
025    import org.joda.time.DateTimeZone;
026    import org.joda.time.LocalDateTime;
027    import org.joda.time.format.DateTimeFormat;
028    import org.joda.time.format.DateTimeFormatter;
029    import org.joda.time.format.DateTimeFormatterBuilder;
030    import org.kuali.student.contract.exception.DictionaryExecutionException;
031    
032    /**
033     *
034     * @author nwright
035     */
036    public class DateUtility {
037    
038        public static String asYMD(String date)
039                throws ParseException {
040            if (date == null) {
041                return null;
042            }
043            return asYMD(asDate(date));
044        }
045    
046        public static Date asDate(String date)
047                throws ParseException {
048            if (date == null) {
049                return null;
050            }
051            String[] formats = {
052                "yyyy-MM-dd",
053                "MM/dd/yyyy"
054            };
055            ParseException pe = null;
056            for (int i = 0; i < formats.length; i++) {
057                DateFormat df = new SimpleDateFormat(formats[i]);
058                try {
059                    return df.parse(date);
060                } catch (ParseException e) {
061                    pe = e;
062                }
063            }
064            throw pe;
065        }
066    
067        public static String asYMD(Date date) {
068            if (date == null) {
069                return null;
070            }
071            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
072            return df.format(date);
073        }
074        
075        private static DateTimeFormatter adjustToEasternTimeZoneFormatter = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm zzz");
076        
077        public static String asYMDHMInEasternTimeZone (DateTime date) {
078            if (date == null)
079                    return null;
080            
081            DateTime adjustedDate = date.withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/Toronto")));
082            
083            String formattedDate = adjustToEasternTimeZoneFormatter.print(adjustedDate);
084            
085    //      DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz", Locale.);
086    
087            return formattedDate;
088        }
089    }