Reputation: 1959
I am fetching certain data from JSON. I have a date ("PaymentDueDateFormatted")
which I need to be compared with the current Date and display the data in listviews. But I can't establish this. This shows zero data.
I am getting the date as for eg: "22-Dec-2017"
. Is it to be converted into millies for compare?
ArrayList<String[]> invoiceListData1 = new ArrayList<>();
for(int i = 0; i<invoices.length();i++){
JSONObject jsonObject1 = invoices.getJSONObject(i);
String date1 = jsonObject1.getString("PaymentDueDateFormatted");
Calendar currentDate = Calendar.getInstance();
if (currentDate.after(date1)) {
String[] data = new String[9];
data[0] = jsonObject1.getString("ID");
data[1] = jsonObject1.getString("InvoiceNo");
switch (getIntent().getExtras().getInt(Common.CUSTOMER_OR_SUPPLIER)) {
case Common.CUSTOMER:
data[2] = jsonObject1.getJSONObject("customerObj").getString("ID");
data[3] = jsonObject1.getJSONObject("customerObj").getString("ContactPerson");
data[7] = jsonObject1.getJSONObject("companiesObj").getString("Name");
break;
case Common.SUPPLIER:
data[2] = jsonObject1.getJSONObject("suppliersObj").getString("ID");
data[3] = jsonObject1.getJSONObject("suppliersObj").getString("ContactPerson");
data[7] = jsonObject1.getJSONObject("companiesObj").getString("Name");
break;
}
data[4] = jsonObject1.getString("PaymentDueDateFormatted");
data[5] = jsonObject1.getString("BalanceDue");
data[6] = jsonObject1.getString("PaidAmount");
data[8] = jsonObject1.getString("DueDays");
invoiceListData1.add(data);
CustomAdapter adapter = new CustomAdapter(Invoices.this, invoiceListData1, (getIntent().getExtras().getInt(Common.CUSTOMER_OR_SUPPLIER) == Common.CUSTOMER ? Common.SALESLIST : Common.PURCHASELIST));
invoiceList.setAdapter(adapter);
(findViewById(R.id.list_card)).setVisibility(View.VISIBLE);
}
}
Upvotes: 0
Views: 74
Reputation: 9056
Try to print your currentdate
and you will understand why both dates are not equal.
don't convert into millies bcoz in current date millies are included time like hours, minutes and seconds
Calendar currentDate = Calendar.getInstance();
this will give you time in including everything(seconds, minutes, hours and more ) so it never be same as string you getting from the response
try this
Date date = new Date();
String todayDate = new SimpleDateFormat("dd-MMM-yyyy").format(date);
instead of this
Calendar currentDate = Calendar.getInstance();
and compare with your string date with it
Upvotes: 2
Reputation:
Convert both dates in milliseconds and compare them:
String string = "22-Dec-2017";
DateFormat format = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
Date date = null;
try {
date = format.parse(string);
} catch (ParseException e) {
e.printStackTrace();
}
if (date != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Long dateInMils = cal.getTimeInMillis();
Calendar calToday = Calendar.getInstance();
calToday.set(Calendar.HOUR_OF_DAY, 0);
calToday.set(Calendar.MINUTE, 0);
calToday.set(Calendar.SECOND, 0);
calToday.set(Calendar.MILLISECOND, 0);
Long todayInMils = cal.getTimeInMillis();
if (dateInMils < todayInMils) {
// your code here
}
}
Upvotes: 0
Reputation: 7669
Try to do this :
String date1 = jsonObject1.getString("PaymentDueDateFormatted");
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
cal.setTime(sdf.parse(date1));// Create calender variable from date and the compare with current time
Calendar currentDate = Calendar.getInstance();
if ( currentDate.after(cal)) {
}
Hope this will help.
Upvotes: 1
Reputation: 2940
try {
Date date = new SimpleDateFormat("dd-MMM-yyyy").parse(date1);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
date.before(cal.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
Upvotes: 1