提问人:Jasko 提问时间:1/28/2015 最后编辑:AlexElinJasko 更新时间:10/30/2021 访问量:502873
如何将 LocalDate 格式化为字符串?
How to format LocalDate to string?
答:
一个很好的方法是使用我将向您展示如何:SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("d MMMM YYYY");
Date d = new Date();
sdf.format(d);
我看到你有一个变量中的日期:
sdf.format(variable_name);
干杯。
评论
LocalDate
SimpleDateFormat
如果 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”
评论
在 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
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MMMM yyyy")));
上面的答案表明了今天的情况
评论
可以简称为:
LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
在 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'
祝您编码愉快!:)
评论
java.time
不幸的是,所有现有的答案都遗漏了一个关键的东西,即 Locale
。
日期时间解析/格式化类型(例如 新式 API 或旧版 API)是 -sensitive。其图案中使用的符号根据与它们一起使用的符号打印文本。如果没有 ,则使用 JVM 的缺省值。查看此答案以了解更多信息。DateTimeFormatter
SimpleDateFormat
Locale
Locale
Locale
Locale
预期输出中的文本是英文的,因此,现有的解决方案只会因为巧合而产生预期的结果(当 JVM 的默认值为英文时)。05.May 1988
Locale
Locale
使用 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
在这里,您可以使用代替,但我更喜欢.yyyy
uuuu
u
y
从 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。
评论
String