DateUtils.getRelativeTimeSpanString强制在android中的其他语言

时间:2015-02-24 13:59:43

标签: android

我正在尝试在android中实现DateUtils.getRelativeTimeSpanString但我想用意大利语显示数据,即使手机的默认语言设置为英语 我想显示数据,即“1 ora fa”而不是“1分钟前”

我正在使用以下代码

DateUtils.  getRelativeTimeSpanString (currenttimeMillis, System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS).toString().toLowerCase(Locale.ITALIAN)

2 个答案:

答案 0 :(得分:0)

我使用了这种方法

public static String getRelativeTimeSpanString(Context context, Date fromdate) {

long then;
then = fromdate.getTime();
Date date = new Date(then);

StringBuffer dateStr = new StringBuffer();

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Calendar now = Calendar.getInstance();

int days = daysBetween(calendar.getTime(), now.getTime());
int weeks= days/7;
int minutes = hoursBetween(calendar.getTime(), now.getTime());
int hours = minutes / 60;
if (days == 0) {

    int second = minuteBetween(calendar.getTime(), now.getTime());
    if (minutes > 60) {
        if (hours >= 1 && hours <= 24) {
            dateStr.append(String.format("%s %s %s",
                    hours,
                    context.getString(R.string.hour)
                    ,context.getString(R.string.ago)));
        }
    }else {
        if (second <= 10) {
            dateStr.append(context.getString(R.string.now));
        } else if (second > 10 && second <= 30) {
            dateStr.append(context.getString(R.string.few_seconds_ago));
        } else if (second > 30 && second <= 60) {
            dateStr.append(String.format("%s %s %s",
                    second,
                    context.getString(R.string.seconds)
                    ,context.getString(R.string.ago)));
        } else if (second >= 60 && minutes <= 60) {
            dateStr.append(String.format("%s %s %s",
                    minutes,
                    context.getString(R.string.minutes)
                    ,context.getString(R.string.ago)));
        }
    }
} else

if (hours > 24 && days < 7) {
    dateStr.append(String.format("%s %s %s",
            days,
            context.getString(R.string.days)
            ,context.getString(R.string.ago)));
}else if(weeks == 1){
    dateStr.append(String.format("%s %s %s",
            weeks,
            context.getString(R.string.weeks)
            ,context.getString(R.string.ago)));
}
else {
    /**
     * make formatted createTime by languageTag("fa")
     *
     * @return
     */
    return SimpleDateFormat.getDateInstance(SimpleDateFormat.LONG,new Locale("fra"))
            .format(date).toUpperCase();
}

return dateStr.toString();
}

public static int minuteBetween(Date d1, Date d2) {
return (int) ((d2.getTime() - d1.getTime()) / android.text.format.DateUtils.SECOND_IN_MILLIS);
}

public static int hoursBetween(Date d1, Date d2) {
return (int) ((d2.getTime() - d1.getTime()) / android.text.format.DateUtils.MINUTE_IN_MILLIS);
}

public static int daysBetween(Date d1, Date d2) {
return (int) ((d2.getTime() - d1.getTime()) / android.text.format.DateUtils.DAY_IN_MILLIS);
}

我将date设置为此方法的参数:

return getRelativeTimeSpanString(context,this.createdTime);

这个按照我想要的位置返回字符串!

答案 1 :(得分:-1)

由于我不知道如何格式化DateUtils.getRelativeTimeSpanString的语言环境,因此很难手动更改默认用户设备提供的语言。我使用TimeUnit制作了这个方法。

在这里,我让您更简单,只需使用此方法并更改将在您的语言中使用的时区,日期格式和字符串。如果您有任何问题或我错过了什么,请告诉我。

public static String getRelativeTimeSpanString(String time) {
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.ENGLISH);//change this format if you don't want to use this format
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+7"));//change this to the timezone you use
        Date datePast = sdf.parse(time);
        Date dateNow = new Date();
        //here you can change the language to match your language
        String sAgo = "yang lalu";
        String sMilli = "mili detik";
        String sSecond = "detik";
        String sMinute = "menit";
        String sHour = "jam";
        String sDay = "hari";
        String sWeek = "minggu";
        String sMonth = "bulan";
        String sYear = "tahun";

        long lDay = TimeUnit.MILLISECONDS.toDays(dateNow.getTime() - datePast.getTime());
        if (lDay == 0) {
            long lHour = TimeUnit.MILLISECONDS.toHours(dateNow.getTime() - datePast.getTime());
            if (lHour == 0) {
                long lMinute = TimeUnit.MILLISECONDS.toMinutes(dateNow.getTime() - datePast.getTime());
                if (lMinute == 0) {
                    long lSecond = TimeUnit.MILLISECONDS.toSeconds(dateNow.getTime() - datePast.getTime());
                    if (lSecond == 0) {
                        long lMilliSecond = TimeUnit.MILLISECONDS.toMillis(dateNow.getTime() - datePast.getTime());
                        if (lMilliSecond == 0) {
                            return time;
                        } else {
                            return lMilliSecond + " " + sMilli + " " + sAgo;
                        }
                    } else {
                        return lSecond + " " + sSecond + " " + sAgo;
                    }
                } else {
                    return lMinute + " " + sMinute + " " + sAgo;
                }
            } else {
                return lHour + " " + sHour + " " + sAgo;
            }
        } else {
            if (lDay < 7) {
                return lDay + " " + sDay + " " + sAgo;
            } else {
                long lWeek = lDay / 7;
                if (lWeek < 4) {
                    return lWeek + " " + sWeek + " " + sAgo;
                } else if (lWeek == 4) {
                    return 1 + sMonth + " " + sAgo;
                } else {
                    long lMonth = lDay / 30;
                    if (lMonth < 12) {
                        return lMonth + " " + sMonth + " " + sAgo;
                    } else if (lMonth == 12) {
                        return 1 + sYear + " " + sAgo;
                    } else {
                        long lYear = lDay / 365;
                        return lYear + " " + sYear + " " + sAgo;
                    }
                }
            }
        }
    } catch (Exception e) {
        return time;
    }
}

也可以使用此方法更改配置的区域设置

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void setDefaultLocale() {
    //call this method from application scope
    Configuration configuration = new Configuration(Resources.getSystem().getConfiguration());
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.locale = Locale.ENGLISH; // or whichever locale you desire
    } else {
        configuration.setLocale(Locale.ENGLISH);
    }
    Resources.getSystem().updateConfiguration(configuration, null);
}

但我不认为改变语言环境是一个好主意。