简体   繁体   中英

Extract dates of months in a date range in MySQL

I want to calculate number of dates belongs to a specific month within a date range belongs to two months as a example refer the following table:

|    DateFrom    |      DateTo     |
| 2018 - 01 - 28 |  2018 - 02 - 04 |

In the above example I want to get the number of dates belongs to the month of January and month of February.

Are there any possibilities to calculate this by directly using MySQL query or any using Java and MySQL combination. any help will be highly appreciated.

There isn't a built in function in mysql to do this. If the dates are in consecutive months you could do something like this

select case when month('2018-01-17') then 
            datediff
            (
            case when month('2018-01-17') in (9,4,6,11) then
             str_to_date(concat(substring(cast('2018-01-17' as char(10)),1,4),'-',substring(cast('2018-01-17' as char(10)),6,2),'-30'),'%Y-%m-%d')
            when month('2018-01-18') in (2) then
             str_to_date(concat(substring(cast('2018-01-17' as char(10)),1,4),'-',substring(cast('2018-01-17' as char(10)),6,2),'-28'),'%Y-%m-%d')
            else
             str_to_date(concat(substring(cast('2018-01-17' as char(10)),1,4),'-',substring(cast('2018-01-17' as char(10)),6,2),'-31'),'%Y-%m-%d')  
            end
          ,
            '2018-01-17')
            end  daysinstartmth,
         case when month('2018-02-04') then datediff('2018-02-04',
         str_to_date(concat(substring(cast('2018-02-04' as char(10)),1,4),'-',substring(cast('2018-02-04' as char(10)),6,2),'-01'),'%Y-%m-%d')
         ) end as endmthdays;

 +----------------+------------+
| daysinstartmth | endmthdays |
+----------------+------------+
|             14 |          3 |
+----------------+------------+
1 row in set (0.00 sec)

A java solution could look like something like below:

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class ZZ {    
    public static void main(String[] args) {
        LocalDate dateFrom = LocalDate.of(2018, 1, 28);
        LocalDate dateTo = LocalDate.of(2019, 2, 4);
        Map<String,Long> map = new LinkedHashMap<>();
        String current = dateFrom.getYear() + "_"+ dateFrom.getMonth();
        long n = countMonths(dateFrom, dateTo);
        for(int i = 0; i < n; i++){
          if(i != n-1){
              map.put(current, countDays(dateFrom,getEndOfCurrentMonth(dateFrom)));
              dateFrom = getEndOfCurrentMonth(dateFrom).plusDays(1);              
              current = dateFrom.getYear() + "_"+ dateFrom.getMonth();
          } 
          else{
              map.put(current, countDays(dateFrom,dateTo));
          }
        }
        for(Entry e : map.entrySet()){
            System.out.println(e.getKey() + ":" + e.getValue());
        }
    }  
    public static LocalDate getEndOfCurrentMonth( LocalDate d) {
        return d.with(TemporalAdjusters.lastDayOfMonth());
    }
    public static long countMonths(LocalDate from, LocalDate to) {
        return ChronoUnit.MONTHS.between(from.with(TemporalAdjusters.firstDayOfMonth()), to.with(TemporalAdjusters.lastDayOfMonth()))+1;
    }
    public static long countDays(LocalDate from, LocalDate to) {
        return ChronoUnit.DAYS.between(from, to)+1;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM