Checking time in java

2

Given a list with timetables eg: String [] schedules = {"11:00", "12:00", "13:00"};

I want to pick up my current time and compare it to the list next time, ie current time is 11:30 am, I get the "12:00" list.

    
asked by anonymous 11.07.2015 / 07:54

2 answers

1

To compare dates you should use Android Calendar.

Calendar horaComparar = Calendar.getInstance();
horaComparar.setTime(new SimpleDateFormat("h:mm").parse(horarios[0]));

Date horaAtual = new Date(System.currentTimeMillis());

if (horaComparar.getTime().compareTo(horaAtual) > 0 ){
   // hora é posterior a hora Atual.
}
  
  • The return value is 0 if both dates are equal.
  •   
  • The return value is greater than 0 if the date is later than the argument date.
  •   
  • The return value is less than 0 if the date is preceding the date argument.
  •   
    
11.09.2015 / 22:10
3

If you're referring to the java.sql.Time class, then just use the method valueOf(String) :

String hora1 = "23:00";
Time n1 = Time.valueOf(hora1 + ":00");

String hora2 = "02:15";
Time n2 = Time.valueOf(hora2 + ":00");

if (n1.after(n2))...

If you're using android.text.format.Time , then see javadoc:

  

This class was deprecated in API level 22.
  Use GregorianCalendar instead.

That translating into Portuguese is:

  

This class has been deprecated in API level 22.
  Use GregorianCalendar instead.

    
11.07.2015 / 08:09