如何将 LocalDate 格式化为字符串?

How to format LocalDate to string?

提问人:Jasko 提问时间:1/28/2015 最后编辑:AlexElinJasko 更新时间:10/30/2021 访问量:502873

问:

我有一个名为 的变量,当我打印它时显示 1988-05-05 我需要将其转换为打印为 05.May 1988。如何做到这一点?LocalDatedate

Java JSR310

评论

3赞 azurefrog 1/28/2015
看一下 DateTimeFormatter 类。它甚至有如何转换为 s 和从 s 转换的示例。String
2赞 El Bert 1/28/2015
你应该展示一些代码,到目前为止你尝试过什么,哪些没有用,这样人们才能帮助你。
0赞 Jasko 1/28/2015
我试图这样做,但由于这个烦人的“它不符合我们的质量标准”的事情,我最终放弃了,我花了 15 分钟才发布这个,因为我不得不用“我”更正“i”。

答:

-18赞 nom 1/28/2015 #1

一个很好的方法是使用我将向您展示如何:SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("d MMMM YYYY");
Date d = new Date();
sdf.format(d);

我看到你有一个变量中的日期:

sdf.format(variable_name);

干杯。

评论

5赞 Tom 1/28/2015
OP 希望以特定格式打印实例的内容。在这里怎么能帮助他?LocalDateSimpleDateFormat
459赞 ProgrammersBlock 1/28/2015 #2

如果 SimpleDateFormat 从 Java 8 中的新 LocalDate 开始,他将不起作用。据我所知,http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html,您将不得不使用 DateTimeFormatter。

LocalDate localDate = LocalDate.now();//For reference
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy");
String formattedString = localDate.format(formatter);

这应该是1988年5月5日印刷的。要获取日期之后和月份之前的句点,您可能需要使用“dd”。LLLL yyyy”

评论

6赞 Jasko 1/28/2015
非常感谢,这很有帮助,但它不适用于“LLLL”来显示月份的名称,但使用“MMMM”我发现这很奇怪,因为即使在文档中它也另有说明。但再次感谢您对我的帮助。
3赞 EngineSense 12/10/2016
现在是 localDate.format(格式化程序);根本不起作用
0赞 Lova Chittumuri 1/31/2018
哪个版本的 JDK 您的 Using @ProgrammersBlock
0赞 pete 2/26/2018 #3

在 ProgrammersBlock 帖子的帮助下,我想出了这个。我的需求略有不同。我需要获取一个字符串并将其作为 LocalDate 对象返回。我收到了使用旧 Calendar 和 SimpleDateFormat 的代码。我想让它更现代一点。这就是我想出的。

    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;


    void ExampleFormatDate() {

    LocalDate formattedDate = null;  //Declare LocalDate variable to receive the formatted date.
    DateTimeFormatter dateTimeFormatter;  //Declare date formatter
    String rawDate = "2000-01-01";  //Test string that holds a date to format and parse.

    dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;

    //formattedDate.parse(String string) wraps the String.format(String string, DateTimeFormatter format) method.
    //First, the rawDate string is formatted according to DateTimeFormatter.  Second, that formatted string is parsed into
    //the LocalDate formattedDate object.
    formattedDate = formattedDate.parse(String.format(rawDate, dateTimeFormatter));

}

希望这对某人有所帮助,如果有人看到完成此任务的更好方法,请添加您的意见。

评论

0赞 user85421 12/21/2019
我看到了一个更好的方法:只是! 在这里以完全错误的形式使用,实际上只是返回第一个字符串;根本没有被使用; 是 ; 不是格式化的日期,甚至不是(根据相关要求)LocalDate.parse(rawDate)String.formatDateTimeFormatterparsestaticLocalDateformattedDateString
8赞 karthik r 8/9/2018 #4
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MMMM yyyy")));

上面的答案表明了今天的情况

评论

0赞 Mahdi 3/9/2020
ofPatterns 需要 android O
105赞 Zon 9/23/2018 #5

可以简称为:

LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
-6赞 Inoy 7/13/2019 #6

在 Joda 库,有一种内置的方法来格式化 LocalDate

import org.joda.time.LocalDate;

LocalDate localDate = LocalDate.now();
String dateFormat = "MM/dd/yyyy";
localDate.toString(dateFormat);

如果您还没有它 - 将其添加到 build.gradle:

implementation 'joda-time:joda-time:2.9.5'

祝您编码愉快!:)

评论

1赞 Edward 9/19/2019
不是根据 javadoc。
0赞 Inoy 12/20/2019
@Edward,如果不是,请证明并提供您认为更好的解决方案。
1赞 Edward 12/21/2019
你想让我证明 LocalDate.toString(String pattern) 不存在吗?如果它确实存在,请提供对该方法的引用 - 我无法在任何 javadocs 中找到它。至于解决方案,这个问题已经有了。
0赞 Inoy 12/21/2019
@Edward,看起来你很难找到它。更新了我的答案。
1赞 Edward 12/21/2019
Joda Time 不是“内置的”。OP 似乎使用的是 java.time.LocalDate,而不是 joda.time
14赞 Arvind Kumar Avinash 5/12/2021 #7

java.time

不幸的是,所有现有的答案都遗漏了一个关键的东西,即 Locale

日期时间解析/格式化类型(例如 新式 API 或旧版 API)是 -sensitive。其图案中使用的符号根据与它们一起使用的符号打印文本。如果没有 ,则使用 JVM 的缺省值。查看此答案以了解更多信息。DateTimeFormatterSimpleDateFormatLocaleLocaleLocaleLocale

预期输出中的文本是英文的,因此,现有的解决方案只会因为巧合而产生预期的结果(当 JVM 的默认值为英文时)。05.May 1988LocaleLocale

使用 java.time现代日期时间 API)的解决方案*:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(1988, 5, 5);
        final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd.MMMM uuuu", Locale.ENGLISH);
        String output = dtf.format(date);
        System.out.println(output);
    }
}

输出:

05.May 1988

在这里,您可以使用代替,但我更喜欢.yyyyuuuuuy

Trail: Date Time 了解有关新式日期时间 API 的更多信息。


* 无论出于何种原因,如果你必须坚持使用 Java 6 或 Java 7,你可以使用 ThreeTen-Backport,它将大部分 java.time 功能向后移植到 Java 6 和 7。如果您正在为 Android 项目工作,并且您的 Android API 级别仍然不符合 Java-8,请查看通过脱糖提供的 Java 8+ API如何在 Android 项目中使用 ThreeTenABP