gpt4 book ai didi

Java LocalDateTime日期时间格式化

转载 作者:知者 更新时间:2024-03-12 08:01:20 25 4
gpt4 key购买 nike

Java LocalDateTime 使用 LocalDateTimeDateTimeFormatterformat() 方法格式化为字符串。 LocalDateTime 表示没有时区的日期时间,例如 2019-11-15T13:15:30。 LocalDateTime 不包含时区。它用于表示日期,例如生日。要格式化 LocalDateTime,我们需要使用 DateTimeFormatter 使用 format() 方法,如下所示。
LocalDateTime.format():使用指定的格式化程序格式化此日期时间。输出将是字符串。

LocalDateTime localDateTime = LocalDateTime.parse("2019-11-15T13:15:30");
String dateTime = localDateTime.format(DateTimeFormatter.ofPattern("MMM dd, yyyy HH:mm:ss"));
System.out.println(dateTime); //Nov 15, 2019 13:15:30

DateTimeFormatter.format():使用此格式化程序格式化日期时间对象。输出将是字符串。

LocalDateTime localDateTime = LocalDateTime.parse("2019-11-15T13:15:30");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM dd, yyyy HH:mm:ss");
String dateTime = dtf.format(localDateTime);
System.out.println(dateTime); //Nov 15, 2019 13:15:30

使用 LocalDateTime.format() 格式化 LocalDateTime

在这里,我们将使用 format() 方法格式化 LocalDateTime。我们将使用 parse 方法实例化 LocalDateTime
LDTFormatDemo.java

package com.concretepage;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LDTFormatDemo {
  public static void main(String[] args) {
	LocalDateTime localDateTime = LocalDateTime.parse("2019-11-15T13:15:30");
	
	String dateTime = localDateTime.format(DateTimeFormatter.ofPattern("MMM dd, yyyy HH:mm:ss"));
	System.out.println(dateTime); //Nov 15, 2019 13:15:30
	
	dateTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy.MM.dd hh-mm-ss a"));
	System.out.println(dateTime); //2019.11.15 01-15-30 PM
	
	dateTime = localDateTime.format(DateTimeFormatter.ofPattern("EEE, MMM d, ''yy"));
	System.out.println(dateTime); //Fri, Nov 15, '19
	
	dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MMM-dd(E) HH:mm:ss:SSS"));
	System.out.println(dateTime);	
	
	dateTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyyy.MMMMM.dd HH:mm:ss GGG"));
	System.out.println(dateTime); //02019.N.15 13:15:30 AD
	
	dateTime = localDateTime.format(DateTimeFormatter.ofPattern("EEE, d MMM yyyy"));
	System.out.println(dateTime); //Fri, 15 Nov 2019
	
	dateTime = localDateTime.format(DateTimeFormatter.ofPattern("d, MMM hh:mm a"));
	System.out.println(dateTime); //15, Nov 01:15 PM
  }
}

输出

Nov 15, 2019 13:15:30
2019.11.15 01-15-30 PM
Fri, Nov 15, '19
2019-Aug-23(Fri) 21:14:44:662
02019.N.15 13:15:30 AD
Fri, 15 Nov 2019
15, Nov 01:15 PM

使用 DateTimeFormatter.format() 格式化 LocalDateTime

在这里,我们将使用 DateTimeFormatterformat() 方法格式化 LocalDateTime
DTFFormatDemo.java

package com.concretepage;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DTFFormatDemo {
  public static void main(String[] args) {
	LocalDateTime localDateTime = LocalDateTime.parse("2019-11-15T13:15:30");
	
	DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM dd, yyyy HH:mm:ss");
	String dateTime = dtf.format(localDateTime);
	System.out.println(dateTime); //Nov 15, 2019 13:15:30
	
	dtf = DateTimeFormatter.ofPattern("yyyy.MM.dd hh-mm-ss a");
	dateTime = dtf.format(localDateTime);
	System.out.println(dateTime); //2019.11.15 01-15-30 PM
	
	dtf = DateTimeFormatter.ofPattern("EEE, MMM d, ''yy");
	dateTime = dtf.format(localDateTime);
	System.out.println(dateTime); //Fri, Nov 15, '19
	
	dtf = DateTimeFormatter.ofPattern("yyyy-MMM-dd(E) HH:mm:ss:SSS");
	dateTime = dtf.format(LocalDateTime.now());
	System.out.println(dateTime); 		
	
	dtf = DateTimeFormatter.ofPattern("yyyyy.MMMMM.dd HH:mm:ss GGG");
	dateTime = dtf.format(localDateTime);
	System.out.println(dateTime); //02019.N.15 13:15:30 AD
	
	dtf = DateTimeFormatter.ofPattern("EEE, d MMM yyyy");
	dateTime = dtf.format(localDateTime);
	System.out.println(dateTime); //Fri, 15 Nov 2019
	
	dtf = DateTimeFormatter.ofPattern("d, MMM hh:mm a");
	dateTime = dtf.format(localDateTime);
	System.out.println(dateTime); //15, Nov 01:15 PM
  }
}

输出

Nov 15, 2019 13:15:30
2019.11.15 01-15-30 PM
Fri, Nov 15, '19
2019-Aug-23(Fri) 21:16:27:760
02019.N.15 13:15:30 AD
Fri, 15 Nov 2019
15, Nov 01:15 PM

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com