Solved

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

Posted on 2004-10-11
8
373 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 39
How to convert String matching to regex in java 4 34
Java syntax, or is it Selenium 6 16
Html Table Looping (part 2) 5 10
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

832 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