- Java Programming Examples
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Tutorial
- Java - Tutorial
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to display hour and minute (current time) in Java
Problem Description
How to display hour and minute (current time)?
Solution
This example demonstrates how to display the hour and minute of that moment by using Calender.getInstance() of Calender class.
import java.util.Calendar; import java.util.Formatter; public class MainClass{ public static void main(String args[]) { Formatter fmt = new Formatter(); Calendar cal = Calendar.getInstance(); fmt = new Formatter(); fmt.format("%tl:%tM", cal, cal); System.out.println(fmt); } }
Result
The above code sample will produce the following result. The result will change depending upon the current system time.
09:12
The following is an another sample example of Hour and minute
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class HelloWorld { public static void main(String[] args) { Calendar now = Calendar.getInstance(); System.out.println(now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE)); } }
The above code sample will produce the following result. The result will change depending upon the current system time.
5:31
java_date_time.htm
Advertisements