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

How to format LocalDate to string?

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

一个很好的方法是使用 SimpleDateFormat 我会告诉你如何:

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

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

sdf.format(variable_name);

干杯。

如果他从 Java 中新增的 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);

那应该打印出 05 May 1988。要获得日期之后和月份之前的时间段,您可能必须使用 "dd'.LLLL yyyy"

在 ProgrammersBlock 帖子的帮助下,我想到了这个。我的需求略有不同。我需要将一个字符串 return 作为 LocalDate 对象。我收到了使用旧日历和 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));

}

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

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

以上回答是今天的答案

可以简写为:

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

Joda 库

中有一种 built-in 格式化 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'

编码愉快! :)

java.time

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

日期时间 parsing/formatting 类型(例如现代 API 的 DateTimeFormatter 或遗留 API 的 SimpleDateFormat)是 Locale-敏感的。其模式中使用的符号根据与它们一起使用的 Locale 打印文本。在没有 Locale 的情况下,它使用 JVM 的默认值 Locale。检查 this answer 以了解更多信息。

预期输出中的文本 05.May 1988 是英文的,因此,现有的解决方案将产生预期的结果只是巧合(当 JVM 的默认 Locale英语 Locale).

解决方案使用java.timemodern date-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

在这里,您可以使用 yyyy 而不是 uuuu,但是 uy

Trail: Date Time.

了解有关现代日期时间 API 的更多信息

* 无论出于何种原因,如果您必须坚持Java 6 或Java 7,您可以使用ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and