0

I need to compare whether a DateTime object is greater/less than 9AM PST using JodaTime library in Android. I am getting the DateTime object in PST like this:

DateTime dt = new DateTime(DateTimeZone.forID( "America/Los_Angeles" ));

How can I compare whether the time recorded for this object is greater/less than 9AM PST?

Thanks.

user2284140
  • 197
  • 1
  • 4
  • 18

1 Answers1

1

You can use one of the next ways to do what you want.

1) Create a DateTime with the TimeZone and the hour that you want.

DateTime dt = new DateTime()
             .withHourOfDay(9)
             .withZone(DateTimeZone.forID("America/Los_Angeles"));

And then compare dt with the DateTime that you receive, create, etc.

dt.isAfter(date) or dt.isBefore(date)

2) You can set the TimeZone that you want to a copy of the DateTime that you receive.

I say copy because DateTime object is immutable. You can not modify the DateTime which you receive but create one from it, yes. You can read more about that here: Why are Joda objects immutable?

datePST = date.withZone(DateTimeZone.forID("America/Los_Angeles"));
int hour = datePST.getHourOfDay()

Now you just need to check if hour is < or > than 9.

Cata
  • 573
  • 5
  • 13