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
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
...
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());
}
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
}
}
myLocalTime.plusMinutes( gapInMinutes ) // Using `java.time.LocalTime` class.
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.
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]
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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.