Code improvements in Common Utils
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / commons / utils / DateUtil.java
1 /*
2  * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-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.onap.vfc.nfvo.emsdriver.commons.utils;
17
18 import java.text.ParseException;
19 import java.text.SimpleDateFormat;
20 import java.util.Calendar;
21 import java.util.Date;
22
23 public class DateUtil {
24
25     public static long[] getScanScope(Date fireTime, long collectPeriod) {
26         Calendar fire = Calendar.getInstance();
27         long start ;
28         long end ;
29         fire.setTime(fireTime);
30         fire.set(Calendar.SECOND, 0);
31         fire.set(Calendar.MILLISECOND, 0);
32
33         if (collectPeriod < 3600) {//minute
34             long minite = fire.get(Calendar.MINUTE);
35             long collectMinite = (int) collectPeriod / 60;
36             long s = minite % collectMinite;
37             end = fire.getTimeInMillis() - s * 60 * 1000;
38             start = end - collectPeriod * 1000;
39         } else if (collectPeriod == 3600) {
40             fire.set(Calendar.MINUTE, 0);
41             end = fire.getTimeInMillis();
42             start = end - collectPeriod * 1000;
43         } else if (collectPeriod == 24 * 60 * 60) { //day
44             fire.set(Calendar.HOUR_OF_DAY, 0);
45             fire.set(Calendar.MINUTE, 0);
46             end = fire.getTimeInMillis();
47             start = end - collectPeriod * 1000;
48         } else {
49
50             if (collectPeriod > 0) {
51                 end = fire.getTimeInMillis() - (fire.getTimeInMillis() + 8 * 60 * 60 * 1000) % (collectPeriod * 1000);
52             } else {
53                 return new long[0];
54             }
55             start = end - collectPeriod * 1000;
56         }
57
58         return new long[]{start, end};
59     }
60
61     public static String getTimeString(String timeString) {
62         if (timeString == null) {
63             return "";
64         } else {
65             timeString = timeString.replace("T", " ");
66             if (timeString.contains("+")) {
67                 timeString = timeString.substring(0, timeString.indexOf("+"));
68             }
69             return timeString;
70         }
71     }
72
73     public static String addTime(String srcTimeString, String period) throws ParseException {
74             String finaldate = getTimeString(srcTimeString);
75             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
76             Date date = sdf.parse(finaldate);
77             Calendar calendar = Calendar.getInstance();
78             calendar.setTime(date);
79             calendar.add(Calendar.MINUTE, Integer.valueOf(period));
80             return sdf.format(calendar.getTime());
81     }
82
83 }