DependentDateUtils.java 6.2 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 30 31 32 33 34 35 36

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

public class DependentDateUtils {

    /**
     * get last day interval list
     * @param businessDate
     * @param hourNumber
     * @return
     */
    public static List<DateInterval> getLastHoursInterval(Date businessDate, int hourNumber){
        List<DateInterval> dateIntervals = new ArrayList<>();
        for(int index = hourNumber; index > 0; index--){
T
Tboy 已提交
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 47 48 49 50 51 52 53 54 55 56 57 58 59
    /**
     * get today day interval list
     * @param businessDate
     * @return
     */
    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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    /**
     * get last day interval list
     * @param businessDate
     * @param someDay
     * @return
     */
    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 81 82 83 84 85 86 87 88
    /**
     * get interval between this month first day and businessDate
     * @param businessDate
     * @return
     */
    public static List<DateInterval> getThisMonthInterval(Date businessDate) {
        Date firstDay = DateUtils.getFirstDayOfMonth(businessDate);
        return getDateIntervalListBetweenTwoDates(firstDay, businessDate);
    }

L
ligang 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    /**
     * get interval between last month first day and last day
     * @param businessDate
     * @return
     */
    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
     * @param businessDate
     * @param isBeginDay
     * @return
     */
    public static List<DateInterval> getLastMonthBeginInterval(Date businessDate, boolean isBeginDay) {

        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 已提交
121 122 123 124 125 126 127 128 129 130
    /**
     * get interval between monday to businessDate of this week
     * @param businessDate
     * @return
     */
    public static List<DateInterval> getThisWeekInterval(Date businessDate) {
        Date mondayThisWeek = DateUtils.getMonday(businessDate);
        return getDateIntervalListBetweenTwoDates(mondayThisWeek, businessDate);
    }

L
ligang 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    /**
     * get interval between monday to sunday of last week
     * default set monday the first day of week
     * @param businessDate
     * @return
     */
    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
     * @param businessDate
     * @param dayOfWeek monday:1,tuesday:2,wednesday:3,thursday:4,friday:5,saturday:6,sunday:7
     * @return
     */
    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);
    }

    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;
    }
}