[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2004-10-11
8
Medium Priority
?
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 500 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

656 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