I tried my best to do research and look for the tutorial, but still confused about it. My problem is, I want to display something like "minutes ago" based on create date and time I fetched from json data.
When I fetched the date and time from server I want to display it in livestatTimeAgo textview, but it show error because getTimeAgo method is in long format. So I used parseLong to change the data I fetched, but still got error. Can somebody give me hint on how to display long data to string value like this.
This is the error log:
FATAL EXCEPTION: main
Process: com.baracode.eilsan, PID: 898
java.lang.NumberFormatException: For input string: "2017-08-25 09:20:54"
at java.lang.Long.parseLong(Long.java:443)
at java.lang.Long.parseLong(Long.java:485)
at com.baracode.eilsan.livestat.adapter.LivestatAdapter.onBindViewHolder(LivestatAdapter.java:49)
at com.baracode.eilsan.livestat.adapter.LivestatAdapter.onBindViewHolder(LivestatAdapter.java:29)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6400)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6433)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5377)
This is my code
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.baracode.eilsan.R;
import com.baracode.eilsan.model.LiveFeed;
import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
/**
* Created by kille on 17/7/2017.
*/
public class LivestatAdapter extends RecyclerView.Adapter<LivestatAdapter.MyViewHolder> {
private final ArrayList<LiveFeed> livestatList;
private LiveFeed liveFeed;
public LivestatAdapter(ArrayList<LiveFeed> livestatList) {
this.livestatList = livestatList;
}
@Override
public LivestatAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_livestat, parent, false);
return new LivestatAdapter.MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
LiveFeed livestatModel = livestatList.get(position);
holder.livestatTimeAgo.setText(getTimeAgo(Long.parseLong(livestatModel.createdAt)));
holder.livestatOrganisationName.setText(livestatModel.beneficiaryName);
holder.livestatMoneyDonated.setText(livestatModel.amount);
holder.livestatDonorName.setText(livestatModel.donorName);
}
@Override
public int getItemCount() {
return livestatList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView livestatTimeAgo, livestatDonorName, livestatOrganisationName, livestatMoneyDonated;
public MyViewHolder(View view) {
super(view);
livestatTimeAgo = view.findViewById(R.id.livestat_time_ago);
livestatOrganisationName = view.findViewById(R.id.livestat_organisation_name);
livestatMoneyDonated = view.findViewById(R.id.livestat_money_donated);
livestatDonorName = view.findViewById(R.id.livestat_donor_name);
}
}
// private String ConvertTime(String time) {
//
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// SimpleDateFormat format1 = new SimpleDateFormat("hh:mm aa MM-dd");
// java.util.Date date = null;
//
// try {
// date = format.parse(time);
// } catch (ParseException e) {
// e.printStackTrace();
// }
//
// String convertedDate = format1.format(date);
//
// return convertedDate;
// }
public static String getTimeAgo(long timestamp) {
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();//get your local time zone.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm ss");
sdf.setTimeZone(tz);//set time zone.
String localTime = sdf.format(new Date(timestamp * 1000));
Date date = new Date();
try {
date = sdf.parse(localTime);//get local date
} catch (ParseException e) {
e.printStackTrace();
}
if(date == null) {
return null;
}
long time = date.getTime();
Date curDate = currentDate();
long now = curDate.getTime();
if (time > now || time <= 0) {
return null;
}
int timeDIM = getTimeDistanceInMinutes(time);
String timeAgo = null;
if (timeDIM == 0) {
timeAgo = "less than a minute";
} else if (timeDIM == 1) {
return "1 minute";
} else if (timeDIM >= 2 && timeDIM <= 44) {
timeAgo = timeDIM + " minutes";
} else if (timeDIM >= 45 && timeDIM <= 89) {
timeAgo = "about an hour";
} else if (timeDIM >= 90 && timeDIM <= 1439) {
timeAgo = "about " + (Math.round(timeDIM / 60)) + " hours";
} else if (timeDIM >= 1440 && timeDIM <= 2519) {
timeAgo = "1 day";
} else if (timeDIM >= 2520 && timeDIM <= 43199) {
timeAgo = (Math.round(timeDIM / 1440)) + " days";
} else if (timeDIM >= 43200 && timeDIM <= 86399) {
timeAgo = "about a month";
} else if (timeDIM >= 86400 && timeDIM <= 525599) {
timeAgo = (Math.round(timeDIM / 43200)) + " months";
} else if (timeDIM >= 525600 && timeDIM <= 655199) {
timeAgo = "about a year";
} else if (timeDIM >= 655200 && timeDIM <= 914399) {
timeAgo = "over a year";
} else if (timeDIM >= 914400 && timeDIM <= 1051199) {
timeAgo = "almost 2 years";
} else {
timeAgo = "about " + (Math.round(timeDIM / 525600)) + " years";
}
return timeAgo + " ago";
}
public static Date currentDate() {
Calendar calendar = Calendar.getInstance();
return calendar.getTime();
}
private static int getTimeDistanceInMinutes(long time) {
long timeDistance = currentDate().getTime() - time;
return Math.round((Math.abs(timeDistance) / 1000) / 60);
}
}
here my livefeed class..
public class LiveFeed {
@SerializedName("id")
public String id = "";
@SerializedName("ref_id")
public String refId = "";
@SerializedName("beneficiary_type")
public String beneficiaryType = "";
@SerializedName("beneficiary_name")
public String beneficiaryName = "";
@SerializedName("donor_name")
public String donorName = "";
@SerializedName("amount")
public String amount = "";
@SerializedName("description")
public String description = "";
@SerializedName("remark")
public String remark = "";
@SerializedName("status")
public String status = "";
@SerializedName("created_at")
public String createdAt = "";
@SerializedName("updated_at")
public String updatedAt = "";
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRefId() {
return refId;
}
public void setRefId(String refId) {
this.refId = refId;
}
public String getBeneficiaryType() {
return beneficiaryType;
}
public void setBeneficiaryType(String beneficiaryType) {
this.beneficiaryType = beneficiaryType;
}
public String getBeneficiaryName() {
return beneficiaryName;
}
public void setBeneficiaryName(String beneficiaryName) {
this.beneficiaryName = beneficiaryName;
}
public String getDonorName() {
return donorName;
}
public void setDonorName(String donorName) {
this.donorName = donorName;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
}
So this is the correct answer for my question..updated code on how to get the time..for anyone who want to refer !!! thank you
DateFormat getDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String fromDate = getDate.format(Calendar.getInstance().getTime());
String endDate = livestatModel.updatedAt; //here my jsondataName
long diff = 0;
try {
//Convert to Date
Date startDate = getDate.parse(fromDate);
Calendar c1 = Calendar.getInstance();
//Change to Calendar Date
c1.setTime(startDate);
//Convert to Date
Date endDate2 = getDate.parse(endDate);
Calendar c2 = Calendar.getInstance();
//Change to Calendar Date
c2.setTime(endDate2);
//get Time in milli seconds
long ms1 = c1.getTimeInMillis();
long ms2 = c2.getTimeInMillis();
//get difference in milli seconds
diff = ms1 - ms2;
if (diff < 0) {
holder.livestatTimeAgo.setText(" 0 day " + " 0 hour " + " 0 minute " + " 0 sec " + "left");
} else {
int diffInDays = (int) (diff / (1000 * 60 * 60 * 24));
int diffInHours = (int) ((diff / (1000 * 60 * 60)) % 24);
int diffInMinutes = (int) ((diff % (1000 * 60 * 60)) / (1000 * 60));
int diffInSec = (int) ((diff / 1000) % 60);
holder.livestatTimeAgo.setText(diffInDays + " days " + diffInHours + " hours " + diffInMinutes + " minutes " + diffInSec + " secs " + "Ago");
}
}catch (Exception e){}