Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Thursday, November 26, 2020

Java 8 - DateTimeFormatter Class to convert date to string

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


No comments:

Post a Comment

Please do not add any spam links in the comments section.