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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Calculating when the condition is during a time frame

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...


     private void advanceTime() {
    			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;
    			}
    		}
    Last edited by YoungMaker; September 26th, 2011 at 04:05 PM.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. Have Problem with if condition...Please Help me...
    By Jalpesh Ruparel in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2011, 01:54 AM
  2. Waiting until condition
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: October 22nd, 2010, 09:24 AM
  3. [SOLVED] frame are exiting at the same time
    By chronoz13 in forum AWT / Java Swing
    Replies: 8
    Last Post: January 25th, 2010, 08:13 PM
  4. Problem with condition
    By shamed in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 7th, 2009, 04:51 PM