集册 Java实例教程 使用SimpleDateFormat格式化小时

使用SimpleDateFormat格式化小时

欢马劈雪     最近更新时间:2020-01-02 10:19:05

620
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
小时可以采用H、HH、H、HH、k、kk、k和kk格式。

 

import java.text.SimpleDateFormat;//时代Java 提供

import java.util.Date;


public class Main {


  public static void main(String[] args) {


    // create Date object

    Date date = new Date();


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

    String strDateFormat = "h";

    SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);


    System.out.println("hour in h format : " + sdf.format(date));
/** 来自 N  o w  J a v a . c o m**/

    // formatting hour in hh (01-12 in AM/PM) format like 01, 02..12.

    strDateFormat = "hh";

    sdf = new SimpleDateFormat(strDateFormat);


    System.out.println("hour in hh format : " + sdf.format(date));


    // formatting hour in H (0-23) format like 0, 1...23.

    strDateFormat = "H";

    sdf = new SimpleDateFormat(strDateFormat);


    System.out.println("hour in H format : " + sdf.format(date));


    // formatting hour in HH (00-23) format like 00, 01..23.

    strDateFormat = "HH";

    sdf = new SimpleDateFormat(strDateFormat);


    System.out.println("hour in HH format : " + sdf.format(date));


    // formatting hour in k (1-24) format like 1, 2..24.

    strDateFormat = "k";

    sdf = new SimpleDateFormat(strDateFormat);


    System.out.println("hour in k format : " + sdf.format(date));


    // formatting hour in kk (01-24) format like 01, 02..24.

    strDateFormat = "kk";

    sdf = new SimpleDateFormat(strDateFormat);


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


    // formatting hour in K (0-11 in AM/PM) format like 0, 1..11.

    strDateFormat = "K";

    sdf = new
展开阅读全文