Employing the Formatter Class in Java

Employing the Formatter Class in Java

The Formatter class is used to format the output as needed.

Here, we are trying to print the current time formatted in more than one format. Please see the output to understand further

import java.util.*;public class FormatterEx{   public static void main(String args[])   {      FormatterEx formatterEx = new FormatterEx();      formatterEx.proceed();   }      private void proceed()   {      // The output is formatted adn then written to the console      System.out.println("Format-1");      System.out.format("Current time: %tT", Calendar.getInstance());      System.out.println();      System.out.println("Format-2");      System.out.format("Current time: %tc%n", Calendar.getInstance());      //These are few examples. There is plenty of documentation available using which you can improve the output as needed   }}/*

Expected output:

[root@mypc]# java FormatterExFormat-1Current time: 18:17:32Format-2Current time: Fri Aug 23 18:17:32 IST 2019*/

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist