1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
86
87 return formattedDate;
88 }
89 }