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.system.fusion.adapter;
41 import java.io.Serializable;
42 import java.text.DateFormat;
43 import java.text.SimpleDateFormat;
44 import java.util.Date;
45 import java.util.List;
46 import java.util.TimeZone;
48 import org.onap.portalsdk.core.FusionObject;
49 import org.onap.portalsdk.core.domain.User;
50 import org.onap.portalsdk.core.service.DataAccessService;
51 import org.onap.portalsdk.core.util.SystemProperties;
52 import org.onap.portalsdk.core.web.support.AppUtils;
55 public class DateUtils implements Serializable, FusionObject{
57 public static final String US_PACIFIC = "US/Pacific";
58 public static final String US_MOUNTAIN = "US/Mountain";
59 public static final String US_CENTRAL = "US/Central";
60 public static final String US_EASTERN = "US/Eastern";
61 public static final String US_HAWAII = "US/Hawaii";
62 public static final String US_ALASKA = "US/Alaska";
64 //Arizona State has Mountain Time with no Daylight Savings
65 public static final String US_ARIZONA = "America/Phoenix";
67 private static final String DB_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
68 private static final String GET_CURRENT_DATE = "getCurrentDate";
70 private static DataAccessService dataAccessService;
72 public static DataAccessService getDataAccessService() {
73 return dataAccessService;
76 public void setDataAccessService(DataAccessService dataAccessService) {
77 this.dataAccessService = dataAccessService;
81 * Parses a date value with given pattern,
82 * to return a Date Object
90 public static Date parseDate(String dateValue,String inPattern) throws Exception{
91 return parseDate(dateValue,inPattern,null);
95 * Parses a date value with the given pattern for the specific TimeZone,
96 * to return a Date Object
100 * @param currentTimeZone
101 * @return Date Object
105 public static Date parseDate(String dateValue,String inPattern,
106 String currentTimeZone) throws Exception{
107 DateFormat df = new SimpleDateFormat(inPattern);
108 if(currentTimeZone !=null && !(currentTimeZone.trim().equals(""))){
109 df.setTimeZone(TimeZone.getTimeZone(currentTimeZone));
111 Date date = df.parse(dateValue);
116 * Parses a date value with the given pattern for the specific User(in User TimeZone),
117 * to return a Date Object
122 * @return Date Object
126 public static Date parseUserDate(String dateValue, String inPattern, Long userId) throws Exception{
127 User user = (User)getDataAccessService().getDomainObject(User.class, userId, null);
129 String userTimeZone = null;
130 Long timezoneId = user.getTimeZoneId();
132 if (timezoneId != null) {
133 userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
136 return parseDate(dateValue,inPattern,userTimeZone);
140 * Formats a given date object to the desired pattern
144 * @return Formatted date value
147 public static String formatDate(Date date,String outPattern)throws Exception{
148 return formatDate(date,outPattern,null);
152 * Formats a date value with the given pattern into a date value with the desired pattern
157 * @return Formatted date value
161 public static String formatDate(String dateValue,String inPattern,
162 String outPattern) throws Exception{
163 return formatDate(dateValue,inPattern,null,outPattern,null);
167 * Formats a given date object to the desired pattern for the TimeZone provided
170 * @param requiredTimeZone
171 * @return Formatted date value
174 public static String formatDate(Date date,String outPattern,
175 String requiredTimeZone) throws Exception{
176 DateFormat df = new SimpleDateFormat(outPattern);
177 if(requiredTimeZone != null && !requiredTimeZone.trim().equals("")){
178 df.setTimeZone(TimeZone.getTimeZone(requiredTimeZone));
180 return df.format(date);
184 * Formats a date value with the given pattern
185 * into a date value with the desired pattern for the TimeZone provided
190 * @param requiredTimeZone
191 * @return Formatted date value
195 public static String formatDate(String dateValue,String inPattern,
196 String outPattern,String requiredTimeZone) throws Exception{
197 return formatDate(dateValue,inPattern,null,outPattern,requiredTimeZone);
201 * Formats a date value with the given pattern for a specific TimeZone,
202 * into a date value with the desired pattern for the TimeZone provided
206 * @param currentTimeZone
208 * @param requiredTimeZone
209 * @return Formatted date value
213 public static String formatDate(String dateValue,String inPattern,String currentTimeZone,
214 String outPattern,String requiredTimeZone) throws Exception{
215 Date date = parseDate(dateValue,inPattern,currentTimeZone);
216 return formatDate(date,outPattern,requiredTimeZone);
220 * Formats a date value with the given pattern, for a specific User(in User TimeZone),
221 * into a date value with the desired pattern for the TimeZone provided
227 * @param requiredTimeZone
228 * @return Formatted date value
232 public static String formatUserDate(String dateValue,String inPattern, Long userId,String outPattern,String requiredTimeZone) throws Exception{
233 User user = (User)getDataAccessService().getDomainObject(User.class, userId, null);
235 String userTimeZone = null;
236 Long timezoneId = user.getTimeZoneId();
238 if (timezoneId != null) {
239 userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
242 return formatDate(dateValue,inPattern,userTimeZone,outPattern,requiredTimeZone);
246 * Formats a date value with a given pattern for a specific User(User TimeZone),
247 * into a date value with the desired pattern for Database TimeZone
253 * @return Formatted date value
257 public static String formatUserDateForDBTimeZone(String dateValue,String inPattern, Long userId,String outPattern) throws Exception{
258 User user = (User)getDataAccessService().getDomainObject(User.class, userId, null);
260 String userTimeZone = null;
261 Long timezoneId = user.getTimeZoneId();
263 /*if (timezoneId != null) {
264 userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
267 String dbTimeZone = SystemProperties.getProperty(SystemProperties.DATABASE_TIME_ZONE);
269 return formatDate(dateValue,inPattern,userTimeZone,outPattern,dbTimeZone);
273 * Get the current database Date/Time
274 * @return Date object
276 public static Date getCurrentDBDate()throws Exception{
277 String dbTimeZone = SystemProperties.getProperty(SystemProperties.DATABASE_TIME_ZONE);
278 List results = (List)getDataAccessService().executeNamedQuery(GET_CURRENT_DATE, null, null);
279 return parseDate(((Object[])results.get(0))[0]+" "+((Object[])results.get(0))[1],DB_DATE_FORMAT,dbTimeZone);
283 * Get the current date value formatted for the User's TimeZone in the desired pattern
290 public static String getCurrentDBDateForUser(String outPattern,Long userId)throws Exception{
291 User user = (User)getDataAccessService().getDomainObject(User.class, userId, null);
293 String userTimeZone = null;
294 Long timezoneId = user.getTimeZoneId();
296 /*if (timezoneId != null) {
297 userTimeZone = AppUtils.getLookupValueByLabel(timezoneId.toString(), "fn_lu_timezone", "timezone_id", "timezone_value");
300 Date dbDate = getCurrentDBDate();
302 return formatDate(dbDate,outPattern,userTimeZone);