且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将 LocalDate 对象格式化为 MM/dd/yyyy 并保持格式不变

更新时间:2022-12-15 12:50:29

考虑到您的编辑,只需将 parsedDate 设置为等于您的格式化文本字符串,如下所示:

Considering your edit, just set parsedDate equal to your formatted text string, like so:

parsedDate = text;

LocalDate 对象只能以 ISO8601 格式 (yyyy-MM-dd) 打印.为了以其他格式打印对象,您需要对其进行格式化并将 LocalDate 保存为字符串,就像您在自己的示例中演示的那样


A LocalDate object can only ever be printed in ISO8601 format (yyyy-MM-dd). In order to print the object in some other format, you need to format it and save the LocalDate as a string like you've demonstrated in your own example

DateTimeFormatter formatters = DateTimeFormatter.ofPattern("d/MM/uuuu");
String text = date.format(formatters);