Java 如何将 LocalDate 格式化为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28177370/
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 to string?
提问by Jasko
I have a LocalDate variable called date,when I print it displays 1988-05-05 I need to convert this to be printed as 05.May 1988.How to do this?
我有一个名为 date 的 LocalDate 变量,当我打印它时显示 1988-05-05 我需要将其转换为打印为 05.May 1988.How to do this?
采纳答案by ProgrammersBlock
SimpleDateFormat will not work if he is starting with LocalDate which is new in Java 8. From what I can see, you will have to use DateTimeFormatter, http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html.
如果他从 Java 8 中新增的 LocalDate 开始,SimpleDateFormat 将不起作用。据我所知,您将不得不使用 DateTimeFormatter,http://docs.oracle.com/javase/8/docs/api/java/时间/格式/DateTimeFormatter.html。
LocalDate localDate = LocalDate.now();//For reference
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy");
String formattedString = localDate.format(formatter);
That should print 05 May 1988. To get the period after the day and before the month, you might have to use "dd'.LLLL yyyy"
那应该打印 05 May 1988。要获得一天之后和一个月之前的时间段,您可能必须使用“dd'.LLLL yyyy”
回答by nom
A pretty nice way to do this is to use SimpleDateFormat
I'll show you how:
一个很好的方法是使用SimpleDateFormat
我将向您展示如何:
SimpleDateFormat sdf = new SimpleDateFormat("d MMMM YYYY");
Date d = new Date();
sdf.format(d);
I see that you have the date in a variable:
我看到你在一个变量中有日期:
sdf.format(variable_name);
Cheers.
干杯。
回答by pete
With the help of ProgrammersBlock posts I came up with this. My needs were slightly different. I needed to take a string and return it as a LocalDate object. I was handed code that was using the older Calendar and SimpleDateFormat. I wanted to make it a little more current. This is what I came up with.
在 ProgrammersBlock 帖子的帮助下,我想出了这个。我的需求略有不同。我需要获取一个字符串并将其作为 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));
}
Hopefully this will help someone, if anyone sees a better way of doing this task please add your input.
希望这会对某人有所帮助,如果有人看到执行此任务的更好方法,请添加您的意见。
回答by karthik r
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MMMM yyyy")));
The above answer shows it for today
上面的答案显示了今天
回答by Zon
Could be short as:
可以简写为:
LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
回答by Inoy
There is a built-in way to format LocalDatefor joda lib
有一种内置方法可以为 joda lib格式化 LocalDate
import org.joda.time.LocalDate;
LocalDate localDate = LocalDate.now();
String dateFormat = "MM/dd/yyyy";
localDate.toString(dateFormat);
In case you don't have it already - add this to the build.gradle:
如果您还没有 - 将其添加到 build.gradle:
implementation 'joda-time:joda-time:2.9.5'
Happy coding! :)
快乐编码!:)