Formatting hour using SimpleDateFormat
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("h");
System.out.println("hour in h format : " + sdf.format(date));
sdf = new SimpleDateFormat("hh");
System.out.println("hour in hh format : " + sdf.format(date));
sdf = new SimpleDateFormat("H");
System.out.println("hour in H format : " + sdf.format(date));
sdf = new SimpleDateFormat("HH");
System.out.println("hour in HH format : " + sdf.format(date));
}
}
The output:
hour in h format : 10
hour in hh format : 10
hour in H format : 10
hour in HH format : 10
Home
Java Book
Essential Classes
Java Book
Essential Classes
SimpleDateFormat:
- SimpleDateFormat class introduction
- SimpleDateFormat formats date, day,
- Add AM PM to time format using SimpleDateFormat
- Formatting date in default formats using DateFormat
- Formatting day using SimpleDateFormat
- Formatting day of week using SimpleDateFormat
- Formatting hour using SimpleDateFormat
- Formatting hour using SimpleDateFormat: k
- Formatting Minutes using SimpleDateFormat
- Formatting month using SimpleDateFormat
- Formatting seconds using SimpleDateFormat
- Formatting TimeZone using SimpleDateFormat
- Formatting year using SimpleDateFormat
- Use various format
- Convert date string from one format to another format using SimpleDateFormat