View Javadoc

1   /*
2    * Copyright 2010 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.osedu.org/licenses/ECL-2.0
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.student.contract.model.util;
17  
18  import java.text.DateFormat;
19  import java.text.ParseException;
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  import java.util.TimeZone;
23  
24  import org.joda.time.DateTime;
25  import org.joda.time.DateTimeZone;
26  import org.joda.time.LocalDateTime;
27  import org.joda.time.format.DateTimeFormat;
28  import org.joda.time.format.DateTimeFormatter;
29  import org.joda.time.format.DateTimeFormatterBuilder;
30  import org.kuali.student.contract.exception.DictionaryExecutionException;
31  
32  /**
33   *
34   * @author nwright
35   */
36  public class DateUtility {
37  
38      public static String asYMD(String date)
39              throws ParseException {
40          if (date == null) {
41              return null;
42          }
43          return asYMD(asDate(date));
44      }
45  
46      public static Date asDate(String date)
47              throws ParseException {
48          if (date == null) {
49              return null;
50          }
51          String[] formats = {
52              "yyyy-MM-dd",
53              "MM/dd/yyyy"
54          };
55          ParseException pe = null;
56          for (int i = 0; i < formats.length; i++) {
57              DateFormat df = new SimpleDateFormat(formats[i]);
58              try {
59                  return df.parse(date);
60              } catch (ParseException e) {
61                  pe = e;
62              }
63          }
64          throw pe;
65      }
66  
67      public static String asYMD(Date date) {
68          if (date == null) {
69              return null;
70          }
71          DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
72          return df.format(date);
73      }
74      
75      private static DateTimeFormatter adjustToEasternTimeZoneFormatter = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm zzz");
76      
77      public static String asYMDHMInEasternTimeZone (DateTime date) {
78      	if (date == null)
79      		return null;
80      	
81      	DateTime adjustedDate = date.withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/Toronto")));
82      	
83      	String formattedDate = adjustToEasternTimeZoneFormatter.print(adjustedDate);
84      	
85  //    	DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz", Locale.);
86  
87      	return formattedDate;
88      }
89  }