Home  >  Java 8

Java 8 DateTimeFormatter and DateTimeFormatterBuilder Example

By Arvind Rai, October 17, 2014
Java 8 has provided DateTimeFormatter and DateTimeFormatterBuilder to play with formatting date, time or both in different ways. DateTimeFormatter has in-built formats that can directly be used to parse a character sequence. DateTimeFormatterBuilder provides custom way to create a formatter.

DateTimeFormatter Example

java.time.format.DateTimeFormatter provides formatting pattern to parse a character sequence. There are many constant fields in DateTimeFormatter that can be used to format a date or time or combination of both. In the example we are using some constant of DateTimeFormatter.
DateTimeFormatterDemo.java
package com.cp.time.format;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterDemo {
	public static void main(String[] args) {
		LocalTime time = LocalTime.parse("10:15:30", DateTimeFormatter.ISO_TIME);
		System.out.println(time);
		LocalDate date = LocalDate.parse("20131206", DateTimeFormatter.BASIC_ISO_DATE);
		System.out.println(date);
		LocalDateTime dateTime1 = LocalDateTime.parse("Thu, 5 Jun 2014 05:10:40 GMT", DateTimeFormatter.RFC_1123_DATE_TIME);
		System.out.println(dateTime1);
		LocalDateTime dateTime2 = LocalDateTime.parse("2014-11-03T11:15:30", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
		System.out.println(dateTime2);
		LocalDateTime dateTime3 = LocalDateTime.parse("2014-10-05T08:15:30+02:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
		System.out.println(dateTime3);
	}
} 
Output
Find the output.
10:15:30
2013-12-06
2014-06-05T05:10:40
2014-11-03T11:15:30
2014-10-05T08:15:30 



DateTimeFormatterBuilder Example

If we want to create our own DateTimeFormatter object, then java.time.format.DateTimeFormatterBuilder will help. All the formatter has been created using DateTimeFormatterBuilder. There are different methods like appendValue, appendLiteral and appendText etc that is used to generate a formatter.
DateTimeFormatterBuilderDemo.java
package com.cp.time.format;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.TextStyle;
import java.time.temporal.ChronoField;
public class DateTimeFormatterBuilderDemo {
	public static void main(String[] args) {
		DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
		DateTimeFormatter formatter = builder.appendLiteral("Day is:")
		    .appendValue(ChronoField.DAY_OF_MONTH)
		    .appendLiteral(", month is:")
		    .appendValue(ChronoField.MONTH_OF_YEAR)
		    .appendLiteral(", and year:")
		    .appendPattern("u")
		    .appendLiteral(" with the time:")
		    .appendValue(ChronoField.HOUR_OF_DAY)
		    .appendLiteral(":")
		    .appendText(ChronoField.MINUTE_OF_HOUR, TextStyle.NARROW_STANDALONE)
		    .toFormatter(); 
		    LocalDateTime dateTime  = LocalDateTime.now(); 
		    String str =  dateTime.format(formatter); 
		    System.out.println(str);
	}
} 
Output
Find the output.
Day is:17, month is:10, and year:2014 with the time:23:35 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us