• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Java 8 Date Time API – DateTimeFormatter Example

April 14, 2015 //  by Krishna Srinivasan//  Leave a Comment

Along with lambda expression, virtual methods, streams and many other nice features, Java 8 has also updated a new Date and Time API which is maintained under the JSR-310. One of the greatest on this new API is that all the date and time related APIs are unified under the same package java.time.

In my previous articles I have explained about the LocalDate , LocalTime and few other APIs introduced with the Java 8 version.This example demonstrates how to use DateTimeFormatter class with few important methods defined in that class. Also Java 8 has introduced new package java.time.format for defining the classes related to date and time formatting.

If you have any clarifications, please write it in the comments section or post it in our facebook page. You can read more articles on Java 8 here.

DateTimeFormatter

java.time.format.DateTimeFormatter is the primary class used in Java 8 for formatting the date and time values. It has another class DateTimeFormatBuilder, which used for creating the custom formatting. I will explain that in the next example. There is no more explanation required to understand this class, please read the below example where I have written some of the key methods defined in this class for performing the formatting of date/time values.

also read:

  • Convert String to Date using SimpleDateFormat
  • File Modified Date Manipulation in Java
  • Class Name : DateTimeFormatter
  • Package : java.time.format
  • Version Released : Java 1.8

LocalDateTime – Few Methods

  • ofPattern(String) – This static method returns the DateTimeFormatter instance for the specified pattern.
  • withZone(ZoneId) – This instance method returns instance for the specified ZoneId
  • format(ZonedDateTime) – This instance method returns the formatted string
  • LocalTime.parse(String,DateTimeFormatter.ISO_TIME) – This is a static method in the LocalTime class which pasrses the string and format the string into DateTimeFormatter.ISO_TIME format
  • LocalDate.psrse(String,DateTimeFormatter.ISO_DATE) – This is a static method in the LocalDate class which pasrses the string and format the string into DateTimeFormatter.ISO_DATE format

DateTimeFormatter – Example

package javabeat.net;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Java 8 Date Time API - DateTimeFormatter Example
 *
 * @author www.javabeat.net
 *
 */
public class DateTimeFormatterExample {
	public static void main(String[] args) {
		DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter
				.ofPattern("yyyy/MM/dd HH:mm:ss z");
		DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter
				.ofPattern("yyyy/MM/dd");
		DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter
				.ofPattern("dd/MMM/YYYY");
		DateTimeFormatter formatterWithZone = dateTimeFormatter3
				.withZone(ZoneId.of("America/Los_Angeles"));
		ZoneId getZoneMethod = formatterWithZone.getZone();
		LocalTime time = LocalTime
				.parse("10:15:30", DateTimeFormatter.ISO_TIME);
		LocalDate date = LocalDate.parse("2015-04-10",
				DateTimeFormatter.ISO_DATE);
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		String formatter1 = dateTimeFormatter1.format(zonedDateTime);
		String formatter2 = dateTimeFormatter2.format(zonedDateTime);
		String formatter3 = dateTimeFormatter3.format(zonedDateTime);
		String formatterWithZoneStr = formatterWithZone.format(zonedDateTime);
		System.out.println("DateTimeFormatter Example Demo");
		System.out.println("------------------------------");
		System.out.println("DateTimeFormatter 1 : " + formatter1);
		System.out.println("DateTimeFormatter 2 : " + formatter2);
		System.out.println("DateTimeFormatter 3 : " + formatter3);
		System.out.println("DateTimeFormatter with Zone3 : " + formatterWithZoneStr);
		System.out.println("DateTimeFormatter().getZone() : " + getZoneMethod.getId());
		System.out.println("LocalTime.parser - DateTimeFormatter.ISO_TIME : "
				+ time);
		System.out.println("LocalDate.parser - DateTimeFormatter.ISO_TIME : "
				+ date);
	}
}

Output for the above program will be:

DateTimeFormatter Example Demo
------------------------------
DateTimeFormatter 1 : 2015/04/14 08:16:24 IST
DateTimeFormatter 2 : 2015/04/14
DateTimeFormatter 3 : 14/Apr/2015
DateTimeFormatter with Zone3 : 13/Apr/2015
DateTimeFormatter().getZone() : America/Los_Angeles
LocalTime.parser - DateTimeFormatter.ISO_TIME : 10:15:30
LocalDate.parser - DateTimeFormatter.ISO_TIME : 2015-04-10

If there is any issues in the format of the date or time passed to the parse method, the following exception will be thrown from the application:

Exception in thread "main" java.time.format.DateTimeParseException: Text '201-04-10' could not be parsed at index 0
	at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1948)
	at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1850)
	at java.time.LocalDate.parse(LocalDate.java:400)
	at javabeat.net.DateTimeFormatterExample.main(DateTimeFormatterExample.java:28)

Category: JavaTag: Java 8

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Java 8 Date Time API – LocalDateTime Example
Next Post: Java 8 Date Time API – DateTimeFormatterBuilder Example »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact