Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中查找json日期是否为一个月的列表_Java_Android - Fatal编程技术网

Java 如何在android中查找json日期是否为一个月的列表

Java 如何在android中查找json日期是否为一个月的列表,java,android,Java,Android,对不起,我的英语不好,我有来自json的日期列表 { "data": [ { "lead_id": "1763", "name": "Gaurav Kumar", "date": "16-02-2020", "time": "10:00 To 11:00 AM", }, { "lead_id": "1759",

对不起,我的英语不好,我有来自json的日期列表

{
    "data": [
        {
            "lead_id": "1763",
            "name": "Gaurav Kumar",
            "date": "16-02-2020",
            "time": "10:00 To 11:00 AM",
        },
        {
            "lead_id": "1759",
            "name": "Test",
            "date": "04-02-2020",
            "time": "10:00 To 11:00 AM",
        },
        {
            "lead_id": "1751",
            "name": "kavita sharma",
            "date": "08-02-2020",
            "time": "10:00 To 11:00 AM", 
        },
        {
            "lead_id": "1751",
            "name": "kavita sharma",
            "date": "09-02-2020",
            "time": "10:00 To 11:00 AM",
        }
    ]
}
下面的代码帮助我找到当前日期

Calendar calendar = Calendar.getInstance();
        Date today = calendar.getTime();

        @SuppressLint("SimpleDateFormat")
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

        todayAsString = dateFormat.format(today);

        System.out.println(todayAsString);
但我想知道我如何检查日期列表是否为同一个月

//这是我用来检查当前日期的代码,但我想检查所有日期是否都是同一个月。请告诉我怎么做

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://delhidailyservice.com/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        APIService request = retrofit.create(APIService.class);
        Call<LeadData> call = request.leadData(prefConfig.readLoginId());
        call.enqueue(new Callback<LeadData>() {
            @Override
            public void onResponse(Call<LeadData> call, Response<LeadData> response) {

               /* new Handler().postDelayed(new Runnable() {
                    public void run() {
                        // if (isAdded()) {
                        pDialog.dismiss();
                        // }
                    }
                }, 5000);*/

                pDialog.dismiss();

                LeadData allEvent = response.body();
                allEventData = (List<Leads>) allEvent.getData();
//                Log.d("Error", ""+allEventData.size());

                allEventDatanew.clear();
                for (int i = 0; i < allEventData.size(); i++) {

                    if (todayAsString.equalsIgnoreCase(allEventData.get(i).getDate())) {
                        Leads allevent = new Leads();


                        String service = allEventData.get(i).getService();
                        String date = allEventData.get(i).getDate();
                        String name = allEventData.get(i).getName();
                        String time = allEventData.get(i).getTime();
                        String city = allEventData.get(i).getCity();
                        String status = allEventData.get(i).getStatus();
                        String credit = allEventData.get(i).getCredit();
                        String address = allEventData.get(i).getAddress();
                        String id = allEventData.get(i).getLeadId();

                        try {
                            //String details = allEventData.get(i).getDetail();

                            //abcd = Html.fromHtml(details).toString();

                            // tv_detail.setText(abcd);


                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                        allevent.setService(service);
                        allevent.setDate(date);
                        allevent.setName(name);
                        allevent.setTime(time);
                        allevent.setCity(address + " , " + city);
                        allevent.setStatus(status);
                        allevent.setCredit(credit);
                        allevent.setLeadId(id);


                        allEventDatanew.add(allevent);


                    }

                }
                // Log.d("Error1", ""+allEventDatanew.size());

                individualDataAdapter = new LeadAdapter(allEventDatanew, getContext());

                recyclerViewIndividualEvent.setAdapter(individualDataAdapter);
                individualDataAdapter.notifyDataSetChanged();
reformation-reformation=new-reformation.Builder()
.baseUrl(“https://delhidailyservice.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService请求=改装.create(APIService.class);
Call Call=request.leadData(prefConfig.readLoginId());
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
/*new Handler().postDelayed(new Runnable()){
公开募捐{
//if(isAdded()){
pDialog.disclose();
// }
}
}, 5000);*/
pDialog.disclose();
leaddataallevent=response.body();
allEventData=(List)allEvent.getData();
//Log.d(“错误”,“allEventData.size());
alleventdata.clear();
对于(int i=0;i
您可以使用此功能

private void checkDate() {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM");
            Date date = new Date();
            String currentMonth = dateFormat.format(date);
            Log.d("Current Month", dateFormat.format(date));


            JSONObject jsonObject = new JSONObject(jsonData);

            JSONArray jsonArray = jsonObject.getJSONArray("data");

            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject innerJsonObject = jsonArray.getJSONObject(i);
                String jsonDate = innerJsonObject.getString("date");
                SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
                Date modifiedDate = format.parse(jsonDate);
                String monthString = (String) DateFormat.format("MM", modifiedDate);


                if (TextUtils.equals(monthString, currentMonth)) {
                    Log.d(TAG, "Match " + jsonDate);
                    //TODO:
                } else {
                    Log.d(TAG, "Mismatch " + jsonDate);
                    //TODO:
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
private boolean checkAllDates(String jsonData) throws Exception {

        SimpleDateFormat dateFormat = new SimpleDateFormat("MM");
        Date date = new Date();
        String currentMonth = dateFormat.format(date);

        JSONObject jsonObject = new JSONObject(jsonData);
        JSONArray jsonArray = jsonObject.getJSONArray("data");

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject innerJsonObject = jsonArray.getJSONObject(i);
            String jsonDate = innerJsonObject.getString("date");
            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
            Date modifiedDate = format.parse(jsonDate);
            String monthString = (String) DateFormat.format("MM", modifiedDate);
            if (!TextUtils.equals(monthString, currentMonth)) return false;
        }

        return true;

    }

你可以使用这个功能

private void checkDate() {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM");
            Date date = new Date();
            String currentMonth = dateFormat.format(date);
            Log.d("Current Month", dateFormat.format(date));


            JSONObject jsonObject = new JSONObject(jsonData);

            JSONArray jsonArray = jsonObject.getJSONArray("data");

            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject innerJsonObject = jsonArray.getJSONObject(i);
                String jsonDate = innerJsonObject.getString("date");
                SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
                Date modifiedDate = format.parse(jsonDate);
                String monthString = (String) DateFormat.format("MM", modifiedDate);


                if (TextUtils.equals(monthString, currentMonth)) {
                    Log.d(TAG, "Match " + jsonDate);
                    //TODO:
                } else {
                    Log.d(TAG, "Mismatch " + jsonDate);
                    //TODO:
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
private boolean checkAllDates(String jsonData) throws Exception {

        SimpleDateFormat dateFormat = new SimpleDateFormat("MM");
        Date date = new Date();
        String currentMonth = dateFormat.format(date);

        JSONObject jsonObject = new JSONObject(jsonData);
        JSONArray jsonArray = jsonObject.getJSONArray("data");

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject innerJsonObject = jsonArray.getJSONObject(i);
            String jsonDate = innerJsonObject.getString("date");
            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
            Date modifiedDate = format.parse(jsonDate);
            String monthString = (String) DateFormat.format("MM", modifiedDate);
            if (!TextUtils.equals(monthString, currentMonth)) return false;
        }

        return true;

    }
java.time和ThreeTenABP
DateTimeFormatter dateFormatter=DateTimeFormatter.of模式(“dd-MM-uuu”);
列出allEventData=Arrays.asList(新线索(“16-02-2020”),
新潜在客户(“04-02-2020”)、新潜在客户(“08-02-2020”),
新线索(“09-02-2020”);
if(allEventData.isEmpty()){
System.out.println(“无数据”);
}否则{
YearMonth=YearMonth.parse(allEventData.get(0.getDate(),dateFormatter);
布尔值otherMonthFound=false;
用于(潜在客户:allEventData){
如果(!YearMonth.parse(lead.getDate(),dateFormatter).equals(month)){
otherMonthFound=真;
打破
}
}
如果(其他月份){
System.out.println(“它们不都在同一个月”);
}否则{
System.out.println(“它们都在同一个月”+月份);
}
}
此代码段的输出为:

它们都在2020-02年的同一个月

我省略了我使用的
Leads
类中的其他字段,因为它们对解决方案没有影响

问:java.time不需要Android API级别26吗? java.time在较旧和较新的Android设备上都能很好地工作。它至少需要Java6

  • 在Java8和更高版本以及更新的Android设备上(API级别26),现代API是内置的
  • 在非androidjava6和7中,获取三个后端口,即现代类的后端口(三个十用于jsr310;请参见底部的链接)
  • 在(较旧的)Android上,使用Android版的ThreeTen Backport。它被称为ThreeTenABP。并确保您使用子包从
    org.ThreeTen.bp
    导入日期和时间类
链接
  • 解释如何使用java.time
  • ,其中首先描述了
    java.time
  • ,java.time的后端口到Java6和Java7(JSR-310为三十)
  • ,Android版Three Ten Backport
  • ,解释得非常透彻
java.time和ThreeTenABP
DateTimeFormatter dateFormatter=DateTimeFormatter.of模式(“dd-MM-uuu”);
列出allEventData=Arrays.asList(新线索(“16-02-2020”),
新潜在客户(“04-02-2020”)、新潜在客户(“08-02-2020”),
新线索(“09-02-2020”);
if(allEventData.isEmpty()){
System.out.println(“无数据”);
}否则{
YearMonth=YearMonth.parse(allEventData.ge
ZoneId z = ZoneId.of( "America/Montreal" ) ;
YearMonth ymCurrent = YearMonth.now( z ) ;
YearMonth ym = YearMonth.from( ld ) ;
if( ym.equals( ymCurrent ) ) { … }