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:

[[email protected]]# java FormatterExFormat-1Current time: 18:17:32Format-2Current time: Fri Aug 23 18:17:32 IST 2019*/
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

More From DevX