[Solved] multiple values in Array ( java )


First of all a few things to help you with your code:

  1. Try following atleast a few conventions (tab your code in for example)
  2. In your if statements and foor loops you are handeling way too many thing. If the first statement didn’t catch that the value is under 60, there is no need to check if it is over or equal to 60, because that is already a given thing.
  3. Catching Exception is as good as handeling no exceptions (if that is your intention, then thats fine).

    for (int i = 0; i<notesArray.size(); i++ ){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
            Date past = format.parse(notesTimeStampArray.get(i));
            Date now = new Date();
            String TimeStamp;
    
            if ( TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  < 60  ) {
                //second
                TimeStamp =  (TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime()) + "s");
                arrayList.add(TimeStamp);
            } else if (TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  < 3600) {
                //minute
                TimeStamp =  (TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + "m");
                arrayList.add(TimeStamp);
            } else if (TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  < 86400){
                //hour
                TimeStamp =  (TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + "h");
                arrayList.add(TimeStamp);
            } else if (TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime())  < 604800){
                //day
                TimeStamp =  (TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + "d");
                arrayList.add(TimeStamp);
            } else {
                //week
                TimeStamp =  (TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime())/7 + "w") ;
                arrayList.add(TimeStamp);
            }
        } catch (Exception j){
            j.printStackTrace();
        }
        data.add(current);
    }
    

Because I don’t really understand your problem, I can only give you the advice to use the debug mode of your IDE. Try adding a Breakpoint to the begining of the for-loop and follow the program step by step.

4

solved multiple values in Array ( java )