Java 如何将 LocalDate 对象格式化为 MM/dd/yyyy 并保持格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39689866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to format LocalDate object to MM/dd/yyyy and have format persist
提问by MOZAKATAK
I am reading text and storing the dates as LocalDate variables.
我正在阅读文本并将日期存储为 LocalDate 变量。
Is there any way for me to preserve the formatting from DateTimeFormatter so that when I call the LocalDate variable it will still be in this format.
有什么方法可以让我保留 DateTimeFormatter 的格式,这样当我调用 LocalDate 变量时它仍然是这种格式。
EDIT:I want the parsedDate to be stored in the correct format of 25/09/2016 rather than printing as a string
编辑:我希望 parsedDate 以 25/09/2016 的正确格式存储,而不是作为字符串打印
My code:
我的代码:
public static void main(String[] args)
{
LocalDate date = LocalDate.now();
DateTimeFormatter formatters = DateTimeFormatter.ofPattern("d/MM/uuuu");
String text = date.format(formatters);
LocalDate parsedDate = LocalDate.parse(text, formatters);
System.out.println("date: " + date); // date: 2016-09-25
System.out.println("Text format " + text); // Text format 25/09/2016
System.out.println("parsedDate: " + parsedDate); // parsedDate: 2016-09-25
// I want the LocalDate parsedDate to be stored as 25/09/2016
}
回答by Wojciech Kazior
Just format the date while printing it out:
只需在打印时格式化日期:
public static void main(String[] args) {
LocalDate date = LocalDate.now();
DateTimeFormatter formatters = DateTimeFormatter.ofPattern("d/MM/uuuu");
String text = date.format(formatters);
LocalDate parsedDate = LocalDate.parse(text, formatters);
System.out.println("date: " + date);
System.out.println("Text format " + text);
System.out.println("parsedDate: " + parsedDate.format(formatters));
}
回答by Rebecca Close
EDIT: Considering your edit, just set parsedDate equal to your formatted text string, like so:
编辑:考虑到您的编辑,只需将 parsedDate 设置为等于您的格式化文本字符串,如下所示:
parsedDate = text;
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
LocalDate 对象只能以 ISO8601 格式 (yyyy-MM-dd) 打印。为了以其他格式打印对象,您需要对其进行格式化并将 LocalDate 保存为字符串,就像您在自己的示例中演示的那样
DateTimeFormatter formatters = DateTimeFormatter.ofPattern("d/MM/uuuu");
String text = date.format(formatters);
回答by Joe C
Short answer: no.
简短的回答:没有。
Long answer: A LocalDate
is an object representing a year, month and day, and those are the three fields it will contain. It does not have a format, because different locales will have different formats, and it will make it more difficult to perform the operations that one would want to perform on a LocalDate
(such as adding or subtracting days or adding times).
长答案:ALocalDate
是代表年、月和日的对象,这三个字段将包含它。它没有格式,因为不同的语言环境将有不同的格式,并且会更难执行人们想要在 a 上执行的操作LocalDate
(例如添加或减去天数或添加时间)。
The String representation (produced by toString()
) is the international standard on how to print dates. If you want a different format, you should use a DateTimeFormatter
of your choosing.
字符串表示(由 生成toString()
)是关于如何打印日期的国际标准。如果您想要不同的格式,您应该使用DateTimeFormatter
您选择的格式。
回答by George Siggouroglou
This could be possible if you could extend LocalDate and override the toString()
method but the LocalDate class is immutable and therefore (secure oop) final. This means that if you wish to use this class the only toString()
method you will be able to use is the above (copied from LocalDate sources):
如果您可以扩展 LocalDate 并覆盖该toString()
方法,那么这可能是可能的,但是 LocalDate 类是不可变的,因此(安全 oop)是最终的。这意味着如果您希望使用此类,您可以使用的唯一toString()
方法是上述(从 LocalDate 来源复制):
@Override
public String toString() {
int yearValue = year;
int monthValue = month;
int dayValue = day;
int absYear = Math.abs(yearValue);
StringBuilder buf = new StringBuilder(10);
if (absYear < 1000) {
if (yearValue < 0) {
buf.append(yearValue - 10000).deleteCharAt(1);
} else {
buf.append(yearValue + 10000).deleteCharAt(0);
}
} else {
if (yearValue > 9999) {
buf.append('+');
}
buf.append(yearValue);
}
return buf.append(monthValue < 10 ? "-0" : "-")
.append(monthValue)
.append(dayValue < 10 ? "-0" : "-")
.append(dayValue)
.toString();
}
If you mustoverride this behavior, you could box LocalDate and use your custom toString
method but this could cause more problems than it solves!
如果您必须覆盖此行为,您可以将 LocalDate 装箱并使用您的自定义toString
方法,但这可能会导致比它解决的问题更多的问题!