【Java 8 新特性】Java LocalDate 格式转化

102 篇文章 72 订阅

我们可以使用LocalDateDateTimeFormatterformat()方法将LocalDate格式化为字符串。

Java 8中引入的LocalDate表示yyyy-MM-dd格式的日期,如2019-05-08

它不存储时间或时区。

我们可以使用DateTimeFormatterLocalDate格式化为所需的格式。

要格式化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实例传递给LocalDateformat()方法。

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: 在这里,我们将使用DateTimeFormatterofLocalizedDate方法来实例化DateTimeFormatter,然后我们将这个formatter实例传递给LocalDateformat()方法。

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实例传递给LocalDateformat()方法。

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: 在这里,我们将使用DateTimeFormatterofPattern方法实例化DateTimeFormatter,然后我们将把LocalDate实例传递给DateTimeFormatterformat()方法。

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: 在这里,我们将使用DateTimeFormatterofLocalizedDate方法来实例化DateTimeFormatter,然后我们将把LocalDate实例传递给DateTimeFormatterformat()方法。

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实例传递给DateTimeFormatterformat() 方法。

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

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫巳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值