Compare Local time Joda Time

Asked

Viewed 855 times

2

I have a comparison that checks whether the current time is after the time set.

LocalTime horaConfig = new LocalTime(6, 00, 00);
LocalTime horaAtual = new LocalTime(20, 00, 00);

horaAtual.isAfter(horaConfig);

My problem is, since the current time is 8:00 p.m., Joda Time understands that 6:00 in the morning is after 8:00 P.M. and ends up stopping my process. Only working if I set a time till 11:00.

Is there a method to solve this problem of comparing with Time?

  • It’s not just checking that one is smaller than the other?

1 answer

3


Localtime only stores the time information, does not store information of the day, therefore, 20h will always be bigger than 6h.

Use Datetime instead, and indicate the full date, with day, month, year and time. Example:

import org.joda.time.DateTime;
public class MeuJoda {
    public static void main(String[] args) {
        DateTime dataConfig = DateTime.parse("2014-09-12T06:00:00Z");
        DateTime dataAtual = DateTime.parse("2014-09-11T20:00:00Z");
        System.out.println(dataAtual.isAfter(dataConfig));
    }
}

returns false, for in dataAtual I put 20h yesterday, and in dataConfig I put 6h today.

  • 1

    Thank you, it worked straight for my problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.