Java 将字符串yyyy-MM-dd转换为日期格式,使用从右到左的电话语言(阿拉伯语)

Java 将字符串yyyy-MM-dd转换为日期格式,使用从右到左的电话语言(阿拉伯语),java,android,date,Java,Android,Date,这是我的代码: SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String exp_date="2015-08-28"; try { Log.v("dateformat111","is "+exp_date); Date exp_dateFormated = format.parse(exp_date); Log.v("dateformat111","is "+exp_dateFormated)

这是我的代码:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String exp_date="2015-08-28";
try {
    Log.v("dateformat111","is "+exp_date);
    Date exp_dateFormated = format.parse(exp_date);
    Log.v("dateformat111","is "+exp_dateFormated);       
} catch (ParseException e) {
    Log.v("dateformat111","did not found date");
    e.printStackTrace();
}
而且这个陷阱被称为“陷阱”,所以出了问题

日志:

例外情况:

java.text.ParseException: Unparseable date: "2015-08-28"
不是复制品 我正在解析的字符串与SimpleDataFormat的格式相同 “yyyy-MM-dd”、“2015-08-28”

我发现了导致异常的原因 以英语(ltr)作为电话语言,我的代码可以正常工作
但将其更改为阿拉伯语(rtl)时会引发异常,这是为什么?

您的问题是由于区域设置中的日期无效。尝试更改您的区域设置,如下所示

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd",Locale.US); 
它会起作用的。

试试这个

public static String formatTime(Date time, Locale locale){
    String timeFormat = UserSettingManager
                           .getUserSetting(UserSettingManager.PREF_TIME_FORMAT);

    if(StringUtils.isEmptyOrWhitespace(timeFormat)){
        timeFormat = DEFAULT_TIME_FORMAT;
    }

    SimpleDateFormat formatter;

    try {
        formatter = new SimpleDateFormat(timeFormat, locale);            
    } catch(Exception e) {
        formatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT, locale);
    }
    return formatter.format(time);
}
Main.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.text.format.DateFormat;
import android.util.Log;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

@SuppressWarnings("unused")
@SuppressLint("SimpleDateFormat")
public class DateFormate extends Activity {

    Button btnDate;
    TextView txtDate;

    String date="2015-9-25";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_formate);

        btnDate=(Button)findViewById(R.id.btnDate);
        txtDate=(TextView)findViewById(R.id.textView1);

        btnDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
                Date d;
                try {
                    d = df.parse(date);
                    df=new SimpleDateFormat("yyyy-MM-dd");
                    String myDate = df.format(d);
                   Log.i("Date",myDate);
                   txtDate.setText(""+myDate);
                } catch (ParseException e) {}
            }
        });
    }
}
main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.dateformate.DateFormate" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/btnDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="23dp"
        android:text="DateFormate" />

</RelativeLayout>

这对我有用。

试试这个

public static String formatTime(Date time, Locale locale){
    String timeFormat = UserSettingManager
                           .getUserSetting(UserSettingManager.PREF_TIME_FORMAT);

    if(StringUtils.isEmptyOrWhitespace(timeFormat)){
        timeFormat = DEFAULT_TIME_FORMAT;
    }

    SimpleDateFormat formatter;

    try {
        formatter = new SimpleDateFormat(timeFormat, locale);            
    } catch(Exception e) {
        formatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT, locale);
    }
    return formatter.format(time);
}
这样叫,

Log.e("CHINESE DATE", formatTime(new Date(), Locale.CHINESE));

代码没有抛出任何异常。您的代码很好,其他地方一定有问题。您遇到了什么异常?你能发布stacktrace吗?很可能是区域设置issue@Ahmedna-这是你显示的日志信息。堆栈跟踪是什么?在那之后?在df.parse(date),java.text.ParseException:Unparseable datei update question,您的代码与电话的英语语言完美配合,但与阿拉伯语(rtl)相同的异常我有相同的SimpleDataFormat格式和我试图解析的字符串:“yyyy-MM-dd”,“2015-08-28”注释try块中的第二行,并将第三行变成如下
Log.v(“dateformat111”,“is”+format.parse(exp_date))
那个问题是试图以某种格式存储日期,我只是尝试使用它,尝试了您的cod和相同的异常,我找到了导致异常的原因,我的代码使用电话英语(ltr),但当将电话语言更改为阿拉伯语(rtl)时抛出异常,阿拉伯语的日期格式与“yyyy-MM-dd”不同??无论locale.getDefault()返回的语言环境是什么,都不支持使用阿拉伯语语言环境格式化的日期。例如,将其更改为Locale.US将起作用;而且成功了,thanx,如果您愿意,请编辑您的答案以便于访问