Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everyone ..
i want to make a time slot that increase by 15 minute i mean that i want to make a time slot like this :

1:00 - 1:15
1:15 - 1:30
1:30 - 1:45
1:45 - 2:00
2:00 - 2:15
.
.
.
.

What I have tried:

i try and make this method but it isn't give a right solution so please help me:



 private ArrayList<String> getTimeSet(boolean isCurrentDay) {
        ArrayList results = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
        for (int i = 0; i < 9; i++) {
            int n=15;
            Calendar calendar = new GregorianCalendar();
            if(!isCurrentDay)


                calendar.set(Calendar.HOUR_OF_DAY, 9);
            calendar.add(Calendar.HOUR_OF_DAY, -i );

            calendar.set(Calendar.MINUTE, 0);
            calendar.add(Calendar.MINUTE, n);

            calendar.set(Calendar.SECOND, 0);

            String  day1 = sdf.format(calendar.getTime());
            calendar.add(Calendar.HOUR, 0);
            calendar.add(Calendar.MINUTE, n);

            String day2 = sdf.format(calendar.getTime());


            String day = day1 + " - " + day2;
            results.add(i, day);
            System.out.println(results);
            n+=15;
        }
        return results;
    }
}
Posted
Updated 4-Mar-20 21:17pm
v2
Comments
Richard MacCutchan 4-Mar-20 4:45am    
Use a counter that goes from zero to however many minutes you want in total, incrementing by 15. Each time you need to create a time just convert the value (number of minutes) to hours and minutes and set the time from that.
Lania Fatah 4-Mar-20 8:33am    
thanks for your notes i will try this , thank you
Richard MacCutchan 4-Mar-20 9:58am    
Using the Calendar class, set it to the start time you want. You then just repeatedly call Calendar.add()[^] to add 15 minutes. The class will take care of normalising the new time to the correct, hour, day etc.
Lania Fatah 5-Mar-20 15:17pm    
thanks for your help
David Crow 4-Mar-20 8:29am    
"but it isn't give a right solution"

So what is it giving?

1 solution

You've made the following errors:

0. This is not C++! I removed the C++ tag from your question.
1. It doesn't make sense to recreate a new calendar instance with every iteration of the loop! Create it outside the loop!
2. Setting the inital time should only occur once, before the loop starts, not inside the loop. Move it outside the loop!
3. You initialize the hour only if isCurrentDay is true. When it's false, what should the starting hour be? You probably need to set some meaningful value.
4. Adding -i to the hour of the day doesn't make any sense. Remove that line!
5. Adding 0 to the hour doesn't do anything. Remove that line!
6. You add the result strings into a container, which is good. But then you print these results inside the loop, which means you keep printing the growing list of strings with every iteration! That's nonsense! Move the output after the loop!
7. You add 15 to n, but then the scope of the loop ends, and n is destroyed because it was created inside the loop! This line has no effect. Either remove the line and calculate the total offset in every loop iteration, or move the declaration of n outside the loop!
Actually you don't need n at all when considering problem 1: if you keep the calender instance alive outside the loop, you can simply increment the time by 15 minutes with every iteration.
8. Why do you print the results inside this function at all? It does return the results, so the caller has the option to print them if needed.

Fixing the above errors gives you this code:

Java
private ArrayList<String> getTimeSet(boolean isCurrentDay) {
        ArrayList results = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR_OF_DAY, 0);// what should be the default?
        if(!isCurrentDay)
            calendar.set(Calendar.HOUR_OF_DAY, 9);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        for (int i = 0; i < 9; i++) {
            String  day1 = sdf.format(calendar.getTime());

            // add 15 minutes to the current time; the hour adjusts automatically!
            calendar.add(Calendar.MINUTE, 15);

            String day2 = sdf.format(calendar.getTime());

            String day = day1 + " - " + day2;
            results.add(i, day);
        }
        return results;
    }
}


You can call it like this:
Java
ArrayList<String> results = getTimeSet(true);
System.out.println(results);

Note that there is no need to keep track of the hour: if adding some minutes moves the amount outside the valid range [0,59], then the class will take care of adjusting the hour accordingly.
 
Share this answer
 
v2
Comments
Lania Fatah 5-Mar-20 15:08pm    
Thank you for taking the time to help me, it will help me get everything I need i really not know how i thanked you , thanks very much....
Lania Fatah 5-Mar-20 15:08pm    
it really help me , thank youuu

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900