File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes 5 minutes Time increments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "5 minutes Time increments" Watch "5 minutes Time increments" New topic

5 minutes Time increments

Nee Sun

Joined: Sep 15, 2005
Posts: 3
Hi my Java Program needs to increment the time for every five minutes starting at 6 am to 10 pm. 06:00:00 to 06:05:00 is the first interval, 06:05:00 to 06:10:00,
06:10:00 to 06:15:00,
06:15:00 to 06:20:00,
09:55:00 to 10:00:00

Can some one give me any hints
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Look at the add() methods provided by Calendar.

Calendar is an abstract class, so you'll need an instance of something that extends the Calendar class. You probably want Gregorian Calendar.

You could repeatedly call:
gregorianCalendar.add(Calendar.MINUTE, 5);

Fun programming etcetera!
Nee Sun

Joined: Sep 15, 2005
Posts: 3
import java.util.Calendar;

public class DateFormatExample2 {
public static void main(String[] args) {
Calendar calendar1 = Calendar.getInstance();
int hour = 06;
int minute = 00;
int second = 00;
for (int i = 0; i < 192; i++)
calendar1.set(Calendar.HOUR, hour);
calendar1.set(Calendar.MINUTE, minute);
calendar1.set(Calendar.SECOND, second);
System.out.print(calendar1.get(Calendar.HOUR) + ":" + calendar1.get (Calendar.MINUTE) + ":" + calendar1.get(Calendar.SECOND) + " to ");
calendar1.add(Calendar.MINUTE, 5);
System.out.print(calendar1.get(Calendar.HOUR) + ":" + calendar1.get(Calendar.MINUTE) + ":" + calendar1.get(Calendar.SECOND));
minute = minute + 5;

This Sloves my purpose but is there any better way to do it, if please advice or modify the code and send a reply

Thank you
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33130

Welcome to JavaRanch!

That's a good start. It doesn't look like it works after 7:00 though because the hour isn't adjusted before you set it.

Another approach:

You really only need to do this once, so you could do it outside the loop.

This is the critical piece of code and you have it right. Without the calendar1.set() methods, calendar1 will retain its value. So at the beginning of each loop, it is the same as the ending value from the previous loop.

Stylistically, you might want to show where 192 came from.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: 5 minutes Time increments
It's not a secret anymore!