Priyan RockZ
Priyan RockZ

Reputation: 1615

java.lang.IllegalArgumentException in Android with DateFormat

Here is my code fragment

here date in 10-Sep-2013 09:53:37 format

TextView tvDate = (TextView) convertView.findViewById(R.id.entered_date);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
tvDate.setText(dateFormat.format(salesReportItems.getDate().toString()));
TextView tvCardType = (TextView) convertView.findViewById(R.id.card_type);
tvCardType.setText(salesReportItems.getCardType().toString());

Please help me to sort out this issue.here is my error. enter image description here

Dear Piyush,

Here is out put when i used your code

enter image description here

Upvotes: 0

Views: 3130

Answers (5)

Priyan RockZ
Priyan RockZ

Reputation: 1615

try {
SimpleDateFormat sd = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
Date d = sd.parse(salesReportItems.getDate().toString());
sd = new SimpleDateFormat("yyyy-MM-dd");
TextView tvDate = (TextView) convertView.findViewById(R.id.entered_date);
tvDate.setText(sd.format(d));
} catch (Exception e) {
    e.printStackTrace();
}

Issue sorted with above code.

Upvotes: 0

user1831682
user1831682

Reputation:

TextView tvDate = (TextView) convertView.findViewById(R.id.entered_date);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

Remove your third line , get the date as string,

String date=salesReportItems.getDate().toString();

use System.out.println(date); to date get displayed in Logcat;

from the observed date form string in pattern like this;

sring str="1990-08-27";

then use,

tvDate.setText(dateFormat.format(str));

instead of dateFormat.format use dateFormat.parse(str);

Upvotes: 1

Pankaj Kumar
Pankaj Kumar

Reputation: 82958

Create a method like below

private String formatDate(String dateString) {
        try {
            SimpleDateFormat sd = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss" /* 10-Sep-2013 09:53:37*/);
            Date d = sd.parse(dateString);
            sd = new SimpleDateFormat("yyyy-MM-dd");
            return sd.format(d);
        } catch (ParseException e) {
        }
        return "";
    }

And then call it as

tvDate.setText(formatDate(salesReportItems.getDate().toString()));

Read more about How can I change the date format in Java?

Upvotes: 1

Yuichi Araki
Yuichi Araki

Reputation: 3458

I suppose that this line

tvDate.setText(dateFormat.format(salesReportItems.getDate().toString()));

needs to be like this.

tvDate.setText(dateFormat.format(salesReportItems.getDate()));

Upvotes: 1

kalyan pvs
kalyan pvs

Reputation: 14590

Try to use your code like this..

if salesReportItems is Date type object then..

String timeStamp = new SimpleDateFormat("yyyy-MM-dd")
                    .format(salesReportItems);
tvDate.setText(timeStamp);

Upvotes: 1

Related Questions