繁体   English   中英

无法比较日期

[英]Can't compare date

我正在从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);
    }
}

尝试打印您currentdate ,你就会明白为什么这两个日期是不相等的。

不要在当前日期转换为毫厘bcoz毫厘包括的时间,例如小时,分钟和秒

 Calendar currentDate = Calendar.getInstance();

这将使您有时间包含所有内容(秒,分钟,小时等),因此它与您从响应中获得的字符串永远不会相同

尝试这个

Date date = new Date();
String todayDate = new SimpleDateFormat("dd-MMM-yyyy").format(date);

代替这个

 Calendar currentDate = Calendar.getInstance();

并与您的字符串日期进行比较

    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();
    }

尝试这样做:

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)) {
  }

希望这会有所帮助。

转换两个日期(以毫秒为单位)并进行比较:

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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM