How to format date and time in Android?

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

    Locale

    To get date or time in locale format from milliseconds I used this:

    Date and time

    Date date = new Date(milliseconds);
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.getDefault());
    dateFormat.format(date);
    

    Date

    Date date = new Date(milliseconds);
    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
    dateFormat.format(date);
    

    Time

    Date date = new Date(milliseconds);
    DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
    dateFormat.format(date);
    

    You can use other date style and time style. More info about styles here.

提交回复
热议问题