Source:
public void getCampaigns() { if (view != null && okHttpJsonApiClient != null) { if (this.campaign != null) { view.showCampaigns(campaign); return; } Single<CampaignResponseDTO> campaigns = okHttpJsonApiClient.getCampaigns(); campaigns.observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribeWith(new SingleObserver<CampaignResponseDTO>() { @Override public void onSubscribe(Disposable d) { disposable = d; } @Override public void onSuccess(CampaignResponseDTO campaignResponseDTO) { List<Campaign> campaigns = campaignResponseDTO.getCampaigns(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); if (campaigns == null || campaigns.isEmpty()) { Timber.tag(TAG).e("The campaigns list is empty"); view.showCampaigns(null); } Collections.sort(campaigns, (campaign, t1) -> { Date date1, date2; try { date1 = dateFormat.parse(campaign.getStartDate()); date2 = dateFormat.parse(t1.getStartDate()); } catch (ParseException e) { e.printStackTrace(); return -1; } return date1.compareTo(date2); }); Date campaignEndDate, campaignStartDate; Date currentDate = new Date(); try { for (Campaign aCampaign : campaigns) { campaignEndDate = dateFormat.parse(aCampaign.getEndDate()); campaignStartDate = dateFormat.parse(aCampaign.getStartDate()); if (campaignEndDate.compareTo(currentDate) >= 0 && campaignStartDate.compareTo(currentDate) <= 0) { campaign = aCampaign; break; } } } catch (ParseException e) { e.printStackTrace(); } view.showCampaigns(campaign); } @Override public void onError(Throwable e) { Timber.tag(TAG).e("could not fetch campaigns: %s", e.getMessage()); } }); } }

Target:
public void getCampaigns() { if (view != null && okHttpJsonApiClient != null) { if (this.campaign != null) { view.showCampaigns(campaign); return; } Single<CampaignResponseDTO> campaigns = okHttpJsonApiClient.getCampaigns(); campaigns.observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribeWith(new SingleObserver<CampaignResponseDTO>() { @Override public void onSubscribe(Disposable d) { disposable = d; } @Override public void onSuccess(CampaignResponseDTO campaignResponseDTO) { List<Campaign> campaigns = campaignResponseDTO.getCampaigns(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); if (campaigns == null || campaigns.isEmpty()) { Timber.e("The campaigns list is empty"); view.showCampaigns(null); } Collections.sort(campaigns, (campaign, t1) -> { Date date1, date2; try { date1 = dateFormat.parse(campaign.getStartDate()); date2 = dateFormat.parse(t1.getStartDate()); } catch (ParseException e) { e.printStackTrace(); return -1; } return date1.compareTo(date2); }); Date campaignEndDate, campaignStartDate; Date currentDate = new Date(); try { for (Campaign aCampaign : campaigns) { campaignEndDate = dateFormat.parse(aCampaign.getEndDate()); campaignStartDate = dateFormat.parse(aCampaign.getStartDate()); if (campaignEndDate.compareTo(currentDate) >= 0 && campaignStartDate.compareTo(currentDate) <= 0) { campaign = aCampaign; break; } } } catch (ParseException e) { e.printStackTrace(); } view.showCampaigns(campaign); } @Override public void onError(Throwable e) { Timber.e(e.getMessage(), "could not fetch campaigns"); } }); } }