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 {
25 public static long[] getScanScope(Date fireTime, long collectPeriod) {
26 Calendar fire = Calendar.getInstance();
29 fire.setTime(fireTime);
30 fire.set(Calendar.SECOND, 0);
31 fire.set(Calendar.MILLISECOND, 0);
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;
50 if (collectPeriod > 0) {
51 end = fire.getTimeInMillis() - (fire.getTimeInMillis() + 8 * 60 * 60 * 1000) % (collectPeriod * 1000);
55 start = end - collectPeriod * 1000;
58 return new long[]{start, end};
61 public static String getTimeString(String timeString) {
62 if (timeString == null) {
65 timeString = timeString.replace("T", " ");
66 if (timeString.contains("+")) {
67 timeString = timeString.substring(0, timeString.indexOf("+"));
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());