Home » Tip Bank » Java » Classes
  • Comment on this Article
Language: Java
Expertise: Intermediate
Oct 1, 2019
  • facebook
  • twitter
  • in
  • Email

WEBINAR:

On-Demand

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


Watch

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

*/
M S Sridhar
 
Submit a Tip
Browse "Java" Tips
Browse All Tips
htmlandcssbook.com
javascriptkit.com