区域设置为阿拉伯语时,Android中的日期格式问题 [英] Date format issue in android when locale is arabic
问题描述
我在这里遇到了一个严重的问题,我正在构建一个可以在阿拉伯语设备上运行的应用程序,我需要将日期发送到服务器,我正在使用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)
设置年,月,月,日,小时,分钟和秒字段.其他领域不 改变如果不需要,请先致电清除.月的值是从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屋!