groovy LocalDateTime 转成 yyyy-MM-dd HHmmss 格式字符串
您可以使用DateTimeFormatter类来将LocalDateTime对象转换为指定格式的字符串。下面是一个将LocalDateTime转换为yyyy-MM-dd HH:mm:ss格式的示例代码:
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
LocalDateTime localDateTime = LocalDateTime.now()
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
String formattedDateTime = localDateTime.format(formatter)
println formattedDateTime
在上面的代码中,我们首先创建了一个LocalDateTime对象,然后创建了一个DateTimeFormatter对象,并使用ofPattern方法指定了要转换的日期时间格式。最后,我们使用format方法将LocalDateTime对象转换为指定格式的字符串,并将结果打印出来。
希望这可以帮助到您
原文地址: https://www.cveoy.top/t/topic/iySI 著作权归作者所有。请勿转载和采集!