How to format date and time in Android?

前端 未结 24 1307
面向向阳花
面向向阳花 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:18

    You can use DateFormat. Result depends on default Locale of the phone, but you can specify Locale too :

    https://developer.android.com/reference/java/text/DateFormat.html

    This is results on a

    DateFormat.getDateInstance().format(date)                                          
    

    FR Locale : 3 nov. 2017

    US/En Locale : Jan 12, 1952


    DateFormat.getDateInstance(DateFormat.SHORT).format(date)
    

    FR Locale : 03/11/2017

    US/En Locale : 12.13.52


    DateFormat.getDateInstance(DateFormat.MEDIUM).format(date)
    

    FR Locale : 3 nov. 2017

    US/En Locale : Jan 12, 1952


    DateFormat.getDateInstance(DateFormat.LONG).format(date)
    

    FR Locale : 3 novembre 2017

    US/En Locale : January 12, 1952


    DateFormat.getDateInstance(DateFormat.FULL).format(date)
    

    FR Locale : vendredi 3 novembre 2017

    US/En Locale : Tuesday, April 12, 1952


    DateFormat.getDateTimeInstance().format(date)
    

    FR Locale : 3 nov. 2017 16:04:58


    DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date)
    

    FR Locale : 03/11/2017 16:04


    DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(date)
    

    FR Locale : 03/11/2017 16:04:58


    DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(date)
    

    FR Locale : 03/11/2017 16:04:58 GMT+01:00


    DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL).format(date)
    

    FR Locale : 03/11/2017 16:04:58 heure normale d’Europe centrale


    DateFormat.getTimeInstance().format(date)
    

    FR Locale : 16:04:58


    DateFormat.getTimeInstance(DateFormat.SHORT).format(date)
    

    FR Locale : 16:04


    DateFormat.getTimeInstance(DateFormat.MEDIUM).format(date)
    

    FR Locale : 16:04:58


    DateFormat.getTimeInstance(DateFormat.LONG).format(date)
    

    FR Locale : 16:04:58 GMT+01:00


    DateFormat.getTimeInstance(DateFormat.FULL).format(date)
    

    FR Locale : 16:04:58 heure normale d’Europe centrale


提交回复
热议问题