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,553
  • 7
  • 38
  • 68

6 Answers6

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,761
  • 3
  • 40
  • 57
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,300
  • 1
  • 18
  • 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

Use this:

   public  String FormatDate(String dateString) {

    try {
        SimpleDateFormat sd = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss"");
        Date d = sd.parse(dateString);
        sd = new SimpleDateFormat("yyyy-MMM-dd");
        System.out.println(sd.format(d));
        return sd.format(d);
    } catch (Exception e) {
    }
    return "";
}
Piyush
  • 24,288
  • 6
  • 40
  • 72
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,071
  • 26
  • 167
  • 187
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. thanks dear Piyush Gupta.your guide support me to sort out this issue :-)

Priyan RockZ
  • 1,553
  • 7
  • 38
  • 68