简体   繁体   English

scala 中带有日期到字符串转换的字符串插值

[英]String interpolation with date to string conversion in scala

I'm trying to figure out how to make string interpolation with date cast to string.我试图弄清楚如何使用日期转换为字符串进行字符串插值。 Here is the sample, in python I do这是示例,在 python 我做

def get_the_last_n_days(days):
    return f"'{str(datetime.now().date() - timedelta(days))}'"

Basically, it will return the current date subtract by the number of given days.基本上,它将返回当前日期减去给定天数。 For instance, considering the date 2021-04-08 :例如,考虑日期2021-04-08

  • If the given number is 1, the output will be the string 2021-04-08 ;如果给定数字为 1,则 output 将是字符串2021-04-08
  • If the given number is 8, the output will be the string 2021-03-31 ;如果给定的数字是 8,则 output 将是字符串2021-03-31

Again, in python I would something like:同样,在 python 我会这样:

print(f"I'm married since {get_the_last_n_days(0)}")

To interpolate the string, the statement above would print something like I'm married since 2021-04-08为了插入字符串,上面的语句将打印出类似I'm married since 2021-04-08

I've tried some approach but without success.我尝试了一些方法,但没有成功。

Something like this?像这样的东西?

import java.time.LocalDate

def daysAgo(n: Long) : LocalDate =
  LocalDate.now().minusDays(n)

s"I'm vaccinated since ${daysAgo(9)}."
//res0: String = I'm vaccinated since 2021-03-29.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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