Formatting hour using SimpleDateFormat: k

 



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("k");
    System.out.println("hour in k format : " + sdf.format(date));

    sdf = new SimpleDateFormat("kk");
    System.out.println("hour in kk format : " + sdf.format(date));

    sdf = new SimpleDateFormat("K");
    System.out.println("hour in K format : " + sdf.format(date));

    sdf = new SimpleDateFormat("KK");
    System.out.println("hour in KK format : " + sdf.format(date));

    
  }
}

 

The output:


hour in k format : 10
hour in kk format : 10
hour in K format : 10
hour in KK format : 10
Home 
  Java Book 
    Essential Classes  

SimpleDateFormat:
  1. SimpleDateFormat class introduction
  2. SimpleDateFormat formats date, day,
  3. Add AM PM to time format using SimpleDateFormat
  4. Formatting date in default formats using DateFormat
  5. Formatting day using SimpleDateFormat
  6. Formatting day of week using SimpleDateFormat
  7. Formatting hour using SimpleDateFormat
  8. Formatting hour using SimpleDateFormat: k
  9. Formatting Minutes using SimpleDateFormat
  10. Formatting month using SimpleDateFormat
  11. Formatting seconds using SimpleDateFormat
  12. Formatting TimeZone using SimpleDateFormat
  13. Formatting year using SimpleDateFormat
  14. Use various format
  15. Convert date string from one format to another format using SimpleDateFormat