**Class Utils**
public static String getTimeAgo(String time) {
String time_ago = "";
try {
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date past = format.parse(time);
Date now = new Date();
long seconds = TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime());
long minutes = TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime());
long hours = TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime());
long days = TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime());
if (seconds < 60) {
time_ago = seconds + " seconds ago";
} else if (minutes < 60) {
time_ago = seconds + " minutes ago";
} else if (hours < 24) {
time_ago = hours + " hours ago";
} else {
time_ago = days + " days ago";
}
} catch (Exception e) {
e.printStackTrace();
}
return time_ago;
}
**Model**
public class History{
.....
String mTime;
public String getTime() {
return Utils.getTimeAgo(mTime);
}
public void setTime(String mTime) {
this.mTime = mTime;
}
适配器
中的 getView@Override
public View getView(final int position, View view, final ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item_history, null);
holder = new ViewHolder();
holder.mTvTitle = (TextView) view.findViewById(R.id.text_title);
holder.mTvMediaName = (TextView) view.findViewById(R.id.text_mediaName);
holder.mTvTime = (TextView) view.findViewById(R.id.text_time);
holder.mImageUrl = (ImageView) view.findViewById(R.id.image_url);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
final History history = mListHistory.get(position);
holder.mTvTitle.setText(history.getTitle());
holder.mTvMediaName.setText(history.getMediaName());
holder.mTvTime.setText(history.getTime());
holder.mImageUrl.setImageResource(android.R.color.transparent);
Picasso.with(mContext).load(history.getImageUrl())
//.fit().centerInside()
.into(holder.mImageUrl);
return view;
}
我设置String time = "2016-01-24T16:00:00.000Z";
holder.mTvTime.setText(history.getTime());
不显示值
这是错误:
java.text.ParseException:Unparseable date:&#34; 43天前&#34; (在偏移2处)
请。救救我!