001 /* 002 * Copyright 2010 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.osedu.org/licenses/ECL-2.0 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 023 import org.kuali.student.contract.exception.DictionaryExecutionException; 024 025 /** 026 * 027 * @author nwright 028 */ 029 public class DateUtility { 030 031 public String asYMD(String date) 032 throws ParseException { 033 if (date == null) { 034 return null; 035 } 036 return asYMD(asDate(date)); 037 } 038 039 public Date asDate(String date) 040 throws ParseException { 041 if (date == null) { 042 return null; 043 } 044 String[] formats = { 045 "yyyy-MM-dd", 046 "MM/dd/yyyy" 047 }; 048 ParseException pe = null; 049 for (int i = 0; i < formats.length; i++) { 050 DateFormat df = new SimpleDateFormat(formats[i]); 051 try { 052 return df.parse(date); 053 } catch (ParseException e) { 054 pe = e; 055 } 056 } 057 throw pe; 058 } 059 060 public String asYMD(Date date) { 061 if (date == null) { 062 return null; 063 } 064 DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 065 return df.format(date); 066 } 067 }