Home  >  Java 8

Java LocalDateTime Format

By Arvind Rai, August 23, 2019
Java LocalDateTime is formatted using format() method of LocalDateTime or DateTimeFormatter resulting into string. LocalDateTime represents date-time without a time-zone such as 2019-11-15T13:15:30. LocalDateTime does not contain time-zone. It is used to represent date such as birthdate. To format a LocalDateTime, we need to use DateTimeFormatter using format() method as following.
LocalDateTime.format(): Formats this date-time using the specified formatter. Output will be string.
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(): Formats a date-time object using this formatter. Output will be string.
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 

Format LocalDateTime with LocalDateTime.format()

Here we will format a LocalDateTime using its format() method. We will instantiate LocalDateTime using its parse method.
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
  }
} 
Output
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 

Format LocalDateTime with DateTimeFormatter.format()

Here we will format a LocalDateTime using format() method of DateTimeFormatter.
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
  }
} 
Output
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 

References

Java Doc: LocalDateTime
Java Doc: DateTimeFormatter
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us