How to format date and time in Android?

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

    0 讨论(0)
  • Use SimpleDateFormat

    Like this:

    event.putExtra("starttime", "12/18/2012");
    
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
    Date date = format.parse(bundle.getString("starttime"));
    
    0 讨论(0)
  • 2020-11-22 01:20

    This is my method, you can define and input and output format.

    public static String formattedDateFromString(String inputFormat, String outputFormat, String inputDate){
        if(inputFormat.equals("")){ // if inputFormat = "", set a default input format.
            inputFormat = "yyyy-MM-dd hh:mm:ss";
        }
        if(outputFormat.equals("")){
            outputFormat = "EEEE d 'de' MMMM 'del' yyyy"; // if inputFormat = "", set a default output format.
        }
        Date parsed = null;
        String outputDate = "";
    
        SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
        SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());
    
        // You can set a different Locale, This example set a locale of Country Mexico.
        //SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, new Locale("es", "MX"));
        //SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, new Locale("es", "MX"));
    
        try {
            parsed = df_input.parse(inputDate);
            outputDate = df_output.format(parsed);
        } catch (Exception e) { 
            Log.e("formattedDateFromString", "Exception in formateDateFromstring(): " + e.getMessage());
        }
        return outputDate;
    
    }
    
    0 讨论(0)
  • 2020-11-22 01:21

    SimpleDateFormat

    I use SimpleDateFormat without custom pattern to get actual date and time from the system in the device's preselected format:

    public static String getFormattedDate() {
        //SimpleDateFormat called without pattern
        return new SimpleDateFormat().format(Calendar.getInstance().getTime());
    }
    

    returns:

    • 13.01.15 11:45
    • 1/13/15 10:45 AM
    • ...
    0 讨论(0)
  • 2020-11-22 01:22

    The other answers are generally correct. I should like to contribute the modern answer. The classes Date, DateFormat and SimpleDateFormat used in most of the other answers, are long outdated and have caused trouble for many programmers over many years. Today we have so much better in java.time, AKA JSR-310, the modern Java date & time API. Can you use this on Android yet? Most certainly! The modern classes have been backported to Android in the ThreeTenABP project. See this question: How to use ThreeTenABP in Android Project for all the details.

    This snippet should get you started:

        int year = 2017, month = 9, day = 28, hour = 22, minute = 45;
        LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute);
        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        System.out.println(dateTime.format(formatter));
    

    When I set my computer’s preferred language to US English or UK English, this prints:

    Sep 28, 2017 10:45:00 PM
    

    When instead I set it to Danish, I get:

    28-09-2017 22:45:00
    

    So it does follow the configuration. I am unsure exactly to what detail it follows your device’s date and time settings, though, and this may vary from phone to phone.

    0 讨论(0)
  • Shortest way:

    // 2019-03-29 16:11
    String.format("%1$tY-%<tm-%<td %<tR", Calendar.getInstance())
    

    %tR is short for %tH:%tM, < means to reuse last parameter(1$).

    It is equivalent to String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM", Calendar.getInstance())

    https://developer.android.com/reference/java/util/Formatter.html

    0 讨论(0)
提交回复
热议问题