This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "5 minutes Time increments" Watch "5 minutes Time increments" New topic
Author

5 minutes Time increments

Nee Sun
Greenhorn

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,
etc
etc
etc
etc
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
Greenhorn

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));
System.out.println();
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

Nee,
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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
 
subject: 5 minutes Time increments
 
Similar Threads
calculating the number of business minutes in a month
back ground process id ??
java sql help
BIG PROBLEM ON DATE
Adding 2 Times Together