CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2016
    Posts
    4

    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

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  3. #3
    Join Date
    Sep 2016
    Posts
    4

    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.

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java - how to check time fall in which section

    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.
    Norm

  5. #5
    Join Date
    Sep 2016
    Posts
    4

    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;
    }

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  7. #7
    Join Date
    Sep 2016
    Posts
    4

    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.

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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
  •  





Click Here to Expand Forum to Full Width

Featured