How to format date and time in Android?

前端 未结 24 1606
面向向阳花
面向向阳花 2020-11-22 00:51

How to format correctly according to the device configuration date and time when having a year, month, day, hour and minute?

24条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 01:31

    In my opinion, android.text.format.DateFormat.getDateFormat(context) makes me confused because this method returns java.text.DateFormat rather than android.text.format.DateFormat - -".

    So, I use the fragment code as below to get the current date/time in my format.

    android.text.format.DateFormat df = new android.text.format.DateFormat();
    df.format("yyyy-MM-dd hh:mm:ss a", new java.util.Date());
    
    or
    
    android.text.format.DateFormat.format("yyyy-MM-dd hh:mm:ss a", new java.util.Date());
    

    In addition, you can use others formats. Follow DateFormat.

提交回复
热议问题