$show=/label

Java 8 - DateTimeFormatter Class to convert date to string

SHARE:

A guide to how to use DateTimeFormatter to convert java 8 date's into string form in predefined and custom date patterns.

1. Overview

In this tutorial, you'll learn how to use DateTimeFormatter class to convert date to string in java 8.

As you know, DateTimeFormatter is also a final, an immutable and thread safe class.

What we'll learn here is 

What are the predefined date formats in DateTimeFormatter class?
How to use custom or user defined date patterns for date parsing to string?
How to use DateTimeFormatter class with FormatStyle enum?

How to convert LocalDateTime to String?
How to convert LocalDate to String?
How to convert LocalTime to String?
How to convert ZonedDateTime to String?

Java 8 - DateTimeFormatter Class to convert date to string



2. DateTimeFormatter Supported Predefined and Custom Formats


Below are the examples how to get the builtin and create custom formats. Use DateTimeFormatter.ofPattern(String) method to define our own date pattern and it takes the string argument to store the custom pattern.
package com.javaprogramto.java8.dates.datetimeformatter;

import java.time.format.DateTimeFormatter;

public class DateTimeFormatterExample {

	public static void main(String[] args) {

		// Predefined formats
		DateTimeFormatter predefinedFormat1 = DateTimeFormatter.BASIC_ISO_DATE;
		DateTimeFormatter predefinedFormat2 = DateTimeFormatter.ISO_DATE;
		DateTimeFormatter predefinedFormat3 = DateTimeFormatter.ISO_ZONED_DATE_TIME;

		// Custom formats
		DateTimeFormatter customFormat1 = DateTimeFormatter.ofPattern("yyyy-MMM");
		DateTimeFormatter customFormat2 = DateTimeFormatter.ofPattern("yyyy-MM-dd 'at' hh:mm a");
		DateTimeFormatter customFormat3 = DateTimeFormatter.ofPattern("yyyy-MM-dd z");

	}

}
 

3. DateTimeFormatter With FormatStyle Enum


As always builtin formats does not enough to fulfil the human readable formats. 
FormatStyle enum is defined withe 4 predefined formats FULL, LONG, MEDIUM and SHORT forms.

Step 1:
Get the right FormatStyle format.

Step 2:
Call DateTimeFormatter.ofLocalizedDate() and pass the FormatStyle to it as argument.

Step 3:
Create the LocalDateTime object using now() method and next call format() method on LocalDateTime instance by passing the formatter from the step 2.
package com.javaprogramto.java8.dates.datetimeformatter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class DateTimeFormatterFormatStyleExample {

	public static void main(String[] args) {

		FormatStyle full = FormatStyle.FULL;

		DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(full);
		
		LocalDateTime datetime = LocalDateTime.now();
		
		String fullForm = datetime.format(formatter);
		
		System.out.println("Full date form in string : "+fullForm);
	}

}
 
Output:
Full date form in string : Thursday, November 26, 2020
 

4. DateTimeFormatter - Convert LocalDateTime to String 


Next, Convert java 8 LocalDateTime to String format using DateTimeFormatter class in simple steps.

Step 1:
Create a custom date formatter using DateTimeFormatter.ofPattern() and pass the pattern to this method as "yyyy-MM-dd 'at' hh:mm a". Store the result into format variable.

Step 2:
Get the current date and time using LocalDateTime.now() and store the result in the datetime variable.

Step 3:
Finally, call datetime.format(formatter) method which returns the string which holds the date time value in our own format.
package com.javaprogramto.java8.dates.datetimeformatter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatterLocalDateTimeToStringExample {

	public static void main(String[] args) {

		// Creating a custom formatter
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd 'at' hh:mm a");

		// Getting current date time using LocalDateTime.now() method
		LocalDateTime datetime = LocalDateTime.now();

		// converting date to string
		String fullForm = datetime.format(formatter);

		// printing date string form
		System.out.println("LocalDateTime form in string : " + fullForm);
	}

}
 
Output:
LocalDateTime form in string : 2020-11-26 at 08:55 PM
 

5. DateTimeFormatter - Convert LocalDate to String 


Next, Convert java 8 LocalDate to String format using DateTimeFormatter class in simple steps.

Step 1:
Create a custom date formatter using DateTimeFormatter.ofPattern() and pass the pattern as "yyyy-MMM" to this method. Store the result into format variable.

Step 2:
Get the current date and time using LocalDate.now() and store the result in the datetime variable.

