Hello Guys This problem is driving me crazy right now. I have a string in my database called dueDate
with value Jan 18, 2018
. I retrieved it and save it in a string in my recyclerAdapter with String dueDate = transactionTasks.get(position).get_transactiontaskpaydet().toString();
Now i wanna compare it with the current date and if the due date is after my current date it should display the dueDate with red color.
bellow are my coded.
String dueDate = transactionTasks.get(position).get_transactiontaskpaydet().toString();
Date cdate = new Date();
Date ddate = new Date(dueDate);
if(cdate.after(ddate)){
holder.date.setTextColor(Color.RED);
}
This codes work perfectly but the problem is Date(dueDate);
is deprecated. And when i use another method that uses a try
and catch
, i don't get any result.
Below are the codes
SimpleDateFormat format = new SimpleDateFormat("E, MMM dd yyyy");
Date curDate = new Date();
try {
Date datedue = format.parse(dueDate);
if(curDate.after(datedue)) {
holder.date.setTextColor(Color.RED);
}
} catch (ParseException e) {
e.printStackTrace();
}
And if I try creating the Date
outside the try
block by using Date duedate = null;
I get an error because its not getting the values inside the try
block. this is driving me crazy because I shouldn't use deprecated code even if it works perfectly.
All the answers I found didn't work for me. I just need to be able to convert a string to a date so I can compare it with the current date. Thanks Guys