0

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

Priyan RockZ
  • 1,605
  • 7
  • 40
  • 68

5 Answers5

1

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);
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
1

I suppose that this line

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

needs to be like this.

tvDate.setText(dateFormat.format(salesReportItems.getDate()));
Yuichi Araki
  • 3,438
  • 1
  • 19
  • 24
1
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);

1

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?

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
0
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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Priyan RockZ
  • 1,605
  • 7
  • 40
  • 68