How to format date and time in Android?

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

    Use the standard Java DateFormat class.

    For example to display the current date and time do the following:

    Date date = new Date(location.getTime());
    DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
    mTimeText.setText("Time: " + dateFormat.format(date));
    

    You can initialise a Date object with your own values, however you should be aware that the constructors have been deprecated and you should really be using a Java Calendar object.

提交回复
热议问题