-1

Im trying to find the biggest difference between several dates how would that be possible? The answer to this should be 01012001-01012011. I'm trying to find the biggest date gap between two lists how would that be possible?

public void Date (){
    SimpleDateFormat myFormat = new SimpleDateFormat("ddMMyyyy");
    List<String> start = Arrays.asList("01011992","01012001","01012001");
    String dateBeforeString = start.get(2);  
    List<String> end = Arrays.asList("02012001","02012001","01012011");
    String dateAfterString = end.get(2);

    try {        
        Date dateBefore = myFormat.parse(dateBeforeString);
        Date dateAfter = myFormat.parse(dateAfterString);
        long difference = dateAfter.getTime() - dateBefore.getTime();
        daysBetween = (difference / (1000*60*60*24));

        System.out.println("Days: "+daysBetween);
    } catch (Exception e) {
        e.printStackTrace();
    }
}   
6
  • 3
    Java is to Javascript as Pain is to Painting, or Ham is to Hampster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately. Commented Oct 31, 2018 at 9:52
  • 2
    Could you be more clear on your question Commented Oct 31, 2018 at 9:55
  • @CertainPerformance And hamster is to hampster as spelling is to spellping Commented Oct 31, 2018 at 9:55
  • Please define "biggest difference between several dates". Commented Oct 31, 2018 at 10:10
  • 1
    Hi, and what is the error which you are getting, where is the mentioned loop, which are you using? Welcome to stackoverflow, we are not there to help you solve the homeworks, start at minimal reproducible example then feel free to edit the post. Simple advise- you have to iterate over arraylists and compare values, in this case you will need to have loop in the loop , for hint look eg. there Commented Oct 31, 2018 at 11:29

1 Answer 1

0

Try this way:

 SimpleDateFormat myFormat = new SimpleDateFormat("ddMMyyyy");
        List<String> start = Arrays.asList("01011992", "01012001", "01012001");
        List<String> end = Arrays.asList("02012001", "02012001", "01012011");



        int lastIndex =0;
        long lastValue =0;
        for (int i = 0; i < start.size(); i++) {
            String dateBeforeString = start.get(i);
            String dateAfterString = end.get(i);
            try {
                Date dateBefore = myFormat.parse(dateBeforeString);
                Date dateAfter = myFormat.parse(dateAfterString);
                long difference = dateAfter.getTime() - dateBefore.getTime();
                long daysBetween = (difference / (1000 * 60 * 60 * 24));


                if(lastValue<daysBetween){
                    lastValue=daysBetween;
                    lastIndex = i;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("result: " +  start.get(lastIndex)+"-"+end.get(lastIndex)+" maxDiff "+lastValue);
Sign up to request clarification or add additional context in comments.

6 Comments

Looks like works, but so complicated to the new members, if he is asking to how write loop, I dont think so, he will knowns the streams. He was asking for loop, there is none
@Nonika Even though this works it doesn't do what I want. In yours it compares the whole string with the other and finds the biggest difference the whole string.
So you need pair from these lists where difference is max and index is same?
@Nonika I want to compare both lists. So for example the first index in the first and second string should compare in between each other. It cannot be first index from list start and second index from list end. In this situation indexes two have the biggest day difference.Do you understand?
so element at index i in list 'start' compared to element at index i from list 'end'?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.