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


Tip of the Day
Language: Java
Expertise: Intermediate
Oct 1, 2019

WEBINAR:

On-Demand

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


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
 
Thanks for your registration, follow us on our social networks to keep up-to-date