Java Program to format time in 24 hours format


This Java example will show you how to format time in 24 hours format

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));
   }
}

 

 

Output:

hour in h format : 2

 

Example 2:

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
   public static void main(String[] argv) throws Exception {
      Date d = new Date();
      SimpleDateFormat simpDate;
      simpDate = new SimpleDateFormat("hh:mm:ss");
      System.out.println(simpDate.format(d));
   }
}

 

Output:

02:17:06


Subscribe For Daily Updates

JAVA File IO examples

JAVA Date & Time Progamming Examples

JAVA Collections

Flutter Questions
Android Questions