Solved

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

Posted on 2004-10-11
8
374 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

861 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