Java divides the number of days of use and the time of daily use according to the start and end time

Demand, there is now a thing that is used across the day, I need to calculate how much time he uses every day

Time period entity:

import lombok.Data; 

import java.text.SimpleDateFormat;
 import java.util.Date; 

/ ** 
 * time period 
 * / 
@Data 
public  class DateRange { 

    private Date begin; // start time 

    private Date end; // end time 

    / / Just for easy viewing, do n’t use this ghost in actual development. 
    Public String toString () {
         return "{" + new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"). Format (begin) + "," + new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"). Format (end) + "}" ; 
    } 

}

Calculation method, both methods will do

  /**
     * 按小时计算,分隔
     *
     * @param begin
     * @param end
     * @return
     */
    public static List<DateRange> splitDateRangeByHour(Date begin, Date end) {
        System.out.println("按小时切割");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(begin);
        List<DateRange> list = Lists.newArrayList();
        DateRange range = new DateRange();
        range.setBegin(begin);
        while (true) { 
            calendar.add (Calendar.HOUR_OF_DAY, 1 );
             if (calendar.getTime (). after (end)) { 
                range.setEnd (end); 
                list.add (range); 
                break ; 
            } 
            if (calendar.get ( Calendar.HOUR_OF_DAY) == 23 ) { 
                calendar.set (Calendar.MINUTE, 59 ); 
                calendar.set (Calendar.SECOND, 59 ); 
                calendar.set (Calendar.MILLISECOND, 0 ); 
                range.setEnd (calendar.getTime ( )); 
                list.add (range); 
                // Calculate the next day and create a new time period
                calendar.add (Calendar.SECOND, 1 ); 
                range = new DateRange (); 
                range.setBegin (calendar.getTime ()); 
                continue ; 
            } 
        } 
        return list; 
    } 

    / ** 
     * Separated by day calculation (high efficiency, only There is a problem for more than one day) 
     * 
     * @param begin 
     * @param end 
     * @return 
     * / 
    public  static List <DateRange> splitDateRangeByDay (Date begin, Date end) { 
        System.out.println ( "Cut by day" );
         long time = end.getTime() - begin.getTime();
        if (time == 0) {
            return Lists.newArrayList();
        }
        long hours = time / (60 * 60 * 1000);
        if (time % (60 * 60 * 1000) > 0) {
            hours += 1;
        }
        if (hours <= 24) {
            return splitDateRangeByHour(begin, end);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(begin);
        List<DateRange> list = Lists.newArrayList();
        DateRange range = new DateRange();
        range.setBegin(begin);
        while (true) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            if (calendar.getTime().after(end)) {
                range.setEnd(end);
                list.add(range);
                break;
            }
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
             // Start of today 
            Date tmpBeginTime = calendar.getTime ();
             // Calculate the last second of the previous day 
            calendar.add (Calendar.SECOND, -1 ); 
            range.setEnd (calendar.getTime ()); 
            list .add (range); 
            // Create a new time 
            range range = new DateRange (); 
            range.setBegin (tmpBeginTime); 
            // Back to today 
            calendar.add (Calendar.SECOND, +1 ); 
        } 
        return list; 
    }

Tests and results

//按天
   public static void main(String[] args) throws ParseException {
        Date begin = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-02-27 08:00:12");
        Date end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-03-03 12:01:08");
        List<DateRange> dateRanges = splitDateRangeByDay(begin, end);
        System.out.println(dateRanges);
    }

输出
2019-02-28 08:00:12
2019-03-01 00:00:00
2019-03-02 00:00:00
2019-03-03 00:00:00
2019-03-04 00:00:00
1111
[{2019-02-27 08:00:12 ,2019-02-27 23:59:59}, {2019-02-28 00:00:00 ,2019-02-28 23:59:59}, {2019-03-01 00:00:00 ,2019-03-01 23:59:59}, {2019-03-02 00:00:00 ,2019-03-02 23:59:59}, {2019-03-03 00:00:00 ,2019-03-03 12:01:08}]

