Use the instance of SimpleDateFormat mDateFormat
to provide the constructor of SpannableString
with a String
representation of the date found in mDate
SpannableString content = new SpannableString(mDateFormat.format(mDate));
Frage
I am new to android development and I have a question concerning parsed dates from feeds. I was wondering, is it possible to use both SimpleDateFormat and SpannableString together? I am running into "type" issues. Below is a snippet of my code and what I am trying to do.
public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();
LayoutInflater inflater = activity.getLayoutInflater();
View rowView = inflater.inflate(R.layout.activity_main_format, null);
TextView dateTextView = (TextView) rowView.findViewById(R.id.date_format);
try {
if (imageText.get(position).getPubDate() != null) {
Date mDate = new Date(imageText.get(position).getPubDate());
SimpleDateFormat mDateFormat = new SimpleDateFormat("MM/dd/yy hh:mm a");
Log.d(TAG, mDateFormat.format(mDate));
SpannableString content = new SpannableString(mDate);
content.setSpan(new UnderlineSpan(), 0, 25, 0);
dateTextView.setText(content);
} else {
dateTextView.setText(imageText.get(position).getPubDate());
}
} catch (MalformedURLException e) { Log.d(TAG, "MalformedURLException");
} catch (IOException e) { Log.d(TAG, "IOException");
}
return rowView;
The issue is with SpannableString content = new SpannableString(mDate); I get an error that attempts to push me towards changing the type of mDate to type String, however, if I do so then the parsing does not work. Can anyone advise how I can solve this please?
Note: Changing SpannableString content = new SpannableString(mDate); to SpannableString content = new SpannableString(mDateFormat.format(mDate)); crashes the application. And the other way is just wrong (leaves an error).
Lösung
Use the instance of SimpleDateFormat mDateFormat
to provide the constructor of SpannableString
with a String
representation of the date found in mDate
SpannableString content = new SpannableString(mDateFormat.format(mDate));