我正在从JSON获取某些数据。我有一个日期("PaymentDueDateFormatted")
,需要将其与当前日期进行比较,并在列表视图中显示数据。但我无法确定这一点。这显示零数据。
我正在获取日期,例如:"22-Dec-2017"
。是否可以将其转换为米来进行比较?
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);
}
}
答案 0 :(得分:2)
尝试打印您的currentdate
,您将理解为什么两个日期不相等。
请不要在当前日期将bcoz转换为Millies 时间,例如小时,分钟和秒
Calendar currentDate = Calendar.getInstance();
这将使您有时间包括所有内容(秒,分钟,小时等),因此它永远不会与您从响应中获得的字符串相同
尝试
Date date = new Date();
String todayDate = new SimpleDateFormat("dd-MMM-yyyy").format(date);
代替此
Calendar currentDate = Calendar.getInstance();
并与您的字符串日期进行比较
答案 1 :(得分:1)
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();
}
答案 2 :(得分:1)
尝试执行此操作:
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)) {
}
希望这会有所帮助。
答案 3 :(得分:0)
将两个日期转换为毫秒并进行比较:
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
}
}