//按小时
    public static void main(String[] args) throws ParseException {
        Date begin = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-02-27 08:00:12");
        Date end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-03-03 12:01:08");
        List<DateRange>dateRanges = splitDateRangeByHour (start, end);
        System.out.println (dateRanges); 
    } 

Output
 2019-02-27 09:00:12 
2019-02-27 10:00:12 
2019-02-27 11:00:12 
2019-02-27 12:00: 12 
2019-02-27 13:00:12 
2019-02-27 14:00:12 
2019-02-27 
15:00:12 2019-02-27 
16:00:12 2019-02-27 17:00: 12 
2019-02-27 18:00:12 
2019-02-27 19:00:12 
2019-02-27 20:00:12 
2019-02-27 21:00:12 
2019-02-27 22:00: 12 
2019-02-27 23:00:12 
2019-02-28 01:00:00 
2019-02-28 02:00:00 
2019-02-28 03:00:00 
2019-02-28 04:00: 00 
2019-02-28 05:00:00 
2019-02-28 06:00:00 
2019-02-28 07:00:00 
2019-02-28 08:00:00 
2019-02-28 09:00:00 
2019-02-28 10:00 : 00
2019-02-28 11:00:00
2019-02-28 12:00:00
2019-02-28 13:00:00
2019-02-28 14:00:00
2019-02-28 15:00:00
2019-02-28 16:00:00
2019-02-28 17:00:00
2019-02-28 18:00:00
2019-02-28 19:00:00
2019-02-28 20:00:00
2019-02-28 21:00:00
2019-02-28 22:00:00
2019-02-28 23:00:00
2019-03-01 01:00:00
2019-03-01 02:00:00
2019-03-01 03:00:00
2019-03-01 04:00:00
2019-03-01 05:00:00
2019-03-01 06:00:00
2019-03-01 07:00:00
2019-03-01 08:00:00
2019-03-01 09:00:00
2019-03-01 10:00:00
2019-03-01 11:00:00
2019-03-01 12:00:00
2019-03-01 13:00:00
2019-03-01 14:00:00
2019-03-01 15:00:00
2019-03-01 16:00:00
2019-03-01 17:00:00
2019-03-01 18:00:00
2019-03-01 19:00:00
2019-03-01 20:00:00
2019-03-01 21:00:00
2019-03-01 22:00:00
2019-03-01 23:00:00
2019-03-02 01:00:00
2019-03-02 02:00:00
2019-03-02 03:00:00
2019-03-02 04:00:00
2019-03-02 05:00:00
2019-03-02 06:00:00
2019-03-02 07:00:00
2019-03-02 08:00:00
2019-03-02 09:00:00
2019-03-02 10:00:00
2019-03-02 11:00:00
2019-03-02 12:00:00
2019-03-02 13:00:00
2019-03-02 14:00:00
2019-03-02 15:00:00
2019-03-02 16:00:00
2019-03-02 17:00:00
2019-03-02 18:00:00
2019-03-02 19:00:00
2019-03-02 20:00:00
2019-03-02 21:00:00
2019-03-02 22:00:00
2019-03-02 23:00:00
2019-03-03 01:00:00
2019-03-03 02:00:00
2019-03-03 03:00:00
2019-03-03 04:00:00
2019-03-03 05:00:00
2019-03-03 06:00:00
2019-03-03 07:00:00
2019-03-03 08:00:00
2019-03-03 09:00:00
2019-03-03 10:00:00
2019-03-03 11:00:00
2019-03-03 12:00:00
2019-03-03 13:00:00
1111
[{2019-02-27 08:00:12 ,2019-02-27 23:59:59}, {2019-02-28 00:00:00 ,2019-02-28 23:59:59}, {2019-03-01 00:00:00 ,2019-03-01 23:59:59}, {2019-03-02 00:00:00 ,2019-03-02 23:59:59}, {2019-03-03 00:00:00 ,2019-03-03 12:01:08}]

 

Related links: https://www.jianshu.com/p/e83e58c3e3a9

Guess you like

Origin www.cnblogs.com/Steven5007/p/12723683.html