dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tip: Employing the Formatter Class in Java

Learn about how to use the Formatter class to ensure that you're getting the output that you require.


advertisement

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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 FormatterEx
Format-1
Current time: 18:17:32
Format-2
Current time: Fri Aug 23 18:17:32 IST 2019

*/

 

Visit the DevX Tip Bank

 





   
Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Thanks for your registration, follow us on our social networks to keep up-to-date