DependentDateUtils.java 6.7 KB
Newer Older
L
ligang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Q
qiaozhanwei 已提交
17
package org.apache.dolphinscheduler.common.utils.dependent;
L
ligang 已提交
18

Q
qiaozhanwei 已提交
19 20
import org.apache.dolphinscheduler.common.model.DateInterval;
import org.apache.dolphinscheduler.common.utils.DateUtils;
L
ligang 已提交
21 22 23 24 25 26 27 28 29

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DependentDateUtils {

    /**
     * get last day interval list
30 31 32
     * @param businessDate businessDate
     * @param hourNumber hourNumber
     * @return DateInterval list
L
ligang 已提交
33 34 35 36
     */
    public static List<DateInterval> getLastHoursInterval(Date businessDate, int hourNumber){
        List<DateInterval> dateIntervals = new ArrayList<>();
        for(int index = hourNumber; index > 0; index--){
37
            Date lastHour = DateUtils.getSomeHourOfDay(businessDate, -index);
L
ligang 已提交
38 39 40 41 42 43 44
            Date beginTime = DateUtils.getStartOfHour(lastHour);
            Date endTime = DateUtils.getEndOfHour(lastHour);
            dateIntervals.add(new DateInterval(beginTime, endTime));
        }
        return dateIntervals;
    }

B
baoliang 已提交
45 46
    /**
     * get today day interval list
47 48
     * @param businessDate businessDate
     * @return DateInterval list
B
baoliang 已提交
49 50 51 52 53 54 55 56 57 58 59
     */
    public static List<DateInterval> getTodayInterval(Date businessDate){

        List<DateInterval> dateIntervals = new ArrayList<>();

        Date beginTime = DateUtils.getStartOfDay(businessDate);
        Date endTime = DateUtils.getEndOfDay(businessDate);
        dateIntervals.add(new DateInterval(beginTime, endTime));
        return dateIntervals;
    }

L
ligang 已提交
60 61
    /**
     * get last day interval list
62 63 64
     * @param businessDate businessDate
     * @param someDay someDay
     * @return DateInterval list
L
ligang 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78
     */
    public static List<DateInterval> getLastDayInterval(Date businessDate, int someDay){

        List<DateInterval> dateIntervals = new ArrayList<>();
        for(int index = someDay; index > 0; index--){
            Date lastDay = DateUtils.getSomeDay(businessDate, -index);

            Date beginTime = DateUtils.getStartOfDay(lastDay);
            Date endTime = DateUtils.getEndOfDay(lastDay);
            dateIntervals.add(new DateInterval(beginTime, endTime));
        }
        return dateIntervals;
    }

feloxx's avatar
feloxx 已提交
79 80
    /**
     * get interval between this month first day and businessDate
81 82
     * @param businessDate businessDate
     * @return DateInterval list
feloxx's avatar
feloxx 已提交
83 84 85 86 87 88
     */
    public static List<DateInterval> getThisMonthInterval(Date businessDate) {
        Date firstDay = DateUtils.getFirstDayOfMonth(businessDate);
        return getDateIntervalListBetweenTwoDates(firstDay, businessDate);
    }

L
ligang 已提交
89 90
    /**
     * get interval between last month first day and last day
91 92
     * @param businessDate businessDate
     * @return DateInterval list
L
ligang 已提交
93 94 95 96 97 98 99 100 101 102 103 104
     */
    public static List<DateInterval> getLastMonthInterval(Date businessDate) {

        Date firstDayThisMonth = DateUtils.getFirstDayOfMonth(businessDate);
        Date lastDay = DateUtils.getSomeDay(firstDayThisMonth, -1);
        Date firstDay = DateUtils.getFirstDayOfMonth(lastDay);
        return getDateIntervalListBetweenTwoDates( firstDay, lastDay);
    }


    /**
     * get interval on first/last day of the last month
105 106 107
     * @param businessDate businessDate
     * @param isBeginDay isBeginDay
     * @return DateInterval list
L
ligang 已提交
108
     */
109 110
    public static List<DateInterval> getLastMonthBeginInterval(Date businessDate,
                                                               boolean isBeginDay) {
L
ligang 已提交
111 112 113 114 115 116 117 118 119 120 121

        Date firstDayThisMonth = DateUtils.getFirstDayOfMonth(businessDate);
        Date lastDay = DateUtils.getSomeDay(firstDayThisMonth, -1);
        Date firstDay = DateUtils.getFirstDayOfMonth(lastDay);
        if(isBeginDay){
            return getDateIntervalListBetweenTwoDates(firstDay, firstDay);
        }else{
            return getDateIntervalListBetweenTwoDates(lastDay, lastDay);
        }
    }

feloxx's avatar
feloxx 已提交
122 123
    /**
     * get interval between monday to businessDate of this week
124 125
     * @param businessDate businessDate
     * @return DateInterval list
feloxx's avatar
feloxx 已提交
126 127 128 129 130 131
     */
    public static List<DateInterval> getThisWeekInterval(Date businessDate) {
        Date mondayThisWeek = DateUtils.getMonday(businessDate);
        return getDateIntervalListBetweenTwoDates(mondayThisWeek, businessDate);
    }

L
ligang 已提交
132 133 134
    /**
     * get interval between monday to sunday of last week
     * default set monday the first day of week
135 136
     * @param businessDate businessDate
     * @return DateInterval list
L
ligang 已提交
137 138 139 140 141 142 143 144 145 146 147
     */
    public static List<DateInterval> getLastWeekInterval(Date businessDate) {
        Date mondayThisWeek = DateUtils.getMonday(businessDate);
        Date sunday = DateUtils.getSomeDay(mondayThisWeek, -1);
        Date monday = DateUtils.getMonday(sunday);
        return getDateIntervalListBetweenTwoDates(monday, sunday);
    }

    /**
     * get interval on the day of last week
     * default set monday the first day of week
148
     * @param businessDate businessDate
L
ligang 已提交
149
     * @param dayOfWeek monday:1,tuesday:2,wednesday:3,thursday:4,friday:5,saturday:6,sunday:7
150
     * @return DateInterval list
L
ligang 已提交
151 152 153 154 155 156 157 158 159
     */
    public static List<DateInterval> getLastWeekOneDayInterval(Date businessDate, int dayOfWeek) {
        Date mondayThisWeek = DateUtils.getMonday(businessDate);
        Date sunday = DateUtils.getSomeDay(mondayThisWeek, -1);
        Date monday = DateUtils.getMonday(sunday);
        Date destDay = DateUtils.getSomeDay(monday, dayOfWeek -1);
        return getDateIntervalListBetweenTwoDates(destDay, destDay);
    }

160 161 162 163 164 165
    /**
     * get date interval list between two dates
     * @param firstDay firstDay
     * @param lastDay lastDay
     * @return DateInterval list
     */
L
ligang 已提交
166 167 168 169 170 171 172 173 174 175 176
    public static List<DateInterval> getDateIntervalListBetweenTwoDates(Date firstDay, Date lastDay) {
        List<DateInterval> dateIntervals = new ArrayList<>();
        while(!firstDay.after(lastDay)){
            Date beginTime = DateUtils.getStartOfDay(firstDay);
            Date endTime = DateUtils.getEndOfDay(firstDay);
            dateIntervals.add(new DateInterval(beginTime, endTime));
            firstDay = DateUtils.getSomeDay(firstDay, 1);
        }
        return dateIntervals;
    }
}