2

This is my code:

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

And the catch is being called so something went wrong

Log :

09-24 12:21:34.931  19014-19014/? V/dateformat111﹕ did not found date

Exception :

java.text.ParseException: Unparseable date: "2015-08-28"

Not a duplicate of How to parse a date? the string I'm parsing is the same format of SimpleDateFormat "yyyy-MM-dd" ,"2015-08-28"

I found out what is causing the exception with English (ltr) as phone language my code works but when change it to Arabic (rtl) the exception is thrown, why is that?

Community
  • 1
  • 1
Ahmed na
  • 1,084
  • 2
  • 13
  • 34

2 Answers2

3

Your Problem is due to invalid date in locale. try changing your locale as below.

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd",Locale.US); 

It will work.

karan
  • 8,637
  • 3
  • 41
  • 78
  • i have the same format of SimpleDateFormat and the string i'm trying to parse : "yyyy-MM-dd" ,"2015-08-28" – Ahmed na Sep 24 '15 at 10:39
  • comment 2nd line in try block and make 3rd line as below `Log.v("dateformat111","is "+format.parse(exp_date));` – karan Sep 24 '15 at 10:44
  • that question was trying to store date in a certain format ,i'm just trying to use it ,tried your cod and same exception , i found what is causing the exception ,my code is working with phone English language (ltr),but when change the phone language to Arabic (rtl) exception get thrown the date format in arabic is different than "yyyy-MM-dd" ?? – Ahmed na Sep 24 '15 at 11:25
  • Whatever the locale returned by Locale.getDefault() is doesn't support dates formatted from arabic locale. Changing it to Locale.US for example, will work. – karan Sep 24 '15 at 11:44
  • i made it SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd",Locale.US); and worked ! ,thanx ,edit your answer for easy access if you like – Ahmed na Sep 24 '15 at 14:00
0

try this,

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>

this is working for me.

Ganpat Kaliya
  • 888
  • 2
  • 9
  • 16