Printing out datetime in a specific format in Java?

后端 未结 4 1260
抹茶落季
抹茶落季 2020-12-02 00:58

I want to print out datetime in java in a specific format. I have this C# code which prints out the datetime in this format.

DateTime value = new DateTime(2         


        
4条回答
  •  醉梦人生
    2020-12-02 01:14

    If you need date in 24 hour system then use this approach

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Date custDate = new Date();
    System.out.println(sdf.format(custDate));
    

    Please note in 24 hour system there is no need to show AM/PM.

    If you want date in 12 hour system then use below approach

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
    Date custDate = new Date();
    System.out.println(sdf.format(custDate));
    

    "a" in the date format will help to show AM/PM.

    Please import below classes for above code to work

    java.text.SimpleDateFormat

    java.util.Date

提交回复
热议问题