You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala函数报错:plusDays与minusDays参数类型不匹配问题排查

Fixing Type Mismatch Error with LocalDate in Scala

First, let's break down the core issue from your error message:

:143: error: type mismatch; found : java.time.LocalDate required: String
toEnd(rd1.toLocalTime) + jourOuvree(rd1.toLocalDate.plusDays(1), rd2.toLocalDate.minusDays(1)) * 8.hours + toStart(rd2.toLocalTime)
^

This error makes it clear: your jourOuvree function is defined to accept String arguments, but you're passing it java.time.LocalDate instances (from the plusDays(1) and minusDays(1) calls). Here are two straightforward fixes to resolve this:

Option 1: Update jourOuvree to accept LocalDate directly

The cleanest solution is to adjust the function's parameter types to match what you're passing. If jourOuvree is meant to work with dates, change its definition from expecting Strings to LocalDate:

// Before (accepts Strings)
def jourOuvree(start: String, end: String): Int = {
  // Existing logic that parses strings to dates
}

// After (accepts LocalDate)
import java.time.LocalDate

def jourOuvree(start: LocalDate, end: LocalDate): Int = {
  // Update logic to use LocalDate methods directly (no parsing needed)
}

This way, you can pass the results of plusDays and minusDays directly without any conversion steps.

Option 2: Convert LocalDate to String (if you can't modify jourOuvree)

If you need to keep jourOuvree accepting Strings, convert your LocalDate objects to a format the function understands. Use toString() for the default ISO format (yyyy-MM-dd), or a DateTimeFormatter for a custom format:

Using default ISO format:

toEnd(rd1.toLocalTime) + jourOuvree(
  rd1.toLocalDate.plusDays(1).toString(), 
  rd2.toLocalDate.minusDays(1).toString()
) * 8.hours + toStart(rd2.toLocalTime)

Using a custom format (e.g., dd/MM/yyyy):

import java.time.format.DateTimeFormatter

val dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")

toEnd(rd1.toLocalTime) + jourOuvree(
  rd1.toLocalDate.plusDays(1).format(dateFormatter), 
  rd2.toLocalDate.minusDays(1).format(dateFormatter)
) * 8.hours + toStart(rd2.toLocalTime)

Quick Import Check

Ensure you have the necessary imports for date handling:

import java.time.LocalDate
import java.time.format.DateTimeFormatter // Only needed if using custom formatting

内容的提问来源于stack exchange,提问作者vero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:40:06