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
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.
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.
Re: Java - how to check time fall in which section
Quote:
how to do the compare
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.
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;
}
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.
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.
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.