有没有办法将日期部分插入包含日期格式的路径中?我有一个应用程序,应该将文件保存到年度和每月文件夹中,但我不想对其进行硬编码。
当前的命名约定是这样的:
c:\reports\yyyy\MM_L_yyyy
这应该导致:
c:\reports\2024\05_May_2024
我想将此模板放入配置文件中,并且我考虑使用正则表达式来替换每个日期格式占位符,如下所示:
c:\reports\{date:yyyy}\{date:MM_L_yyyy}
您也会这样做吗?或者对于这种情况已经有更好的解决方案了吗?
使用模板引擎很容易。我选择了PebbleTemplates。这是一个例子:
val sourceTemplate = "c:\\reports\\{{today | date(format='yyyy')}}\\{{today | date(format='MM_MMMM_yyyy')}}"
val engine = PebbleEngine.Builder().build()
val pebbleTemplate = engine.getLiteralTemplate(sourceTemplate)
val writer: Writer = StringWriter()
pebbleTemplate.evaluate(writer, mapOf("today" to LocalDate.now()))
val result = writer.toString() // <-- c:\reports\2024\05_May_2024