在MySQL中的日期范围中提取月份的日期

时间:2018-01-23 07:20:24

标签: mysql

我想计算属于两个月的日期范围内某个特定月份的日期数量,请参考下表:

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

在上面的示例中,我希望获得的日期数量属于1月份和2月份。

有没有可能通过直接使用MySQL查询或任何使用Java和MySQL组合来计算它。任何帮助将受到高度赞赏。

2 个答案:

答案 0 :(得分:0)

mysql中没有内置函数来执行此操作。如果日期是连续几个月你可以做这样的事情

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)

答案 1 :(得分:0)

java解决方案可能如下所示:

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