2

I am wondering how I would loop through a date/time or any type of variable to go from 00:00 to 24:00 every 30 Mins?

So I need a variable that shows times in 24HR format (01:00, 09:00) and every time I loop through it, to add 30 mins to the time? I then need to use this value in a string.

The time needs to start at 00:00AM and will end with 24:00.

Any ideas how should I go with it?

output should be like this - 00:00 00:30 01:00 ....24:00
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Vivek
  • 10,978
  • 14
  • 48
  • 66
  • 2
    Well lets see ... there's 60 seconds in a minute .... – Brian Roach Jan 10 '13 at 06:03
  • Take a look at [Calendar](http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html) – MadProgrammer Jan 10 '13 at 06:05
  • Expanding on Brian's comment: then you would probably add "30 minutes" worth of time. This will vary by library, but since "30 minutes" is the same as "1800 (30 * 60) seconds" .. –  Jan 10 '13 at 06:06
  • Keep mind that not all days are 24 hours long. They may be 23, 25, or some other number of hours long. – Basil Bourque Jul 19 '17 at 00:59

4 Answers4

15

Possibly a little over kill, but it does all the auto rolling and allows the use of DateFormat

DateFormat df = new SimpleDateFormat("HH:mm");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
int startDate = cal.get(Calendar.DATE);
while (cal.get(Calendar.DATE) == startDate) {
    System.out.println(df.format(cal.getTime()));
    cal.add(Calendar.MINUTE, 30);
}

You can't have 24:00 as it's 00:00...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
3

Try to use this this code

private void displayTimeSlots() {

    String timeValue = "T00:00:4.896+05:30";
    SimpleDateFormat sdf = new SimpleDateFormat("'T'hh:mm:ss.SSS");
    try {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(sdf.parse(timeValue));

        if (startCalendar.get(Calendar.MINUTE) < 30) {
            startCalendar.set(Calendar.MINUTE, 30);
        } else {
            startCalendar.add(Calendar.MINUTE, 30); // overstep hour and clear minutes
            startCalendar.clear(Calendar.MINUTE);
        }

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(startCalendar.getTime());

        // if you want dates for whole next day, uncomment next line
        //endCalendar.add(Calendar.DAY_OF_YEAR, 1);
        endCalendar.add(Calendar.HOUR_OF_DAY, 24 - startCalendar.get(Calendar.HOUR_OF_DAY));

        endCalendar.clear(Calendar.MINUTE);
        endCalendar.clear(Calendar.SECOND);
        endCalendar.clear(Calendar.MILLISECOND);

        SimpleDateFormat slotTime = new SimpleDateFormat("hh:mm a");
        while (endCalendar.after(startCalendar)) {

            startCalendar.add(Calendar.MINUTE, 30);
            String Timeslots = slotTime.format(startCalendar.getTime());


            Log.e("DATE", Timeslots);
        }

    } catch (ParseException e) {
        // date in wrong format
    }
}
Zoin-Coder
  • 103
  • 9
  • 1
    @ArslanShahid FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 19 '17 at 01:01
2

tl;dr

myLocalTime.plusMinutes( gapInMinutes )  // Using `java.time.LocalTime` class.

Details

I am answering your Question as written, for a 24-hour day. But beware that days are not always that length. They may be 23, 25, or some other number of hours.

No such thing as 24:00 as that means rolling over to 00:00. So a duration of 30 minutes means ending at 23:30.

java.time

The modern approach uses the java.time classes that supplant the troublesome old date-time time classes.

LocalTime & Duration

The LocalTime class represents a time-of-day without date and without time zone. This class assumes a generic 24-hour day (unrealistic though that is).

The Duration class represents a span-of-time not attached to the timeline.

int gapInMinutes =  30 ;  // Define your span-of-time.
int loops = ( (int) Duration.ofHours( 24 ).toMinutes() / gapInMinutes ) ;
List<LocalTime> times = new ArrayList<>( loops ) ;

LocalTime time = LocalTime.MIN ;  // '00:00'
for( int i = 1 ; i <= loops ; i ++ ) {
    times.add( time ) ;
    // Set up next loop.
    time = time.plusMinutes( gapInMinutes ) ;
}

System.out.println( times.size() + " time slots: " ) ;
System.out.println( times ) ;

See this code run live at IdeOne.com.

48 time slots:

[00:00, 00:30, 01:00, 01:30, 02:00, 02:30, 03:00, 03:30, 04:00, 04:30, 05:00, 05:30, 06:00, 06:30, 07:00, 07:30, 08:00, 08:30, 09:00, 09:30, 10:00, 10:30, 11:00, 11:30, 12:00, 12:30, 13:00, 13:30, 14:00, 14:30, 15:00, 15:30, 16:00, 16:30, 17:00, 17:30, 18:00, 18:30, 19:00, 19:30, 20:00, 20:30, 21:00, 21:30, 22:00, 22:30, 23:00, 23:30]


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

try this -

Calendar instance = Calendar.getInstance();
instance.setTime(new SimpleDateFormat("hh:mm:ss a").parse("00:00:00 AM"));
System.out.println(instance.getTime());
int i=1;
while(i++!=49){
     instance.add(Calendar.MINUTE, 30);
     System.out.println(instance.getTime());
}
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103