Date format issue in android when locale is arabic

╄→гoц情女王★ 提交于 2020-01-14 14:58:49

问题


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"));

but non of them worked for me

any help please.

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));
    }
}

回答1:


You can use:

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

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()));
 }

Also see this:

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).



来源:https://stackoverflow.com/questions/27469112/date-format-issue-in-android-when-locale-is-arabic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!