001/**
002 * Copyright 2004-2014 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 */
016package org.kuali.student.contract.model.util;
017
018import java.text.DateFormat;
019import java.text.ParseException;
020import java.text.SimpleDateFormat;
021import java.util.Date;
022import java.util.TimeZone;
023
024import org.joda.time.DateTime;
025import org.joda.time.DateTimeZone;
026import org.joda.time.LocalDateTime;
027import org.joda.time.format.DateTimeFormat;
028import org.joda.time.format.DateTimeFormatter;
029import org.joda.time.format.DateTimeFormatterBuilder;
030import org.kuali.student.contract.exception.DictionaryExecutionException;
031
032/**
033 *
034 * @author nwright
035 */
036public 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}