Home >Java >javaTutorial >How to correctly use the printf() function for formatted output in Java?
The printf() method is used to print the formatted string. It accepts a string representing the format string and an array of objects representing the elements in the result string. If the number of parameters is greater than the number of characters in the format string, extra objects will be ignored.
The following table lists the various format characters and their descriptions for the Java printf() method format time -
Format characters | Description |
---|---|
'H' | The format of the corresponding parameter is the hour of the day (00-24). |
'I' | The corresponding parameter format is the hour of the day (01 -12). | 'k' | The format of the corresponding parameter is the hour of the day (0-24). |
'l' | The format of the corresponding parameter is the hour of the day (1-12). |
'M' | The format of the corresponding parameter is the number of minutes in an hour (00-59). |
'S' | The format of the corresponding parameter is the number of seconds in a minute (00-60). |
'L' | The corresponding parameter format is milliseconds (000-999). |
'N' | The format of the corresponding parameter is nanoseconds (000000000 - 999999999). |
'p' | The corresponding parameter format is pm or am. |
'z' | The corresponding parameter format is time zxone. p> |
'Z' | The corresponding parameter format is a string representing the time zone. p> |
's' | The corresponding parameter format is the number of seconds since the epoch. |
'Q' | The corresponding parameter format is the number of milliseconds since the epoch. |
The following example demonstrates how to use the printf() method to format a date value.
Live Demonstration
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); } }
15:50:28 Hours: 15 Minutes: 50 Seconds: 28
The following example demonstrates how to print 12 hour and 24 hour time using 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); } }
11:38:08 Time 12 hours: 11:38 am Time 24 hours: 11: hours 38: minutes 08: seconds
If you observed in the above example, we are using the same date object to print different values , we can use index reference 1$ to avoid multiple parameters as shown below -
ExampleLive Demonstration
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); } }
11:47:13 Time 12 hours: 11:47 am Time 24 hours: 11: hours 47: minutes 13: seconds
The above is the detailed content of How to correctly use the printf() function for formatted output in Java?. For more information, please follow other related articles on the PHP Chinese website!