View Javadoc
1   package org.kuali.ole.deliver.service;
2   
3   import java.sql.Timestamp;
4   import java.util.HashMap;
5   import java.util.Map;
6   import java.util.StringTokenizer;
7   
8   /**
9    * Created by pvsubrah on 4/3/15.
10   */
11  public class DateFormatHelper {
12  
13      private static DateFormatHelper dateFormatHelper;
14      private static Map map;
15  
16      private DateFormatHelper() {
17          map = new HashMap();
18          map.put("1", "Jan");
19          map.put("01", "Jan");
20          map.put("2", "Feb");
21          map.put("02", "Feb");
22          map.put("3", "Mar");
23          map.put("03", "Mar");
24          map.put("4", "Apr");
25          map.put("04", "Apr");
26          map.put("5", "May");
27          map.put("05", "May");
28          map.put("6", "Jun");
29          map.put("06", "Jun");
30          map.put("7", "Jul");
31          map.put("07", "Jul");
32          map.put("8", "Aug");
33          map.put("08", "Aug");
34          map.put("9", "Sep");
35          map.put("09", "Sep");
36          map.put("10", "Oct");
37          map.put("11", "Nov");
38          map.put("12", "Dec");
39      }
40  
41      public static DateFormatHelper getInstance() {
42          if (null == dateFormatHelper) {
43              dateFormatHelper = new DateFormatHelper();
44          }
45          return dateFormatHelper;
46      }
47  
48      public String generateDateStringsForMySQL(String dateString) {
49          StringBuilder formattedDate = new StringBuilder();
50  
51          StringTokenizer stringTokenizer = new StringTokenizer(dateString, "/");
52          while (stringTokenizer.hasMoreTokens()) {
53              String month = stringTokenizer.nextToken();
54              String day = stringTokenizer.nextToken();
55              String year = stringTokenizer.nextToken();
56              formattedDate.append("'").append(month).append(",").append(day).append(",").append(year).append("'").append(",'").append
57                      ("%m,%d,%Y").append("'");
58          }
59          return formattedDate.toString();
60      }
61  
62      public String generateDateStringsForMySQL(Timestamp timestamp) {
63  
64          StringBuilder formattedDate = new StringBuilder();
65  
66          String dateTimeString = timestamp.toString();
67  
68          StringTokenizer dateTimeTokenizer = new StringTokenizer(dateTimeString, " ");
69          String date = dateTimeTokenizer.nextToken();
70          String time = dateTimeTokenizer.nextToken();
71  
72          StringTokenizer dateTokenizer = new StringTokenizer(date, "-");
73  
74          String year = dateTokenizer.nextToken();
75          String month = dateTokenizer.nextToken();
76          String day = dateTokenizer.nextToken();
77  
78          StringTokenizer timeTokenizer = new StringTokenizer(time, ":");
79          String hours = timeTokenizer.nextToken();
80          String minutes = timeTokenizer.nextToken();
81          String seconds = timeTokenizer.nextToken();
82  
83          formattedDate.append("'").append(month).append(",").append(day).append(",").append(year).append(",").append(hours+":"+minutes+":"+seconds.substring(0,2)).append("'").append(",'").append
84                  ("%m,%d,%Y,%H:%i:%s").append("'");
85  
86  
87          return formattedDate.toString();
88      }
89  
90  
91      public String generateDateStringsForOracle(String dateString) {
92          StringBuilder formattedDate = new StringBuilder();
93  
94          StringTokenizer stringTokenizer = new StringTokenizer(dateString, "/");
95          while (stringTokenizer.hasMoreTokens()) {
96              String month = stringTokenizer.nextToken();
97              String day = stringTokenizer.nextToken();
98              String year = stringTokenizer.nextToken();
99              formattedDate.append(day).append("-").append(getOracleMonth(month)).append("-").append(year);
100         }
101         return formattedDate.toString();
102     }
103 
104 
105     public String generateDateStringsForOracle(Timestamp timestamp) {
106         StringBuilder formattedDate = new StringBuilder();
107 
108         String dateTimeString = timestamp.toString();
109 
110         StringTokenizer dateTimeTokenizer = new StringTokenizer(dateTimeString, " ");
111         String date = dateTimeTokenizer.nextToken();
112         String time = dateTimeTokenizer.nextToken();
113 
114         StringTokenizer dateTokenizer = new StringTokenizer(date, "-");
115 
116         String year = dateTokenizer.nextToken();
117         String month = dateTokenizer.nextToken();
118         String day = dateTokenizer.nextToken();
119 
120         StringTokenizer timeTokenizer = new StringTokenizer(time, ":");
121         String hours = timeTokenizer.nextToken();
122         String minutes = timeTokenizer.nextToken();
123         String seconds = timeTokenizer.nextToken().substring(0, 2);
124 
125         formattedDate.append("'").append(year).append("/").append(month).append("/").append(day).append(" ").append(hours+":"+minutes+":"+seconds).append("'").append(",'").append
126                 ("YYYY/MM/DD HH24:MI:SS").append("'");
127 
128         return formattedDate.toString();
129 
130     }
131 
132 
133     private String getOracleMonth(String month) {
134 
135         return (String) map.get(month);
136     }
137 
138 }