
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Formatting with printf Correctly in Java
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 Characters | Description |
---|---|
'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.
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.
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
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