This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

5 minutes Time increments

 
Nee Sun
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Nee Sun
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic