Format hour in h (1-12 in AM/PM) format like 1, 2..12 in Java

Description

The following code shows how to format hour in h (1-12 in AM/PM) format like 1, 2..12.

Example


//from ww w . j  a v  a  2  s .  c  om
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {

  public static void main(String[] args) {
    Date date = new Date();
    
    String strDateFormat = "h";
    SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
    System.out.println("hour in h format : " + sdf.format(date));
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Date »




Date Get
Date Set
Date Format
Date Compare
Date Convert
Date Calculation
Date Parse
Timezone