2 * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vfc.nfvo.emsdriver.commons.utils;
18 import java.text.ParseException;
19 import java.text.SimpleDateFormat;
20 import java.util.Calendar;
21 import java.util.Date;
23 public class DateUtil {
26 public static long[] getScanScope(Date fireTime, long collectPeriod) {
27 Calendar fire = Calendar.getInstance();
30 fire.setTime(fireTime);
31 fire.set(Calendar.SECOND,0);
32 fire.set(Calendar.MILLISECOND, 0);
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;
51 if (collectPeriod>0) {
52 end = fire.getTimeInMillis()-(fire.getTimeInMillis()+8*60*60*1000)%(collectPeriod*1000);
56 start = end - collectPeriod * 1000;
59 return new long[] {start,end};
62 public static String TimeString(String timeString){
63 if(timeString == null){
66 timeString = timeString.replace("T", " ");
67 if(timeString.contains("+")){
68 timeString = timeString.substring(0, timeString.indexOf("+"));
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());