2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalsdk.analytics.model.runtime;
40 import java.text.ParseException;
41 import java.text.SimpleDateFormat;
42 import java.util.Date;
43 import java.util.GregorianCalendar;
45 import org.onap.portalsdk.analytics.system.Globals;
46 import org.onap.portalsdk.analytics.util.AppConstants;
47 import org.onap.portalsdk.analytics.util.Utils;
49 public class ReportParamDateValueParser {
51 /*public static final SimpleDateFormat[] dateFormats;
54 dateFormats = new SimpleDateFormat[5];
55 (dateFormats[0] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHYYYY))
57 (dateFormats[1] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHDDYYYY))
59 (dateFormats[2] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMDDYYYY))
61 (dateFormats[3] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMYYYY))
63 (dateFormats[4] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_DDMONYYYY))
67 public static boolean isDateHrParam(String param) {
68 SimpleDateFormat[] dateFormats = new SimpleDateFormat[2];
69 dateFormats[0] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMDDYYYY_HR);
70 dateFormats[0].setLenient(true);
71 dateFormats[1] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_DDMONYYYY_HR);
72 dateFormats[1].setLenient(true);
73 for (int i = 0; i < dateFormats.length; i++) {
75 if (dateFormats[i].parse(param) != null) {
78 } catch (ParseException | NumberFormatException pe) {
79 // do nothing, continue to check param against other dates
84 public static boolean isDateParam(String param) {
85 SimpleDateFormat[] dateFormats = new SimpleDateFormat[5];
86 dateFormats[0] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHYYYY);
87 dateFormats[0].setLenient(true);
88 dateFormats[1] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHDDYYYY);
89 dateFormats[1].setLenient(true);
90 dateFormats[2] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMDDYYYY);
91 dateFormats[2].setLenient(true);
92 dateFormats[3] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMYYYY);
93 dateFormats[3].setLenient(true);
94 dateFormats[4] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_DDMONYYYY);
95 dateFormats[4].setLenient(true);
97 for (int i = 0; i < dateFormats.length; i++) {
99 if (dateFormats[i].parse(param) != null) {
102 } catch (ParseException | NumberFormatException pe) {
103 // do nothing, continue to check param against other dates
109 public static String formatDateParamValue(String param) {
110 return ReportParamDateValueParser.formatDateParamValue(param, null);
113 public static String formatDateHrParamValue(String param) {
114 return ReportParamDateValueParser.formatDateHrParamValue(param, null);
117 public static String formatDateHrParamValue(String param, String dateHrFormatPattern) {
118 String formattedDate = null;
119 Date parsedDate = null;
121 dateHrFormatPattern = (dateHrFormatPattern != null) ? dateHrFormatPattern
124 SimpleDateFormat[] dateFormats = new SimpleDateFormat[2];
125 dateFormats[0] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMDDYYYY_HR);
126 dateFormats[0].setLenient(true);
127 dateFormats[1] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_DDMONYYYY_HR);
128 dateFormats[1].setLenient(true);
130 for (int i = 0; i < dateFormats.length; i++) {
132 if (dateFormats[i].parse(param) != null) {
133 SimpleDateFormat newDateFormat = new SimpleDateFormat(dateHrFormatPattern);
134 parsedDate = dateFormats[i].parse(param);
135 formattedDate = newDateFormat.format(parsedDate);
138 return formattedDate;
140 } catch (ParseException | NumberFormatException pe) {
141 // do nothing, continue to check param against other dates and
142 // format accordingly
149 public static String formatDateParamValue(String param, String dateFormatPattern) {
150 String formattedDate = null;
151 Date parsedDate = null;
153 dateFormatPattern = (dateFormatPattern != null) ? dateFormatPattern
154 : AppConstants.JAVA_DATE_FORMAT_MMDDYYYY;
156 SimpleDateFormat[] dateFormats = new SimpleDateFormat[5];
157 dateFormats[0] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHYYYY);
158 dateFormats[0].setLenient(true);
159 dateFormats[1] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MONTHDDYYYY);
160 dateFormats[1].setLenient(true);
161 dateFormats[2] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMDDYYYY);
162 dateFormats[2].setLenient(true);
163 dateFormats[3] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_MMYYYY);
164 dateFormats[3].setLenient(true);
165 dateFormats[4] = new SimpleDateFormat(AppConstants.JAVA_DATE_FORMAT_DDMONYYYY);
166 dateFormats[4].setLenient(true);
168 for (int i = 0; i < dateFormats.length; i++) {
170 if (dateFormats[i].parse(param) == null) {
174 SimpleDateFormat newDateFormat = new SimpleDateFormat(dateFormatPattern);
175 parsedDate = dateFormats[i].parse(param);
176 formattedDate = newDateFormat.format(parsedDate);
178 if (Globals.getMonthFormatUseLastDay()
179 && (dateFormats[i].toPattern().equals(
180 AppConstants.JAVA_DATE_FORMAT_MMYYYY) || dateFormats[i]
182 AppConstants.JAVA_DATE_FORMAT_MONTHYYYY))) {
184 GregorianCalendar gc = new GregorianCalendar();
185 gc.setTime(parsedDate);
186 int day = gc.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
187 formattedDate = Utils.replaceInString(formattedDate, "/01/", "/"
192 return formattedDate;
194 } catch (ParseException | NumberFormatException pe) {
195 // do nothing, continue to check param against other dates and
196 // format accordingly
202 } // ReportParamValues