
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 Time in 24-Hour Format in Java
Use the SimpleDateFormat class to display time in 24-hour format.
Set the format
Date dt = new Date(); SimpleDateFormat dateFormat; dateFormat = new SimpleDateFormat("kk:mm:ss");
Now, the following will display time in 24-hour format
dateFormat.format(dt)
The following is an example
Example
import java.text.SimpleDateFormat; import java.util.Date; public class Demo { public static void main(String[] argv) throws Exception { Date dt = new Date(); SimpleDateFormat dateFormat; dateFormat = new SimpleDateFormat("kk:mm:ss"); System.out.println("Time in 24 hr format = "+dateFormat.format(dt)); } }
Output
Time in 24 hr format = 11:40:52
Advertisements