Solved

java.sql.time or java.sql.timestamp comparing

Posted on 2004-10-11
8
372 Views
Last Modified: 2008-02-01
I am trying some way to check if the current time is within a certain Range. For example, I want to have a range that goes from Tuesday at 8 am local time to Sunday at 10 am local Time. I would like these variables to be customizable by the way, but i would assume the formula wouldnt rely on a specific time. Then I want to be able to check if the current time is within that range. So if the current day is monday, it would not pass the check.  However if the current time was wednesday it would pass. I can see how to do this if this was just one week, then i could compare it this one time, however i want it to be set up so it doesnt matter what week you are in as long as you are within the defined range.

so im guessing it would be something like this
1)define start time, start day, define end time, end day
2)check if current time and day are after starttime and before end time and same for days.


The part that confuses me is that i want it based on the Day of week, not date of month/year
0
Comment
Question by:Hokester
  • 5
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12280739
boolean withRange =  (date.after(date1) && date.before(date2));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12280763
>>that i want it based on the Day of week, not date of month/year

That seems to somewhat contradict this:

>>i want it to be set up so it doesnt matter what week you are in as long as you are within the defined range.


0
 

Author Comment

by:Hokester
ID: 12280826
Ok, maybe im not being clear
Input: Start = Tuesday 8 am
          End   = Sunday 10 am

-------------------------------------------------------------------------
current time = Monday Oct 11, 4:42 pm
Date now = new Date(System.currentTimeInMillis());

boolean result = codeImMissing(now);

in this case
result = false; because monday oct 11 isnt within time range
-------------------------------------------------------------------------
Next week same time
current time = Monday Oct 18, 4:42 pm
Date now = new Date(System.currentTimeInMillis());

boolean result = codeImMissing(now);

in this case result is still false, not between tuesday and sunday
-------------------------------------------------------------------------
Tomorrow, this time
current time = Tuesay Oct 12, 4:42 pm
Date now = new Date(System.currentTimeInMillis());

boolean result = codeImMissing(now);

in this case result is true because it is between range.
-------------------------------------------------------------------------
So the idea is that i dont want the input to store the October 11 part, i just want it to store and compare based on Tuesday 8 am and Sunday 10 pm, regardless of the 1-30 date of the month.

make any more sense?






0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12280888
>>
Input: Start = Tuesday 8 am
          End   = Sunday 10 am
>>

How do we know whether that's last or next Tuesday/Sunday?
0
 

Author Comment

by:Hokester
ID: 12281125
the end is always guaranteed to be after the start date and be less than 7 days away.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 12281305
OK. Working on the basis that Sunday is *next* Sunday:


     Calendar cal = Calendar.getInstance();
     while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
       cal.add(Calendar.DATE, 1);
     }
     Date endDate = cal.getTime();
     System.out.println("End date = " + endDate);


     while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.TUESDAY) {
       cal.add(Calendar.DATE, -1);
     }

     Date startDate = cal.getTime();
     System.out.println("Start date = " + startDate);


     Date now = cal.getTime();

     boolean withinRange =  (now.after(startDate) && now.before(endDate));
     System.out.println("withinRange = " + withinRange);


You may want to set hours and minutes to startend of day at the limits
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12281309
>>You may want to set hours and minutes to startend of day at the limits

was meant to be

You may want to set hours and minutes to start/end of day at the limits
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bitbucket vs gitbucket 3 57
custom annotations 9 32
tomcat not starting 6 31
How to convert String matching to regex in java 4 21
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now