- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 seconds with SimpleDateFormat(βsβ) in Java
To work with SimpleDateFormat class in Java, import the following package.
import java.text.SimpleDateFormat;
Now, set the format with SimpleDateFormat(“s”) to display seconds in one-digit.
Format f = new SimpleDateFormat(”s”);
Now, get the seconds in a string.
String strSeconds = f.format(new Date());
The following is an example −
Example
import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current date and time Calendar cal = Calendar.getInstance(); SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MMMM/yyyy hh:mm:s"); System.out.println("Today's date = "+simpleformat.format(cal.getTime())); // displaying hour Format f = new SimpleDateFormat("H"); String strHour = f.format(new Date()); System.out.println("Current Hour = "+strHour); // displaying minutes f = new SimpleDateFormat("mm"); String strMinute = f.format(new Date()); System.out.println("Current Minutes = "+strMinute); // displaying seconds in one-digit f = new SimpleDateFormat("s"); String strSeconds = f.format(new Date()); System.out.println("Current Seconds = "+strSeconds); } }
Output
Today's date = 26/November/2018 08:05:3 Current Hour = 8 Current Minutes = 05 Current Seconds = 3
- Related Articles
- Display seconds with SimpleDateFormat('ss') in Java
- Display hour with SimpleDateFormat(βHβ) in Java
- Display minutes with SimpleDateFormat(βmβ) in Java
- Display todayβs date in Java with SimpleDateFormat
- Display hour with SimpleDateFormat(βhhβ) in Java
- Display time zone with SimpleDateFormat(βzβ) in Java
- Display the month number with SimpleDateFormat(βMβ) in Java
- Display minutes with SimpleDateFormat('mm') in Java
- Display year with SimpleDateFormat('yyyy') in Java
- Display AM/PM time marker with SimpleDateFormat(βaβ) in Java
- Display day number with SimpleDateFormat('d') in Java
- Display three letter-month value with SimpleDateFormat('MMM') in Java
- Set Date patterns with SimpleDateFormat in Java
- Display the day in week using SimpleDateFormat('E') in Java
- Format Seconds in s format in Java

Advertisements