Time comparison doesnt work...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • panos100m
    New Member
    • Aug 2007
    • 23

    Time comparison doesnt work...

    Relevant part of the code::

    //that code wouldnt work ..its purpose is to compare the current time with a give time (in this case italian)
    //output of current time is displayed as 0001 (time is 11:42)
    //To be precise 2212 output of given time is displayed as 2213 time is 9:00....any ideas??


    public static Calendar now2(int hour,int minute,int sec) {
    GregorianCalend ar calendar2 = new GregorianCalend ar();
    calendar2.clear ();
    calendar2.set(C alendar.HOUR_OF _DAY,hour );
    calendar2.set(C alendar.MINUTE, minute);
    calendar2.set(C alendar.SECOND, sec);
    return calendar2;
    }


    GregorianCalend ar calendar = new GregorianCalend ar();

    calendar.clear( );
    calendar.get(Ca lendar.HOUR_OF_ DAY);
    calendar.get(Ca lendar.MINUTE);
    calendar.get(Ca lendar.SECOND);

    lanng="IT";
    int openingh=-1;
    int openingm=-1;
    int openings=00;
    int closingh=-1;
    int closingm=-1;
    int closings=00;

    // int currentime = Integer.parseIn t(now("HHMM"));


    if (langg.equalsIg noreCase("IT"))
    {
    openingh=9;
    openingm=00;
    closingm=16;
    closings=00;

    }
    if (langg.equalsIg noreCase("IT"))
    {
    openingh=9;
    openingm=00;
    closingm=10;
    closings=00;

    }

    if (langg.equalsIg noreCase("DA"))
    {
    openingh=8;
    openingm=00;
    closingm=15;
    closings=00;

    }




    SimpleDateForma t format = new SimpleDateForma t("HHMM");
    System.out.prin tln("To be precise " + format.format(c alendar.getTime ()));

    SimpleDateForma t format2 = new SimpleDateForma t("HHMM");
    System.out.prin tln("To be precise " + format2.format( now2(openingh,o peningm,opening s).getTime()));

    if (choiceName.equ als("Callback") ){

    if ( calendar.before (now2(openingh, openingm,openin gs)) && calendar.after( now2(closingh,c losingm,closing s )))
    {
    choice.setEnabl ed(false);
    System.out.prin tln("Callback DISABLED");

    }
    else{
    choice.setEnabl ed(true);}


    }
  • panos100m
    New Member
    • Aug 2007
    • 23

    #2
    I've corrected the code..

    but still the output is not completely correct

    IT
    To be precise 2008-07-20 12:23:55 (correct)
    To be precise 2008-07-20 09:00:00 (correct)

    updated code:


    public static Calendar now2(int hour,int minute,int sec) {
    GregorianCalend ar calendar2 = new GregorianCalend ar();
    // calendar2.clear ();
    calendar2.set(C alendar.HOUR_OF _DAY,hour );
    calendar2.set(C alendar.MINUTE, minute);
    calendar2.set(C alendar.SECOND, sec);
    return calendar2;
    }
    GregorianCalend ar calendar = new GregorianCalend ar();

    String langg="IT";





    System.out.prin tln (langg);
    int openingh=-1;
    int openingm=-1;
    int openings=0;
    int closingh=-1;
    int closingm=-1;
    int closings=0;


    if (langg.equalsIg noreCase("IT"))
    {
    openingh=9;
    openingm=0;
    closingm=10;
    closings=0;

    }

    if (langg.equalsIg noreCase("DA"))
    {
    openingh=8;
    openingm=0;
    closingm=15;
    closings=0;

    }

    if (langg.equalsIg noreCase("ES"))
    {

    openingh=7;
    openingm=0;
    closingm=16;
    closings=0;

    }

    SimpleDateForma t format = new SimpleDateForma t("yyyy-MM-dd HH:mm:ss");
    System.out.prin tln("To be precise " + format.format(c alendar.getTime ()));

    SimpleDateForma t format2 = new SimpleDateForma t("yyyy-MM-dd HH:mm:ss");
    System.out.prin tln("To be precise " + format2.format( now2(openingh,o peningm,opening s).getTime()));

    if (choiceName.equ als("Callback") ){

    if ( calendar.before (now2(openingh, openingm,openin gs)) && calendar.after( now2(closingh,c losingm,closing s )))
    {
    choice.setEnabl ed(false);
    System.out.prin tln("Callback DISABLED");

    }
    else{
    choice.setEnabl ed(true);
    System.out.prin tln("Callback ENABLED, OPTION");
    }
    }

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by panos100m
      I if ( calendar.before (now2(openingh, openingm,openin gs)) && calendar.after( now2(closingh,c losingm,closing s )))
      If I read your code correctly (your should've put it between those [ code ] ... [ /code ] tags),
      that condition can never be true, the time can never be before opening hours
      *and* after closing hours.

      kind regards,

      Jos

      Comment

      • panos100m
        New Member
        • Aug 2007
        • 23

        #4
        Originally posted by JosAH
        If I read your code correctly (your should've put it between those [ code ] ... [ /code ] tags),
        that condition can never be true, the time can never be before opening hours
        *and* after closing hours.

        kind regards,

        Jos

        That's correct so it should be an || there instead of &&

        int closingm=-1; should be closingh and the clear can be removed from now2

        Thanks :-)

        Comment

        Working...