Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Display hour with SimpleDateFormat(“H”) in Java
To work with SimpleDateFormat class in Java, import the following package.
import java.text.SimpleDateFormat;
Now, set the format with SimpleDateFormat(“H”) to display hour in a day.
Format f = new SimpleDateFormat("H");
Now, get the hour −
String strHour = 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);
}
}
Output
Today's date = 26/November/2018 08:00:2 Current Hour = 8
Advertisements
