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
Display just hour and minute using Formatter in Java
Firstly, create a Formatter and a Calendar object.
Formatter f = new Formatter(); Calendar c = Calendar.getInstance();
To display hour using Formatter class, use the ‘l’ conversion a character −
f = new Formatter();
System.out.println(f.format("Hour: %tl", c));
To display minute using Formatter class, use the ‘M’ conversion character −
f = new Formatter();
System.out.println(f.format("Minute: %1$tM", c));
The following is an example −
Example
import java.util.Calendar;
import java.util.Formatter;
public class Demo {
public static void main(String args[]) {
Formatter f = new Formatter();
Calendar c = Calendar.getInstance();
System.out.println("Current date and time: "+c.getTime());
f = new Formatter();
System.out.println(f.format("Hour and Minute with AM/ PM: %tl:%1$tM %1$Tp", c));
f = new Formatter();
System.out.println(f.format("Hour: %tl", c));
f = new Formatter();
System.out.println(f.format("Minute: %1$tM", c));
}
}
Output
Current date and time: Mon Nov 26 07:35:43 UTC 2018 Hour and Minute with AM/ PM: 7:35 AM Hour: 7 Minute: 35
Advertisements