Given a period such as 3 days, or 5 weeks (a period with only one field type), I want to round a given DateTime
to the nearest unit of that period (i.e, ignore the 5 in '5 days'). Examples:
Example 1:
- Period: 3 days.
- DateTime: Wednesday 4:26 AM UTC (2013-05-15T04:26:00Z)
- Rounded DateTime: Wednesday Midnight UTC (2013-05-15T00:00:00Z)
Example 2:
- Period: 5 weeks.
- DateTime: Wednesday 4:26 AM UTC (2013-05-15T04:26:00Z)
- Rounded DateTime: Monday Midnight UTC (2013-05-13T00:00:00Z)
My initial idea was to use Period
's DurationFieldType getFieldTypes()
method, and for every matching field in a DateTime
(below the largest field), set them to zero. However, I don't know how to get the DateTimeFieldType
s from a DateTime
and how to compare them to a DurationFieldType
.
I would prefer not to do a huge if else
approach.