Example usage for org.joda.time DurationField getType

List of usage examples for org.joda.time DurationField getType

Introduction

In this page you can find the example usage for org.joda.time DurationField getType.

Prototype

public abstract DurationFieldType getType();

Source Link

Document

Get the type of the field.

Usage

From source file:com.facebook.util.TimeIntervalType.java

License:Apache License

/**
 * Gets the start instant given the event instant, interval length 
 * and the time zone for this interval type.
 * /*from www  .ja  va2s.c o m*/
 * @param instant the event time instant.
 * @param length the interval length
 * 
 * @return the start instant of the interval of given length that contains 
 * the supplied time instant in the supplied time zone 
 */
public DateTime getTimeIntervalStart(DateTime instant, long length) {
    validateValue(instant.getZone(), length);
    // Get the time in the specified timezone
    DateTime periodStart = instant;
    // Clear all the fields for this intervalType and its subtypes
    TimeIntervalType timeIntervalType = this;
    while (timeIntervalType != null) {
        periodStart = timeIntervalType.clearField(periodStart);
        timeIntervalType = timeIntervalType.subType;
    }
    // figure out the which time interval does the instant lie in
    Period period = new Period(periodStart, instant, periodType);
    DurationField durationField = fieldType.getField(instant.getChronology()).getDurationField();
    int diff = period.get(durationField.getType());
    long startDelta = (diff / length) * length;
    return periodStart.withFieldAdded(durationField.getType(), FieldUtils.safeToInt(startDelta));
}