Solved

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

Posted on 2004-10-11
8
371 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 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

23 Experts available now in Live!

Get 1:1 Help Now