当语言环境为阿拉伯语时,android 中的日期格式问题 [英] Date format issue in android when locale is arabic

查看:55
本文介绍了当语言环境为阿拉伯语时,android 中的日期格式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个严重的问题,我正在构建一个可以在阿拉伯设备上运行的应用程序,我需要将日期发送到服务器,我使用 Android DatePickerDialog 来获取日期,但日期总是用阿拉伯字符发送,当我尝试再次显示它时,它给了我不可解析的日期异常

I have a serious problem here, I am building an app that will work on Arabic devices, and I need to send dates to the server, I am using Android DatePickerDialog to get the date, but the date always sent with Arabic characters, and when i try to display it again it gives me Unparsable date exception

我尝试了以下解决方案,但没有结果

I have tried the following solutions but no results

  • mDateTime = Calendar.getInstance(Locale.US).getTime();
  • mDateFormater.setTimeZone(TimeZone.getTimeZone("GMT"));

但没有一个为我工作

请提供任何帮助.

我的日期选择器对话框代码如下

My date picker dialog code is like following

    public static class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    private TextView mUserView;
    private Date mAffectedDate;
    private SimpleDateFormat mDateFormater;
    private Date mInitialDate;

    public TextView getUserView() {
        return mUserView;
    }

    public void setUserView(TextView userView) {
        this.mUserView = userView;
    }

    public Date getAffectedDate() {
        return mAffectedDate;
    }

    public void setAffectedDate(Date mAffectedDate) {
        this.mAffectedDate = mAffectedDate;
    }

    public SimpleDateFormat getDateFormater() {
        return mDateFormater;
    }

    public void setDateFormater(SimpleDateFormat mDateFormater) {
        this.mDateFormater = mDateFormater;
    }

    public Date getInitialDate() {
        return mInitialDate;
    }


    public void setInitialDate(Date mInitialDate) {
        this.mInitialDate = mInitialDate;
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance(Locale.US);
        c.setTime(mInitialDate);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {

        Date date = new Date();
        date.setYear(year - 1900);
        date.setMonth(month);
        date.setDate(day);

        mAffectedDate.setYear(year - 1900);
        mAffectedDate.setMonth(month);
        mAffectedDate.setDate(day);
        mUserView.setText(mDateFormater.format(date));
    }
}

推荐答案

您可以使用:

Calendar.set(int year, int month, int day, int hourOfDay, int minute, int second)

设置年、月、日、小时、分钟和秒字段.其他领域没有改变了;如果不需要,请先调用 clear.月份值是从 0 开始的,因此使用像 JANUARY 这样的常量可能会更清楚.像这样:

Sets the year, month, day of the month, hour of day, minute, and second fields. Other fields are not changed; call clear first if this is not desired. The month value is 0-based, so it may be clearer to use a constant like JANUARY. Like this:

Calendar cal = Calendar.getInstance(Locale.US);
 public void onDateSet(DatePicker view, int year, int month, int day) {
  cal.set(year, month, day, 0, 0, 0);
  // get time in milliseconds
  Long timeInmilliseconds = cal.getTimeInMillis();
  // print time
  Log.v("log", "Date "+ new Date(cal.getTimeInMillis()));
 }

另见这个:

一个常见的错误是在生成机器可读的输出时隐式使用默认语言环境.这往往适用于开发人员测试设备(特别是因为有这么多开发人员使用 en_US),但在用户处于更复杂区域设置的设备上运行时会失败.例如,如果您要格式化整数,某些语言环境将使用非 ASCII 十进制数字.再举一个例子,如果您要格式化浮点数,某些语言环境将使用,"作为小数点,使用."作为小数点.用于数字分组.这对于人类可读的输出是正确的,但如果呈现给另一台计算机可能会导致问题(例如 parseDouble(String) 无法解析这样的数字).

A common mistake is to implicitly use the default locale when producing output meant to be machine-readable. This tends to work on the developers test devices (especially because so many developers use en_US), but fails when run on a device whose user is in a more complex locale. For example, if you are formatting integers some locales will use non-ASCII decimal digits. As another example, if you are formatting floating-point numbers some locales will use ',' as the decimal point and '.' for digit grouping. That is correct for human-readable output, but likely to cause problems if presented to another computer (parseDouble(String) cannnot parse such a number, for example).

这篇关于当语言环境为阿拉伯语时,android 中的日期格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