我们可以使用 LocalDate
或 DateTimeFormatter
的 format()
方法将 LocalDate
格式化为字符串。 Java 8 中引入的 LocalDate
表示格式为 yyyy-MM-dd 的日期,例如 2019-05-08。它不存储时间或时区。我们可以使用 DateTimeFormatter
将 LocalDate
格式化为所需的格式。要格式化 LocalDate
,我们可以使用以下方法。
LocalDate.format():使用指定的格式化程序格式化此日期。输出将是字符串。
LocalDate localDate = LocalDate.parse("2019-05-08");
String date = localDate.format(DateTimeFormatter.ofPattern("MMM dd, yyyy"));
System.out.println(date); //May 08, 2019
DateTimeFormatter.format():使用此格式化程序格式化日期时间对象。输出将是字符串。
LocalDate localDate = LocalDate.parse("2019-05-08");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM dd, yyyy");
String date = dtf.format(localDate);
System.out.println(date); //May 08, 2019
Example-1:这里我们将使用 ofPattern
方法实例化 DateTimeFormatter
,然后我们将此格式化程序实例传递给 LocalDate
的 format()
方法。
LDFormatDemoOne.java
package com.concretepage;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LDFormatDemoOne {
public static void main(String[] args) {
LocalDate localDate = LocalDate.parse("2019-05-08");
String date = localDate.format(DateTimeFormatter.ofPattern("MMM dd, yyyy"));
System.out.println(date); //May 08, 2019
date = localDate.format(DateTimeFormatter.ofPattern("yyyy.MM.dd"));
System.out.println(date); //2019.05.08
date = localDate.format(DateTimeFormatter.ofPattern("EEE, MMM d, ''yy"));
System.out.println(date); //Wed, May 8, '19
date = localDate.format(DateTimeFormatter.ofPattern("yyyy-MMM-dd(E)"));
System.out.println(date); //2019-May-08(Wed)
date = localDate.format(DateTimeFormatter.ofPattern("yyyyy.MMMMM.dd GGG"));
System.out.println(date); //02019.M.08 AD
date = localDate.format(DateTimeFormatter.ofPattern("EEE, d MMM yyyy"));
System.out.println(date); //Wed, 8 May 2019
date = localDate.format(DateTimeFormatter.ofPattern("MMM, yyyy"));
System.out.println(date); //May, 2019
}
}
输出
May 08, 2019
2019.05.08
Wed, May 8, '19
2019-May-08(Wed)
02019.M.08 AD
Wed, 8 May 2019
May, 2019
Example-2:这里我们将使用 ofLocalizedDate
方法实例化 DateTimeFormatter
,然后我们将此格式化程序实例传递给 LocalDate
的 format()
方法。
LDFormatDemoTwo.java
package com.concretepage;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.format.ResolverStyle;
import java.util.Locale;
public class LDFormatDemoTwo {
public static void main(String[] args) {
LocalDate localDate = LocalDate.parse("2019-05-08");
String date = localDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
System.out.println(date); // Wednesday, May 8, 2019
date = localDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).localizedBy(Locale.UK));
System.out.println(date); // Wednesday, 8 May 2019
date = localDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withResolverStyle(ResolverStyle.SMART));
System.out.println(date); // Wednesday, May 8, 2019
date = localDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
System.out.println(date); // May 8, 2019
date = localDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
System.out.println(date); // May 8, 2019
date = localDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT));
System.out.println(date); // 5/8/19
}
}
输出
Wednesday, May 8, 2019
Wednesday, 8 May 2019
Wednesday, May 8, 2019
May 8, 2019
May 8, 2019
5/8/19
Example-3:这里我们将使用其预定义格式实例化 DateTimeFormatter
,然后我们将此格式化程序实例传递给 LocalDate
的 format()
方法。
LDFormatDemoThree.java
package com.concretepage;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LDFormatDemoThree {
public static void main(String[] args) {
LocalDate localDate = LocalDate.parse("2019-05-08");
String date = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(date); // 20190508
date = localDate.format(DateTimeFormatter.ISO_DATE);
System.out.println(date); // 2019-05-08
date = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(date); // 2019-05-08
date = localDate.format(DateTimeFormatter.ISO_ORDINAL_DATE);
System.out.println(date); // 2019-128
date = localDate.format(DateTimeFormatter.ISO_WEEK_DATE);
System.out.println(date); // 2019-W19-3
}
}
输出
20190508
2019-05-08
2019-05-08
2019-128
2019-W19-3
示例 1:这里我们将使用 ofPattern
方法实例化 DateTimeFormatter
,然后将 LocalDate
实例传递给 DateTimeFormatter
的 format()
方法。
DTFFormatDemoOne.java
package com.concretepage;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DTFFormatDemoOne {
public static void main(String[] args) {
LocalDate localDate = LocalDate.parse("2019-05-08");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM dd, yyyy");
String date = dtf.format(localDate);
System.out.println(date); //May 08, 2019
dtf = DateTimeFormatter.ofPattern("yyyy.MM.dd");
date = dtf.format(localDate);
System.out.println(date); //2019.05.08
dtf = DateTimeFormatter.ofPattern("EEE, MMM d, ''yy");
date = dtf.format(localDate);
System.out.println(date); //Wed, May 8, '19
dtf = DateTimeFormatter.ofPattern("yyyy-MMM-dd(E)");
date = dtf.format(localDate);
System.out.println(date); //2019-May-08(Wed)
dtf = DateTimeFormatter.ofPattern("yyyyy.MMMMM.dd GGG");
date = dtf.format(localDate);
System.out.println(date); //02019.M.08 AD
dtf = DateTimeFormatter.ofPattern("EEE, d MMM yyyy");
date = dtf.format(localDate);
System.out.println(date); //Wed, 8 May 2019
dtf = DateTimeFormatter.ofPattern("MMM, yyyy");
date = dtf.format(localDate);
System.out.println(date); //May, 2019
}
}
输出
May 08, 2019
2019.05.08
Wed, May 8, '19
2019-May-08(Wed)
02019.M.08 AD
Wed, 8 May 2019
May, 2019
示例 2:这里我们将使用 ofLocalizedDate
方法实例化 DateTimeFormatter
,然后将 LocalDate
实例传递给 DateTimeFormatter
的 format()
方法。
DTFFormatDemoTwo.java
package com.concretepage;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.format.ResolverStyle;
import java.util.Locale;
public class DTFFormatDemoTwo {
public static void main(String[] args) {
LocalDate localDate = LocalDate.parse("2019-05-08");
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
String date = dtf.format(localDate);
System.out.println(date); // Wednesday, May 8, 2019
dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).localizedBy(Locale.UK);
date = dtf.format(localDate);
System.out.println(date); // Wednesday, 8 May 2019
dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withResolverStyle(ResolverStyle.SMART);
date = dtf.format(localDate);
System.out.println(date); // Wednesday, May 8, 2019
dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
date = dtf.format(localDate);
System.out.println(date); // May 8, 2019
dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
date = dtf.format(localDate);
System.out.println(date); // May 8, 2019
dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
date = dtf.format(localDate);
System.out.println(date); // 5/8/19
}
}
输出
Wednesday, May 8, 2019
Wednesday, 8 May 2019
Wednesday, May 8, 2019
May 8, 2019
May 8, 2019
5/8/19
示例 3:这里我们将使用其预定义格式实例化 DateTimeFormatter
,然后将 LocalDate
实例传递给 DateTimeFormatter
的 format()
方法。
DTFFormatDemoThree.java
package com.concretepage;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DTFFormatDemoThree {
public static void main(String[] args) {
LocalDate localDate = LocalDate.parse("2019-05-08");
String date = DateTimeFormatter.BASIC_ISO_DATE.format(localDate);
System.out.println(date); // 20190508
date = DateTimeFormatter.ISO_DATE.format(localDate);
System.out.println(date); // 2019-05-08
date = DateTimeFormatter.ISO_LOCAL_DATE.format(localDate);
System.out.println(date); // 2019-05-08
date = DateTimeFormatter.ISO_ORDINAL_DATE.format(localDate);
System.out.println(date); // 2019-128
date = DateTimeFormatter.ISO_WEEK_DATE.format(localDate);
System.out.println(date); // 2019-W19-3
}
}
输出
20190508
2019-05-08
2019-05-08
2019-128
2019-W19-3
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.concretepage.com/java/java-8/java-localdate-format
内容来源于网络,如有侵权,请联系作者删除!