|
-
September 29th, 2016, 03:42 AM
#1
Java - how to check time fall in which section
Hi, I need to check on current time is falling to which timeframe as below, may I know how can create the checking in java??
if (yesterday 20:00 until today 02:00) then display xxx
else if (today 02:01 until today 14:00) then display yyy
else if (today 14:01 until today 20:00) then display zzz
-
September 30th, 2016, 12:06 PM
#2
Re: Java - how to check time fall in which section
If you had Date objects for each of the times and used its get time method to retrieve number of milliseconds, you could easily compare those values.
If you are using java 1.8 look at the java.time package for some useful classes/methods.
Norm
-
October 3rd, 2016, 07:49 PM
#3
Re: Java - how to check time fall in which section
Hi Norn, sorry that I new to Java, can you explain how to do the compare? I found there is java.time as well.
-
October 3rd, 2016, 07:51 PM
#4
Re: Java - how to check time fall in which section
The times I was talking about are contained in a long variable. The compares would be between two numeric values that represent the milliseconds since the EPOCH time.
Norm
-
October 3rd, 2016, 07:56 PM
#5
Re: Java - how to check time fall in which section
I write as below, but it seems not working
Can you help me check what is wrong on the code?
Calendar calendar = Calendar.getInstance();
int hrs = ( calendar.get( Calendar.HOUR_OF_DAY ) + 8 );
int minute = calendar.get( Calendar.MINUTE );
TimsSpan time01 = new TimeSpan(01, 0, 0);
if (( hrs > 20 && minute > 00 ) && ( hrs < 02 && minute < 01 )) {
then xxx;
} else if (( hrs > 2 && minute > 00 ) && ( hrs < 14 && minute < 01 )) {
then yyy;
} else if (( hrs > 14 && minute > 00 ) && ( hrs < 20 && minute < 01 )) {
then zzz;
}
-
October 3rd, 2016, 08:03 PM
#6
Re: Java - how to check time fall in which section
Can you post a small, complete program that compiles, executes and shows the problem?
Add some print statements that shows what the code is doing and what the values are it is testing.
Be sure to wrap the code in code tags to preserve formatting.
Norm
-
October 3rd, 2016, 08:23 PM
#7
Re: Java - how to check time fall in which section
I think you are not able to provide any help on this.. thanks anyway.. I getting help from other place.
-
October 3rd, 2016, 08:29 PM
#8
Re: Java - how to check time fall in which section
Good luck.
It is not possible to help you with the bits and pieces of unformatted code you posted. There needs to be something that will compile and execute for testing.
Be sure to post the solution you find here in case others have the same problem.
Norm
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|