Date and time formatting depending on locale

前端 未结 6 969
生来不讨喜
生来不讨喜 2020-12-14 06:17

I\'m new to both Java and Android development so this might be a stupid question, but I\'ve been searching for days now and can\'t find a solution:

I try to output a

6条回答
  •  攒了一身酷
    2020-12-14 07:09

    public static String formatDate(Date date, boolean withTime)
    {
        String result = "";
        DateFormat dateFormat;
    
        if (date != null)
        {
            try
            {
                String format = Settings.System.getString(context.getContentResolver(), Settings.System.DATE_FORMAT);
                if (TextUtils.isEmpty(format))
                {
                    dateFormat = android.text.format.DateFormat.getDateFormat(context);
                }
                else
                {
                    dateFormat = new SimpleDateFormat(format);
                }
                result = dateFormat.format(date);
    
                if (withTime)
                {
                    dateFormat = android.text.format.DateFormat.getTimeFormat(context);
                    result += " " + dateFormat.format(date);
                }
            }
            catch (Exception e)
            {
            }
        }
    
        return result;
    }
    

提交回复
热议问题