Step 3:
Finally, call datetime.format(formatter) method which returns the string which holds the date time value in our own format.
package com.javaprogramto.java8.dates.datetimeformatter;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatterLocalDateToStringExample {

	public static void main(String[] args) {

		// Creating a custom formatter
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MMM");

		// Getting current date time using LocalDate.now() method
		LocalDate datetime = LocalDate.now();

		// converting date to string
		String fullForm = datetime.format(formatter);

		// printing date string form
		System.out.println("LocalDate form in string : " + fullForm);
	}
}
 
Output:
LocalDate form in string : 2020-Nov
 

6. DateTimeFormatter - Convert LocalTime to String 


Next, Convert java 8 LocalTime to String format "hh:ss a" using DateTimeFormatter class in simple steps.

Step 1:
Create a custom date formatter using DateTimeFormatter.ofPattern() and pass the pattern as "hh:ss a" to this method. Store the result into format variable.

Step 2:
Get the current date and time using LocalTime.now() and store the result in the datetime variable.

Step 3:
Finally, call datetime.format(formatter) method which returns the string which holds the date time value in our own format.
package com.javaprogramto.java8.dates.datetimeformatter;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatterLocalTimeToStringExample {

	public static void main(String[] args) {

		// Creating a custom formatter
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:ss a");

		// Getting current date time using LocalTime.now() method
		LocalTime datetime = LocalTime.now();

		// converting date to string
		String fullForm = datetime.format(formatter);

		// printing date string form
		System.out.println("LocalTime form in string : " + fullForm);
	}
}
 
Output:
LocalTime form in string : 09:03 PM
 

7. DateTimeFormatter - Convert ZonedDateTime to String 


As steps as above but it converts ZonedDateTime to string format.

package com.javaprogramto.java8.dates.datetimeformatter;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatterZonedDateTimeToStringExample {

	public static void main(String[] args) {

		// Creating a custom formatter
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM/dd/yyyy 'at' hh:mm a z");

		// Getting current date time using ZonedDateTime.now() method
		ZonedDateTime datetime = ZonedDateTime.now();

		// converting date to string
		String fullForm = datetime.format(formatter);

		// printing date string form
		System.out.println("ZonedDateTime form in string : " + fullForm);
	}
}
 
Output:
ZonedDateTime form in string : Nov/26/2020 at 09:08 PM IST
 

8. Conclusion


In this article, We've seen how to use the DateTimeFormatter with different Java 8 DateTime API classes to convert them into String format.


Read Next


COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,16,Arrays,16,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,5,Collections,28,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,93,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,35,Dictionary,1,Difference,1,Download,1,Eclipse,2,Efficiently,1,Error,1,Errors,1,Exception,1,Exceptions,3,Fast,1,Files,13,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,4,Grant,1,Grep,1,HashMap,1,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,5,Iterate,2,Jackson API,3,Java,30,Java 10,1,Java 11,5,Java 12,5,Java 13,2,Java 14,2,Java 8,105,Java 8 Difference,2,Java 8 Stream Conversions,2,java 8 Stream Examples,3,Java 9,1,Java Conversions,11,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,104,java.lang,5,java.util. function,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,1,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,2,Math,1,Matrix,5,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,27,String,58,String Programs,12,String Revese,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,16,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java 8 - DateTimeFormatter Class to convert date to string
Java 8 - DateTimeFormatter Class to convert date to string
A guide to how to use DateTimeFormatter to convert java 8 date's into string form in predefined and custom date patterns.
https://1.bp.blogspot.com/-W9LYM3p77Ys/X7_PQOVkrJI/AAAAAAAADFY/4_ROdlgWOdYh2EW8f0zv5AD5y_JID-SzQCLcBGAsYHQ/w640-h440/Java%2B8%2B-%2BDateTimeFormatter%2BClass%2Bto%2Bconvert%2Bdate%2Bto%2Bstring.png
https://1.bp.blogspot.com/-W9LYM3p77Ys/X7_PQOVkrJI/AAAAAAAADFY/4_ROdlgWOdYh2EW8f0zv5AD5y_JID-SzQCLcBGAsYHQ/s72-w640-c-h440/Java%2B8%2B-%2BDateTimeFormatter%2BClass%2Bto%2Bconvert%2Bdate%2Bto%2Bstring.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/11/java-datetimeformatter.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/11/java-datetimeformatter.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content