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); } }
Output
15:50:28 Hours: 15 Minutes: 50 Seconds: 28
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); } }
Output
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 -
< h2>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); } }
Output
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use Java to develop a Cassandra-based geolocation data application Geolocation data applications are widely used in modern society, such as map navigation, location sharing, location recommendations, etc. Cassandra is a distributed, highly scalable NoSQL database that can handle massive amounts of data and is particularly suitable for storing and querying geographical location data. This article will introduce how to use Java to develop a Cassandra-based geographical location data application and provide specific code examples. 1. Environment

The LinkedList class in Java is a class that implements a linked list data structure. It provides many useful methods to operate linked lists. Among them, the removeFirst() method can be used to delete elements from the head of the linked list. The following will introduce how to use the LinkedList.removeFirst() method and give specific code examples. Before using the LinkedList.removeFirst() method, we first need to create a LinkedList

Detailed steps for installing Kafka in a Linux environment 1. Prerequisite operating system: Linux (Ubuntu or CentOS recommended) Java: JDK8 or higher ZooKeeper: version 3.4 or higher Kafka: the latest stable version 2. Install Javasudoapt-getupdatesudoapt- getinstalldefault-jdk3.Install ZooKeeperwg

Implementing distributed counters using Redis and Java: How to achieve high concurrency Introduction: In modern Internet application development, high concurrency is a common challenge. When multiple users access an application at the same time, it needs to be able to correctly handle and track each user's request to avoid data loss or confusion. In this article, we will discuss how to implement a distributed counter using Redis and Java to achieve high-concurrency data tracking and management. 1. Introduction to Redis Redis is an open source base

Overview of how to use Linux script operations to implement remote login in Java: Remote login is a way to use one computer to log in to other computers in a network environment to perform operations. In Linux systems, we usually use the SSH protocol for remote login. This article will introduce how to implement remote login operations by calling Linux scripts in Java, and give specific code examples. Step 1: Write Linux script code First, we need to write a Linux script to pass

Using Dropbox for storage management in Java API development With the widespread application of cloud computing, more and more applications need to store data in the cloud and be able to easily read, write and manage this data. As one of the most popular cloud storage services, Dropbox provides the richest and most flexible API, allowing developers to easily integrate Dropbox's storage management functions into their applications. This article will introduce how to use Dr in JavaAPI development

How to achieve persistence of objects using serialization and deserialization in Java? Introduction: In Java development, object persistence is an important way to achieve long-term storage of data. Serialization and deserialization are one of the commonly used ways to achieve object persistence in Java. This article will introduce the concepts of serialization and deserialization and how to use serialization and deserialization in Java to achieve object persistence. 1. What are serialization and deserialization? Serialization is the process of converting an object into a byte stream so that the object can be transmitted or saved over the network.

As a high-level programming language, Java is widely used in enterprise-level development and mobile application development. In JAVA, processing data is a very important task. In many cases, data needs to be formatted to ensure program correctness and operating efficiency. However, during this process, you may encounter data format errors, and these errors may cause the program to fail to run properly. In JAVA, java.text.ParseException is a common formatting error and a
