Modify emsdriver Code
[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         
26         public static long[] getScanScope(Date fireTime, long collectPeriod) {
27                 Calendar fire = Calendar.getInstance();
28                 long start = 0L;
29                 long end = 0L;
30                 fire.setTime(fireTime);
31                 fire.set(Calendar.SECOND,0);
32                 fire.set(Calendar.MILLISECOND, 0);
33                 
34                 if (collectPeriod < 3600) {//minute
35                         long minite = fire.get(Calendar.MINUTE);
36                         long collectMinite = (int)collectPeriod/60;
37                         long s = minite%collectMinite;  
38                         end = fire.getTimeInMillis() - s*60*1000;
39                         start = end - collectPeriod * 1000;
40                 }else if (collectPeriod == 3600) {
41                         fire.set(Calendar.MINUTE, 0);  
42                         end = fire.getTimeInMillis();
43                         start = end - collectPeriod * 1000;
44                 }else if (collectPeriod == 24*60*60){ //day
45                         fire.set(Calendar.HOUR_OF_DAY, 0);
46                         fire.set(Calendar.MINUTE, 0);
47                         end = fire.getTimeInMillis();
48                         start = end - collectPeriod * 1000;
49                 }else{
50                         
51                         if (collectPeriod>0) { 
52                                 end = fire.getTimeInMillis()-(fire.getTimeInMillis()+8*60*60*1000)%(collectPeriod*1000);
53                         }else {
54                                 return null;
55                         }
56                         start = end - collectPeriod * 1000;
57                 }
58                 
59                 return new long[] {start,end};
60         }
61         
62         public static String TimeString(String timeString){
63                 if(timeString == null){
64                         return "";
65                 }else{
66                         timeString = timeString.replace("T", " ");
67                         if(timeString.contains("+")){
68                                 timeString = timeString.substring(0, timeString.indexOf("+"));
69                         }
70                         return timeString;
71                 }
72         }
73         
74         public static String addTime(String srcTimeString,String period) throws ParseException{
75                 String finaldate = TimeString(srcTimeString);
76                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
77                 Date date = sdf.parse(finaldate);
78                 Calendar calendar = Calendar.getInstance();
79                 calendar.setTime(date);
80                 calendar.add(Calendar.MINUTE, Integer.valueOf(period));
81                 return sdf.format(calendar.getTime());
82         }
83
84 }