Comparing Time in Hours & Minutes

Discussion in 'Java' started by guyzdancin, Feb 22, 2005.

  1. guyzdancin

    guyzdancin Guest

    I'm parsing a csv file that contains data in 15 minute intervals for a
    day. The data is electricity consumption. The day is divided up into
    segments of high, medium and low billing rates. For example, 00:00:00
    to 10:45:00 is low rate, 11:00:00 to 18:45:00 is medium rate and
    19:00:00 to 23:45:00 is high rate. I need a compare method so that I
    can determine if any given time is greater or less than another.

    Below is a sample of the file. The fourth field is time.

    170,3EA,1041,18:00:00,.2847
    170,3EA,1041,18:15:00,.2649
    170,3EA,1041,18:30:00,.2486
    170,3EA,1041,18:45:00,.3585
    170,3EA,1041,19:00:00,.4179
    170,3EA,1041,19:15:00,.4846
    170,3EA,1041,19:30:00,.3480
    170,3EA,1041,19:45:00,.2239
    170,3EA,1041,20:00:00,.2600
    170,3EA,1041,20:15:00,.2024
    170,3EA,1041,20:30:00,.2126
    170,3EA,1041,20:45:00,.3027
    170,3EA,1041,21:00:00,.2882
    170,3EA,1041,21:15:00,.2559
    170,3EA,1041,21:30:00,.1392
    170,3EA,1041,21:45:00,.1373

    Thanks in advance
    guyzdancin, Feb 22, 2005
    #1
    1. Advertising

  2. guyzdancin

    Guest

    What about putting these into an array called lines.

    String[] tokens = lines[counter].split(",");

    tokens = tokens[3].split(":");

    int hours = Integer.parseInt(tokens[0]);
    int minutes = Integer.parseInt(tokens[1]);
    int seconds = Integer.parseInt(tokens[2]);

    Then you could figure out how many seconds there are between midnight
    and that time.
    , Feb 22, 2005
    #2
    1. Advertising

  3. guyzdancin

    Rhino Guest

    "guyzdancin" <> wrote in message
    news:...
    > I'm parsing a csv file that contains data in 15 minute intervals for a
    > day. The data is electricity consumption. The day is divided up into
    > segments of high, medium and low billing rates. For example, 00:00:00
    > to 10:45:00 is low rate, 11:00:00 to 18:45:00 is medium rate and
    > 19:00:00 to 23:45:00 is high rate. I need a compare method so that I
    > can determine if any given time is greater or less than another.
    >
    > Below is a sample of the file. The fourth field is time.
    >
    > 170,3EA,1041,18:00:00,.2847
    > 170,3EA,1041,18:15:00,.2649
    > 170,3EA,1041,18:30:00,.2486
    > 170,3EA,1041,18:45:00,.3585
    > 170,3EA,1041,19:00:00,.4179
    > 170,3EA,1041,19:15:00,.4846
    > 170,3EA,1041,19:30:00,.3480
    > 170,3EA,1041,19:45:00,.2239
    > 170,3EA,1041,20:00:00,.2600
    > 170,3EA,1041,20:15:00,.2024
    > 170,3EA,1041,20:30:00,.2126
    > 170,3EA,1041,20:45:00,.3027
    > 170,3EA,1041,21:00:00,.2882
    > 170,3EA,1041,21:15:00,.2559
    > 170,3EA,1041,21:30:00,.1392
    > 170,3EA,1041,21:45:00,.1373
    >
    > Thanks in advance
    >


    I'll assume that you can split the time from each line of the CSV file into
    hours and minutes on your own; there are various ways to do that.

    Now, for any pair of times you want to compare, convert each time into an
    instance of Calendar; you only need to specify the HOUR_OF_DAY and MINUTE.
    Then, convert the Calendar representing a given time into an instance of
    java.util.Date. Then, use the before() and after() methods in the
    java.util.Date class to compare any pair of Dates. This example illustrates
    the basic technique for creating the Calendars and Dates and then comparing
    the Dates:

    /* Create calendar for first Time. */

    Calendar cal1 = Calendar.getInstance();
    cal1.set(Calendar.HOUR_OF_DAY, 10);

    cal1.set(Calendar.MINUTE, 45);

    Date time1 = cal1.getTime();



    /* Create Calendar for second Time. */

    Calendar cal2 = Calendar.getInstance();

    cal2.set(Calendar.HOUR_OF_DAY, 10);

    cal2.set(Calendar.MINUTE, 45);

    Date time2 = cal2.getTime();



    /* Compare the two times to see which was earlier. */

    if (time2.before(time1)) {

    System.out.println("Time2 is before Time1.");

    } else {

    System.out.println("Time1 is before or equal to Time2.");

    }



    Rhino
    Rhino, Feb 22, 2005
    #3
  4. In article <>,
    "guyzdancin" <> wrote:

    > I'm parsing a csv file that contains data in 15 minute intervals for a
    > day. The data is electricity consumption. The day is divided up into
    > segments of high, medium and low billing rates. For example, 00:00:00
    > to 10:45:00 is low rate, 11:00:00 to 18:45:00 is medium rate and
    > 19:00:00 to 23:45:00 is high rate. I need a compare method so that I
    > can determine if any given time is greater or less than another.
    >
    > Below is a sample of the file. The fourth field is time.
    >
    > 170,3EA,1041,18:00:00,.2847
    > 170,3EA,1041,18:15:00,.2649
    > 170,3EA,1041,18:30:00,.2486
    > 170,3EA,1041,18:45:00,.3585
    > 170,3EA,1041,19:00:00,.4179
    > 170,3EA,1041,19:15:00,.4846
    > 170,3EA,1041,19:30:00,.3480
    > 170,3EA,1041,19:45:00,.2239
    > 170,3EA,1041,20:00:00,.2600
    > 170,3EA,1041,20:15:00,.2024
    > 170,3EA,1041,20:30:00,.2126
    > 170,3EA,1041,20:45:00,.3027
    > 170,3EA,1041,21:00:00,.2882
    > 170,3EA,1041,21:15:00,.2559
    > 170,3EA,1041,21:30:00,.1392
    > 170,3EA,1041,21:45:00,.1373
    >
    > Thanks in advance


    A GregorianCalendar can decode millisecond timestamps into calendar
    components so you can compare hours, minutes, and seconds of a day
    easily.
    Kevin McMurtrie, Feb 22, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 00_CP_D12
    Replies:
    3
    Views:
    8,900
    dilipv
    Feb 22, 2008
  2. Rahmi Acar
    Replies:
    0
    Views:
    559
    Rahmi Acar
    Jul 17, 2003
  3. Eric

    Time Format in datagrid (Hours, Minutes, Seconds)

    Eric, Aug 12, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    283
    Alvin Bruney [MVP]
    Aug 12, 2004
  4. Michael Chernecki

    Comparing system time to specific hours

    Michael Chernecki, Jul 19, 2004, in forum: Perl Misc
    Replies:
    8
    Views:
    113
    krakle
    Jul 21, 2004
  5. rutherf
    Replies:
    2
    Views:
    424
    rutherf
    Oct 28, 2006
Loading...

Share This Page