Run2 scala java time handling
Working with Java's LocalDate and LocalDateTime in Scala for temporal tokenizationFrom its SKILL.md
npx -y skills add cxcscmu/SkillLearnBench --skill run2_scala-java-time-handlingAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
SKILL.md
1.5 KB, 321 tokens by cl100k_base, as published. Nobody here has run it
Java Time API in Scala
Import Statements
import java.time.{LocalDate, LocalDateTime}
import java.time.format.DateTimeFormatter
Creating Formatters
// Predefined patterns
private val isoFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
private val dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
// Using formatter
val formatted = isoFormatter.format(localDateTime)
Handling Optional Format Strings
// Option for custom format
formatStr match {
case Some(pattern) => DateTimeFormatter.ofPattern(pattern).format(value)
case None => defaultFormatter.format(value)
}
// Or with getOrElse
val formatter = formatStr
.map(DateTimeFormatter.ofPattern)
.getOrElse(isoFormatter)
formatter.format(value)
Type Matching for Date/Time
value match {
case dt: LocalDateTime => isoFormatter.format(dt)
case d: LocalDate => dateFormatter.format(d)
case _ => value.toString
}
ISO 8601 Standards
- DateTime:
yyyy-MM-dd'T'HH:mm:ss - Date:
yyyy-MM-dd - Time:
HH:mm:ss
Common Patterns
// Store formatter as val for reuse (thread-safe)
private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
// Format with fallback
def formatDate(date: LocalDate): String = {
try {
formatter.format(date)
} catch {
case _: Exception => date.toString
}
}
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.