How to format date and time in Android?

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

    It's too late but it may help to someone

    DateFormat.format(format, timeInMillis);
    

    here format is what format you need

    ex: "HH:mm" returns 15:30

    0 讨论(0)
  • 2020-11-22 01:15

    Try:

    event.putExtra("startTime", "10/05/2012");
    

    And when you are accessing passed variables:

    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    Date date = formatter.parse(bundle.getString("startTime"));
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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


    0 讨论(0)
  • 2020-11-22 01:18

    This will do it:

    Date date = new Date();
    java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
    mTimeText.setText("Time: " + dateFormat.format(date));
    
    0 讨论(0)
  • 2020-11-22 01:18

    Following this: http://developer.android.com/reference/android/text/format/Time.html

    Is better to use Android native Time class:

    Time now = new Time();
    now.setToNow();
    

    Then format:

    Log.d("DEBUG", "Time "+now.format("%d.%m.%Y %H.%M.%S"));
    
    0 讨论(0)
提交回复
热议问题