How to use formatting with printf() correctly in Java?

JavaServer Side ProgrammingProgramming

The printf() method is used to print a formatted string, it accepts a string representing a format string and an array of objects representing the elements that are to be in the resultant string, if the number of arguments are more than the number of characters in the format string the excess objects are ignored.

Following table lists the various format characters to format time by the Java printf() method along with their description −

Format CharactersDescription
'H'
The corresponding argument is formatted as Hour of the day (00-24).
'I'
The corresponding argument is formatted as hour of the day (01 -12).
'k'
The corresponding argument is formatted as hour of the day (0-24).
'l'
The corresponding argument is formatted as hour of the day (1-12).
'M'
The corresponding argument is formatted as minutes of an hour (00-59).
'S'
The corresponding argument is formatted as seconds of a minute (00-60).
'L'
The corresponding argument is formatted as milliseconds (000-999).
'N'
The corresponding argument is formatted as nano seconds (000000000 - 999999999).
'p'
The corresponding argument is formatted as pm or am.
'z'
The corresponding argument is formatted as time zxone.
'Z'
The corresponding argument is formatted as string representing the time zone.
's'
The corresponding argument is formatted as seconds since the epoch.
'Q'
The corresponding argument is formatted as milliseconds since the epoch.

Example

Following example demonstrates how to format a date value using the printf() method.

Live Demo

import java.util.Date;
public class Example {
   public static void main(String args[]) {  
      //creating the date class
      Date obj = new Date();
      System.out.printf("%tT%n", obj);
      System.out.printf("Hours: %tH%n", obj);
      System.out.printf("Minutes: %tM%n", obj);
      System.out.printf("Seconds: %tS%n", obj);
   }
}

Output

15:50:28
Hours: 15
Minutes: 50
Seconds: 28

Example

Following example demonstrates how to print 12 hours and 24 hours’ time using the java pritntf() method.

Live Demo

import java.util.Date;
public class Example {
   public static void main(String args[]) {  
      //creating the date class
      Date obj = new Date();
      System.out.printf("%tT%n", obj);
      System.out.printf("Time 12 hours: %tI:%tM %tp %n", obj, obj, obj); System.out.printf("Time 24 hours: %tH: hours %tM: minutes %tS: seconds%n", obj, obj, obj);
   }
}

Output

11:38:08
Time 12 hours: 11:38 am
Time 24 hours: 11: hours 38: minutes 08: seconds

If you observe in the above example, we are using the same date object to print various values, we can avoid multiple arguments using the index reference 1$ as shown below −

Example

Live Demo

import java.util.Date;
public class Example {
   public static void main(String args[]) {  
      //creating the date class
      Date obj = new Date();
      System.out.printf("%tT%n", obj);
      System.out.printf("Time 12 hours: %tI:%1$tM %1$tp %n", obj);
      System.out.printf("Time 24 hours: %1$tH: hours %1$tM: minutes %1$tS: seconds%n", obj);
   }
}

Output

11:47:13
Time 12 hours: 11:47 am
Time 24 hours: 11: hours 47: minutes 13: seconds
raja
Published on 06-Feb-2021 04:07:16
Advertisements