Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Calculating when the condition is during a time frame

1. ## Calculating when the condition is during a time frame

I Am writing my first big java project, and I have a fairly simple question.

How can I calculate (using java.util.Calendar) whether a condition is during a space of time, say whether or not its during school hours.

This is what i have so far...

private void autoSound() {
Calendar schoolStart = Calendar.getInstance();
Calendar schoolEnd = Calendar.getInstance();
schoolStart.clear();
schoolEnd.clear();
schoolStart.set(year, month, day, 7, 30);
schoolEnd.set(year, month, day, 18, 25); //14
if(soundy != 2) {
return;
}
else {
if(current.before(schoolStart) == true || current.after(schoolEnd) == true) { //TODO: Fix during school calculation
sound = true;
}
else {
sound = false;
}
System.out.println(sound);
}
}

Current is uptated like so just before this...

Calendar Now = Calendar.getInstance();
year = Now.get(Calendar.YEAR);
day = Now.get(Calendar.DATE);
month = Now.get(Calendar.MONTH)+ 1;
hour = Now.get(Calendar.HOUR);
min = Now.get(Calendar.MINUTE);
days1 = Now.get(Calendar.DAY_OF_WEEK);
current.clear();
current.set(year, month, day, hour, min);
if(hour == 0) {
hour = 12;
}
}

2. ## Re: Calculating when the condition is during a time frame

So you need a method which tells you when a specified time is before some period's end time and after that period's start time? Hmmm, I think there are enough clues that a person reading the API doc for Calendar might be able to reasonably quickly find an answer...

3. ## Re: Calculating when the condition is during a time frame

Perhaps I also need to look at how millitary time and Java time differ from 12 hour time to....

lol n00b mistake