1

I'm making a time comparison in a loop. So it checks me all the times saved in HH:mm format, and then it does other instructions.
If i try to compare a time that's between 00:00 and 09:59, it gives me error. What can i do?

for(int contatore = 0; lista_allarmi.moveToNext() ; contatore++) {
    Date ORANOTIFICA;
    ID_M = lista_allarmi.getInt(0);
    NOME_M = lista_allarmi.getString(1);
    TIPO_M = lista_allarmi.getString(2);
    String ora_notifica = lista_allarmi.getString(3);
    parts = ora_notifica.split(":");
    hrs = Integer.parseInt(parts[0]); // ore
    min = Integer.parseInt(parts[1]); // minuti
    ora_allm.set(Calendar.HOUR_OF_DAY,hrs);
    ora_allm.set(Calendar.MINUTE, min);
    ora_allm.set(Calendar.SECOND, 0);
    ora_allm_piut = ora_allm;
    ora_allm_piut.add(Calendar.MINUTE, 5);
    hrs = ora_allm_piut.get(Calendar.HOUR_OF_DAY);
    min = ora_allm_piut.get(Calendar.MINUTE);
    HPT = String.valueOf(hrs);
    MPT = String.valueOf(min);
    ORARNPT = HPT + ":" + MPT; ORANOTIFICAPIUTIMER;
    Date ora_attuale = Calendar.getInstance().getTime();
    Date ORARIOATT;

    i = new Intent(getContext(), AlarmReceiver.class);
    i.putExtra("id", ID_M);
    i.putExtra("NMM", NOME_M);
    i.putExtra("TPM", TIPO_M);
    pi = PendingIntent.getBroadcast(getContext(), contatore, i, PendingIntent.FLAG_UPDATE_CURRENT);
    if(LocalTime.now().isAfter(parse(ora_notifica)) && LocalTime.now().isBefore(parse(ORARNPT))) {
         //A
    } else {
         //B
    }
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • could you show what `parse(ora_notifica)` does? – dehasi Jun 28 '18 at 11:32
  • 1
    Thank you for posting _some_ code, but this isn't enough for people to quickly spot an error, and neither did you include the error which you get. If it is an exception, include the full stacktrace. For the code, create a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve) and post it here. – M. le Rutte Jun 28 '18 at 11:33
  • Have you tried to debug your code? – Joakim Danielson Jun 28 '18 at 13:00
  • `lista_allarmi.moveToNext()` looks like a really strange limit in a `for` loop. – Joakim Danielson Jun 28 '18 at 13:08
  • Debugger: `java.time.format.DateTimeParseException: Text '9:25' could not be parsed at index 0` but i've saved the time as 09:25 – Federico Chiaradia Jun 28 '18 at 13:32
  • @FedericoChiaradia Post further details as edits to the Question rather than as a Comment. – Basil Bourque Jun 29 '18 at 04:48
  • 1
    In your last comment, you quote an error from parsing in *java.time* classes, but I see no *java.time* parsing in your source code above. Please take more care to be thorough and well-written before posting on Stack Overflow. And take the time to write short contrived example code rather than cut-and-paste your live code. – Basil Bourque Jun 29 '18 at 04:50

2 Answers2

2

tl;dr

LocalTime.parse(
    "9:25" ,
    DateTimeFormatter.ofPattern( "H:m" )
)
.isBefore(
    LocalTime.of( 10 , 0 ) 
)

You are using the wrong classes

Never use the terribly troublesome old date-time classes bundled with the earliest versions of Java. These are now legacy, supplanted entirely by the java.time classes built into Java 8 and later. Avoid Date, Calendar, and SimpleDateFormat altogether.

Format must match input

HH:mm format

java.time.format.DateTimeParseException: Text '9:25' could not be parsed at index 0

Your formatting pattern says HH which means two digit value. For hours 1-9, a leading padding zero is expected. Your input of 9:25 is missing its padding zero.

For such input, use a single H. And likely the minutes lack a padding zero, so use a single m there too.

DateTimeFormatter.ofPattern( "H:m" )

Do not work so hard

The java.time classes will parse your string inputs for you. No need to analyze the text yourself.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "H:m" ) ;
LocalTime lt = LocalTime.parse( "9:25" , f ) ;

Half-Open

compare a time that's between 00:00 and 09:59,

When defining a span of time, the Half-Open approach is generally the best. In this approach, the beginning is considered inclusive while the ending is exclusive. This means the hour of 9 AM starts at 09:00 and runs up to, but does not include, the first moment of the following hour, 10:00.

One benefit of Half-Open is that you avoid trying to nail down the ever-divisible last moment of the hour. Is the last moment 09:59? 09:59.9? 09:59.999999999? Avoid the entire debacle by instead asking “Is the moment before 10 AM?“.

For time-of-day only, without the context of a date, the clock stops at midnight. So if you want ask if a certain time of day is between midnight and 10 AM, you need only ask if it is before 10 AM.

LocalTime stop = LocalTime.of( 10 , 0 );
LocalTime x = LocalTime.parse( "09:25" ) ;
Boolean isEarlyEnough = x.isBefore( stop ) ;

Perhaps you want to use a starting time other than first moment of the day. In that case, specify a start as well as a stop.

LocalTime start = LocalTime.MIN ;  // Constant for 00:00:00.000000000.
LocalTime stop = LocalTime.of( 10 , 0 );
LocalTime x = LocalTime.parse( "09:25" ) ;
Boolean isEarlyEnoughButNotTooEarly = ( ! x.isBefore( start ) ) &&  x.isBefore( stop ) ;  // "Not before" is a briefer way of saying "Is equal to or later than".

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

I forgot to add a "0" when the hour taken with Calendar.HOUR_OF_DAY was <=9 :

hrs=ora_allm_piut.get(Calendar.HOUR_OF_DAY);
min=ora_allm_piut.get(Calendar.MINUTE);
if(hrs<=9){HPT=String.valueOf("0"+hrs);

Now the time respect the time format, and i can make the comparison.