Scala函数报错:plusDays与minusDays参数类型不匹配问题排查
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

