Java LocalDate 格式转化
我们可以使用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(): 使用该格式化器格式化一个date-time
对象。输出将是字符串。
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
用 LocalDate.format() 格式化 LocalDate
例1: 在这里,我们将使用其ofPattern
方法实例化DateTimeFormatter
,然后我们将这个formatter
实例传递给LocalDate
的format()
方法。
LDFormatDemoOne.java
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
例2: 在这里,我们将使用DateTimeFormatter
的ofLocalizedDate
方法来实例化DateTimeFormatter
,然后我们将这个formatter
实例传递给LocalDate
的format()
方法。
LDFormatDemoTwo.java
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
例3: 在这里,我们将使用预定义的格式来实例化DateTimeFormatter
,然后我们将把这个formatter
实例传递给LocalDate
的format()
方法。
LDFormatDemoThree.java
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
用DateTimeFormatter.format()
格式化LocalDate
例1: 在这里,我们将使用DateTimeFormatter
的ofPattern
方法实例化DateTimeFormatter
,然后我们将把LocalDate
实例传递给DateTimeFormatter
的format()
方法。
DTFFormatDemoOne.java
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: 在这里,我们将使用DateTimeFormatter
的ofLocalizedDate
方法来实例化DateTimeFormatter
,然后我们将把LocalDate
实例传递给DateTimeFormatter
的format()
方法。
DTFFormatDemoTwo.java
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
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
参考文献
【1】Java LocalDate
【2】Java DateTimeFormatter
【3】Java LocalDate Format