
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to use 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
- Related Articles
- Date Formatting Using printf
- How to correctly use WITH ROLLUP in MySQL?
- How to use printf () in Android sqlite?
- How to use quotes correctly while using LIKE with search variable in MySQL in Java?
- How to use Emphasized formatting in HTML?
- How to use small formatting in HTML?
- Format numerical data with printf in Java
- How to use % wildcard correctly in MySQL?
- How to use save() correctly in MongoDB?
- How to format date using printf() method in Java?
- How to format time using printf() method in Java?
- Display localized month name with printf method in Java
- How to correctly use DELIMITER in a MySQL stored procedure?
- how to shuffle a 2D array in java correctly?
- How to use the tag for keyboard input formatting in HTML?
