如何从字符串格式化android中的日期

如何从字符串格式化android中的日期,android,date,simpledateformat,localdate,Android,Date,Simpledateformat,Localdate,日志打印 dateStr->2019-07-05 12:05:36 currentDate->Fri Jul 05 00:00:00 GMT+2019 02:00 我需要-->2019年7月5日星期五并翻译成马其顿语 String dateStr = obj.getString("sent_date"); Log.d("date", dateStr); SimpleDateFormat sdf = new SimpleDateFormat("yyy

日志打印
dateStr
->
2019-07-05 12:05:36
currentDate
->
Fri Jul 05 00:00:00 GMT+2019 02:00

我需要-->
2019年7月5日星期五
并翻译成马其顿语

String dateStr = obj.getString("sent_date");
            Log.d("date", dateStr);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date currentDate = null;
            try {
                currentDate = sdf.parse(dateStr);
                Log.d("date", currentDate.toString());

如果要将SimpleDataFormat与马其顿本地语言结合使用,可以执行以下操作:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new Locale("mk", "MK"));
或者,您可以通过以下方式使用DateFormat:

Date date = new Date(location.getTime());
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
myDate = df.parse(dateStr);

有关更多格式选项,请参见

如果要将SimpleDataFormat与Macedonian local一起使用,可以执行以下操作:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new Locale("mk", "MK"));
或者,您可以通过以下方式使用DateFormat:

Date date = new Date(location.getTime());
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
myDate = df.parse(dateStr);

有关更多格式选项,如果要查找日期格式字符串,可以查看

SimpleDateFormat resultFormat = new SimpleDateFormat("E dd MMM yyyy", new Locale("mk", "MK"));
resultFormat.format(currentDate)

应提供预期结果。

如果要查找日期格式字符串

SimpleDateFormat resultFormat = new SimpleDateFormat("E dd MMM yyyy", new Locale("mk", "MK"));
resultFormat.format(currentDate)

应该会给您预期的结果。

请尝试以下代码:

  @SuppressLint("SimpleDateFormat")
    public static String formatTime(String dateFormat) {
        String inputTimePattern = "EEE MMM dd HH:mm:ss zzzz yyyy";
        String outputTimePattern = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat inputFormat = new SimpleDateFormat(inputTimePattern, Locale.ENGLISH);
        SimpleDateFormat outputFormat = new SimpleDateFormat(outputTimePattern, new Locale("mk" ,""));
        Date date;
        try {
            date = inputFormat.parse(dateFormat);
            return outputFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateFormat;
    }

    Log.d(TAG, formatTime("Fri Jul 05 00:00:00 GMT+02:00 2019"));

    Output -> 2019-07-05 00:00:00

尝试以下代码:

  @SuppressLint("SimpleDateFormat")
    public static String formatTime(String dateFormat) {
        String inputTimePattern = "EEE MMM dd HH:mm:ss zzzz yyyy";
        String outputTimePattern = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat inputFormat = new SimpleDateFormat(inputTimePattern, Locale.ENGLISH);
        SimpleDateFormat outputFormat = new SimpleDateFormat(outputTimePattern, new Locale("mk" ,""));
        Date date;
        try {
            date = inputFormat.parse(dateFormat);
            return outputFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateFormat;
    }

    Log.d(TAG, formatTime("Fri Jul 05 00:00:00 GMT+02:00 2019"));

    Output -> 2019-07-05 00:00:00

您可以使用此功能只需将您的愿望日期格式

private String returnData(String date) {
    SimpleDateFormat localDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    localDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    try {
        localDateFormat.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    String finalDate = localDateFormat.format(new Date());
    return finalDate;
 }

您可以使用此功能只需将您的愿望日期格式

private String returnData(String date) {
    SimpleDateFormat localDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    localDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    try {
        localDateFormat.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    String finalDate = localDateFormat.format(new Date());
    return finalDate;
 }

避免使用的SimpleDataFormatPossible duplicate避免使用的SimpleDataFormatPossible duplicate