Home > Java > javaTutorial > body text

How to correctly use the printf() function for formatted output in Java?

PHPz
Release: 2023-09-12 23:57:02
forward
788 people have browsed it

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.

Example

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);
   }
}
Copy after login

Output

15:50:28
Hours: 15
Minutes: 50
Seconds: 28
Copy after login

Example

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);
   }
}
Copy after login

Output

11:38:08
Time 12 hours: 11:38 am
Time 24 hours: 11: hours 38: minutes 08: seconds
Copy after login

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 -

< h2>Example

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("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);
   }
}
Copy after login

Output

11:47:13
Time 12 hours: 11:47 am
Time 24 hours: 11: hours 47: minutes 13: seconds
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!