Android Can';不能比较日期

Android Can';不能比较日期,android,Android,我正在从JSON获取某些数据。我有一个日期(“PaymentDueDateFormatted”),我需要将其与当前日期进行比较,并在列表视图中显示数据。但我无法证实这一点。这显示零数据 我正在获取日期,例如:“2017年12月22日”。是否要将其转换为毫秒进行比较 ArrayList<String[]> invoiceListData1 = new ArrayList<>(); for(int i = 0; i<invoices.length();i++){

我正在从JSON获取某些数据。我有一个日期
(“PaymentDueDateFormatted”)
,我需要将其与当前日期进行比较,并在列表视图中显示数据。但我无法证实这一点。这显示零数据

我正在获取日期,例如:
“2017年12月22日”
。是否要将其转换为毫秒进行比较

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);
    }
}
ArrayList invoiceListData1=new ArrayList();

对于(int i=0;i尝试打印您的
currentdate
,您将理解为什么两个日期不相等

不在当前日期转换为百万bcoz,包括百万 时间像小时、分钟和秒

这将为您提供时间来包括所有内容(秒、分钟、小时和更多),因此它永远不会与您从响应中获得的字符串相同

试试这个

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();
    }
不在当前日期转换为百万bcoz,包括百万 时间像小时、分钟和秒

这将为您提供时间来包括所有内容(秒、分钟、小时和更多),因此它永远不会与您从响应中获得的字符串相同

试试这个

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 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
    }
}
String String=“2017年12月22日”;
DateFormat格式=新的SimpleDataFormat(“dd-MMM-yyyy”,Locale.US);
日期=空;
试一试{
date=format.parse(字符串);
}捕获(解析异常){
e、 printStackTrace();
}
如果(日期!=null){
Calendar cal=Calendar.getInstance();
校准设定时间(日期);
Long-dateInMils=cal.gettimeinmilis();
Calendar calToday=Calendar.getInstance();
calToday.set(Calendar.HOUR\u OF_DAY,0);
calToday.set(Calendar.MINUTE,0);
calToday.set(日历秒,0);
calToday.set(日历毫秒,0);
Long todayInMils=cal.gettimeinmilis();
如果(日期英里<今天英里){
//你的代码在这里
}
}

以毫秒为单位转换两个日期并比较它们:

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
    }
}
String String=“2017年12月22日”;
DateFormat格式=新的SimpleDataFormat(“dd-MMM-yyyy”,Locale.US);
日期=空;
试一试{
date=format.parse(字符串);
}捕获(解析异常){
e、 printStackTrace();
}
如果(日期!=null){
Calendar cal=Calendar.getInstance();
校准设定时间(日期);
Long-dateInMils=cal.gettimeinmilis();
Calendar calToday=Calendar.getInstance();
calToday.set(Calendar.HOUR\u OF_DAY,0);
calToday.set(Calendar.MINUTE,0);
calToday.set(日历秒,0);
calToday.set(日历毫秒,0);
Long todayInMils=cal.gettimeinmilis();
如果(日期英里<今天英里){
//你的代码在这里
}
}

Bro我的字符串日期是2017年12月22日的th格式,而不是2017年12月22日。这会有问题吗?请使用MMM而不是MMBro。您能告诉我如何比较它吗?现在我正在做“如果(今天日期。之后(日期1))”。但它显示了错误您的比较方法在哪里?if语句Bro my string date是2017年12月22日的格式,而不是2017年12月22日的格式。这会有问题吗?请使用MMM而不是MMBro。您能告诉我如何比较它吗?现在我正在做“if(todayDate.after(date1))。但它显示了错误您的比较方法在哪里?if语句