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


156

我有一个名为date的LocalDate变量,在打印时显示1988-05-05,我需要将其转换为1988年5月5日打印。如何执行此操作?


3
看一下DateTimeFormatter类。它甚至包含如何与Strings 相互转换的示例。
azurefrog

2
您应该显示一些代码,到目前为止已经尝试了什么,哪些没有用,那么人们可以为您提供帮助。
2015年

我尝试这样做,但是由于这种令人讨厌的“不符合我们的质量标准”的事情,我最终放弃了,我花了15分钟才发布了此消息,因为我必须用“ I”更正“ i”。
Jasko 2015年

Answers:


306

如果他从Java 8中的新功能LocalDate开始,SimpleDateFormat将无法工作。从我所看到的,您将必须使用DateTimeFormatter,http://docs.oracle.com/javase/8/docs/api/java/ time / format / DateTimeFormatter.html

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

该日期应为1988年5月5日。要获取第二天之后和该月之前的时间,您可能必须使用“ dd'.LLLL y​​yyy”


4
非常感谢您,这很有帮助,但是使用'LLLL'不能显示月份名称,但是使用'MMMM'时我发现这很奇怪,因为即使在文档中也是如此,但是再次感谢您帮助我。
Jasko 2015年

2
现在localDate.format(formatter); 根本不起作用
EngineSense

您使用@ProgrammersBlock使用哪个版本的JDK
Lova Chittumuri,

28

可能简短为:

LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));

3
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MMMM yyyy")));

上面的答案显示了今天


ofPatterns需要android O
Mahdi

0

在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));

}

希望这会对某人有所帮助,如果有人发现更好的方法来完成此任务,请添加您的输入。


我看到一个更好的办法:只要LocalDate.parse(rawDate)String.format在这里以完全错误的形式使用,实际上只是返回第一个字符串;该DateTimeFormatter不被在所有使用的; parse是一种static方法LocalDate; formattedDate不是格式化的日期,甚至没有String(如问题的要求)
user85421

0

有一种内置的方式来格式化 joda lib的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
不是根据javadoc。
爱德华

@Edward,如果不是,请进行证明并提供您认为更好的解决方案。
伊诺伊

1
您要我证明LocalDate.toString(String pattern)不存在吗?请提供对该方法的引用(如果确实存在)-我无法在任何javadocs中找到它。至于解决方案,这个问题已经有了。
爱德华

@Edward,看来您很难找到它。更新了我的答案。
伊诺伊

Joda Time不是“内置”的。OP似乎正在使用java.time.LocalDate,而不是joda.time
Edward

-11

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

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

我看到您在变量中有日期:

sdf.format(variable_name);

干杯。


3
OP希望以LocalDate特定格式打印实例的内容。怎么能SimpleDateFormat在这里帮助他?
汤姆(Tom)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.