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 -
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!

如何使用Java开发一个基于Cassandra的地理位置数据应用地理位置数据应用在现代社会中被广泛使用,例如地图导航、位置共享、位置推荐等。Cassandra是一个分布式、高可扩展性的NoSQL数据库,它能够处理海量数据,特别适合存储和查询地理位置数据。本文将介绍如何使用Java开发一个基于Cassandra的地理位置数据应用,并提供具体的代码示例。1.环境

Java中LinkedList类是一个实现了链表数据结构的类,它提供了许多有用的方法来操作链表。其中,removeFirst()方法可以用来从链表头部删除元素。下面将介绍如何使用LinkedList.removeFirst()方法,并且给出具体的代码示例。在使用LinkedList.removeFirst()方法之前,我们首先需要创建一个LinkedList

Linux环境下安装Kafka的详细步骤1.前提条件操作系统:Linux(推荐使用Ubuntu或CentOS)Java:JDK8或更高版本ZooKeeper:版本3.4或更高版本Kafka:最新稳定版本2.安装Javasudoapt-getupdatesudoapt-getinstalldefault-jdk3.安装ZooKeeperwg

利用Redis和Java实现分布式计数器:如何实现高并发引言:在现代互联网应用程序开发中,高并发是一个常见的挑战。当多个用户同时访问一个应用程序时,它需要能够正确地处理和跟踪每个用户的请求,以避免数据的丢失或混乱。在这篇文章中,我们将讨论如何利用Redis和Java实现一个分布式计数器,以实现高并发的数据跟踪和管理。一、Redis简介Redis是一个开源的基

如何在Java中使用Linux脚本操作实现远程登录概述:远程登录是在网络环境中,使用一台计算机登录到其他计算机上进行操作的一种方式。在Linux系统中,我们通常会使用SSH协议来进行远程登录。本文将介绍如何在Java中通过调用Linux脚本来实现远程登录的操作,并给出具体的代码示例。步骤一:编写Linux脚本代码首先,我们需要编写一个Linux脚本,用于通过

JavaAPI开发中使用Dropbox进行存储管理随着云计算的广泛应用,越来越多的应用程序需要将数据存储在云端,并能够方便地读写和管理这些数据。而Dropbox作为最流行的云存储服务之一,提供了最为丰富和灵活的API,使得开发者能够方便地在自己的应用程序中集成Dropbox的存储管理功能。本文将介绍如何在JavaAPI开发中使用Dr

如何使用Java中的序列化和反序列化实现对象的持久化?引言:在Java开发中,对象的持久化是实现数据长久存储的一种重要方式。而序列化和反序列化是Java中常用的实现对象持久化的方式之一。本文将介绍序列化和反序列化的概念以及如何使用Java中的序列化和反序列化实现对象的持久化。一、什么是序列化和反序列化?序列化是将对象转换为字节流的过程,使得对象在网络传输或保

Java作为一种高级编程语言,被广泛应用于企业级开发和移动应用开发中。在JAVA中,处理数据是一个非常重要的任务。在许多情况下,需要对数据进行格式化处理,以保证程序的正确性和运行效率。但在这个过程中,可能会遇到数据格式错误的情况,而这些错误可能会导致程序无法正常运行。在JAVA中,java.text.ParseException是一种常见的格式化错误,也是一


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft