• 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.util.Formatter Example

June 23, 2014 //  by Krishna Srinivasan//  Leave a Comment

java.util.Formatter class is used for formatting the values passed to the format() method. This class become part of Java since the Java 5 release. This feature is taken from the C language to format the string, numbers, date, etc. in the simplified manner. There is set of variables and expression defined for formatting the values.

also read:

  • Compress Files To ZIP Format Using Java
  • Compress and Uncompress a Zip File
  • Locking files using Java

The formatted values can be either printed in the console and can be written to the file. This feature can be configured in the java.util.Formatter constructor. This API has huge number of constructors and options for the formatting. We have written a very simple example to demonstrate the use of this class.

Lets look at the example:

JavaFormatterExample.java

package javabeat.net.java.core;

import java.util.Calendar;
import java.util.Formatter;

/**
 * java.util.Formatter Example
 *
 * @author Krishna
 *
 */
public class JavaFormatterExample {
	public static void main(String args[]) {
		Formatter format1 = new Formatter();

		//Formatting numbers
		format1.format("Formatting %s : int ( %d )  float ( %f )",
				"numbers example ", 10, 98.6);
		System.out.println(format1);

		//Formatting date and time
		Formatter format2 = new Formatter();
		Calendar cal = Calendar.getInstance();

		format2.format("%tr", cal);
		System.out.println(format2);

		format2 = new Formatter();
		format2.format("%tc", cal);
		System.out.println(format2);

		format2 = new Formatter();
		format2.format("%tl:%tM", cal, cal);
		System.out.println(format2);

		format2 = new Formatter();
		format2.format("%tB %tb %tm", cal, cal, cal);
		System.out.println(format2);
	}
}

Output…

Formatting numbers example  : int ( 10 )  float ( 98.600000 )
02:42:16 PM
Mon Jun 23 14:42:16 IST 2014
2:42
June Jun 06

Category: JavaTag: Java Util

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 Logging Handlers Example
Next Post: Create Custom Formatter for Logger Handler »

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