user1028741
user1028741

Reputation: 2825

Round DateTime by a given DateTimeFieldType

I use org.joda.time.

I have a DateTime and a DateTimeFieldType given.

What I want is to round my DateTime (up) to the nearest DateTime which is after mine, but with an integer number of the given field.

For example:

for (DateTime = 20/10/2013 14:20:31.200, DateTimeFiledType = DateTimeFieldType.dayOfMonth())

I will get: 21/10/2013 00:00:00.000.

Did somebody do something similar?

Upvotes: 1

Views: 250

Answers (1)

adchilds
adchilds

Reputation: 963

You can use:

DateTime.now().dayOfMonth().roundFloorCopy()

or

DateTime.now().dayOfMonth().roundCeilingCopy()

Take a look at the DateTime.Property class for more information on this.

Edit:

As Krayo mentioned, you can pass the property directly as:

DateTime.now().property(DateTimeFieldType.secondOfDay()).roundFloorCopy();

Upvotes: 4

Related Questions