Search in sources :

Example 6 with PeriodFormatter

use of org.joda.time.format.PeriodFormatter in project h2o-3 by h2oai.

the class PrettyPrint method toAge.

public static String toAge(Date from, Date to) {
    if (from == null || to == null)
        return "N/A";
    final Period period = new Period(from.getTime(), to.getTime());
    DurationFieldType[] dtf = new ArrayList<DurationFieldType>() {

        {
            add(DurationFieldType.years());
            add(DurationFieldType.months());
            add(DurationFieldType.days());
            if (period.getYears() == 0 && period.getMonths() == 0 && period.getDays() == 0) {
                add(DurationFieldType.hours());
                add(DurationFieldType.minutes());
            }
        }
    }.toArray(new DurationFieldType[0]);
    PeriodFormatter pf = PeriodFormat.getDefault();
    return pf.print(period.normalizedStandard(PeriodType.forFields(dtf)));
}
Also used : PeriodFormatter(org.joda.time.format.PeriodFormatter) DurationFieldType(org.joda.time.DurationFieldType) Period(org.joda.time.Period)

Example 7 with PeriodFormatter

use of org.joda.time.format.PeriodFormatter in project joda-time by JodaOrg.

the class StringConverter method setInto.

//-----------------------------------------------------------------------
/**
     * Sets the value of the mutable interval from the string.
     * 
     * @param writableInterval  the interval to set
     * @param object  the String to convert, must not be null
     * @param chrono  the chronology to use, may be null
     */
public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) {
    String str = (String) object;
    int separator = str.indexOf('/');
    if (separator < 0) {
        throw new IllegalArgumentException("Format requires a '/' separator: " + str);
    }
    String leftStr = str.substring(0, separator);
    if (leftStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }
    String rightStr = str.substring(separator + 1);
    if (rightStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }
    DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser();
    dateTimeParser = dateTimeParser.withChronology(chrono);
    PeriodFormatter periodParser = ISOPeriodFormat.standard();
    long startInstant = 0, endInstant = 0;
    Period period = null;
    Chronology parsedChrono = null;
    // before slash
    char c = leftStr.charAt(0);
    if (c == 'P' || c == 'p') {
        period = periodParser.withParseType(getPeriodType(leftStr)).parsePeriod(leftStr);
    } else {
        DateTime start = dateTimeParser.parseDateTime(leftStr);
        startInstant = start.getMillis();
        parsedChrono = start.getChronology();
    }
    // after slash
    c = rightStr.charAt(0);
    if (c == 'P' || c == 'p') {
        if (period != null) {
            throw new IllegalArgumentException("Interval composed of two durations: " + str);
        }
        period = periodParser.withParseType(getPeriodType(rightStr)).parsePeriod(rightStr);
        chrono = (chrono != null ? chrono : parsedChrono);
        endInstant = chrono.add(period, startInstant, 1);
    } else {
        DateTime end = dateTimeParser.parseDateTime(rightStr);
        endInstant = end.getMillis();
        parsedChrono = (parsedChrono != null ? parsedChrono : end.getChronology());
        chrono = (chrono != null ? chrono : parsedChrono);
        if (period != null) {
            startInstant = chrono.add(period, endInstant, -1);
        }
    }
    writableInterval.setInterval(startInstant, endInstant);
    writableInterval.setChronology(chrono);
}
Also used : PeriodFormatter(org.joda.time.format.PeriodFormatter) Period(org.joda.time.Period) ReadWritablePeriod(org.joda.time.ReadWritablePeriod) Chronology(org.joda.time.Chronology) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime)

Example 8 with PeriodFormatter

use of org.joda.time.format.PeriodFormatter in project joda-time by JodaOrg.

the class StringConverter method setInto.

//-----------------------------------------------------------------------
/**
     * Extracts duration values from an object of this converter's type, and
     * sets them into the given ReadWritableDuration.
     *
     * @param period  period to get modified
     * @param object  the String to convert, must not be null
     * @param chrono  the chronology to use
     * @return the millisecond duration
     * @throws ClassCastException if the object is invalid
     */
public void setInto(ReadWritablePeriod period, Object object, Chronology chrono) {
    String str = (String) object;
    PeriodFormatter parser = ISOPeriodFormat.standard();
    period.clear();
    int pos = parser.parseInto(period, str, 0);
    if (pos < str.length()) {
        if (pos < 0) {
            // Parse again to get a better exception thrown.
            parser.withParseType(period.getPeriodType()).parseMutablePeriod(str);
        }
        throw new IllegalArgumentException("Invalid format: \"" + str + '"');
    }
}
Also used : PeriodFormatter(org.joda.time.format.PeriodFormatter)

Example 9 with PeriodFormatter

use of org.joda.time.format.PeriodFormatter in project joda-time by JodaOrg.

the class Interval method parseWithOffset.

/**
     * Parses a {@code Interval} from the specified string, using any offset it contains.
     * <p>
     * The String formats are described by
     * {@link ISODateTimeFormat#dateTimeParser()}{@code .withOffsetParsed()}
     * and {@link ISOPeriodFormat#standard()}, and may be 'datetime/datetime',
     * 'datetime/period' or 'period/datetime'.
     * <p>
     * Sometimes this method and {@code new Interval(str)} return different results.
     * This can be confusing as the difference is not visible in {@link #toString()}.
     * <p>
     * When passed a string without an offset, such as '2010-06-30T01:20/P1D',
     * both the constructor and this method use the default time-zone.
     * As such, {@code Interval.parseWithOffset("2010-06-30T01:20/P1D")} and
     * {@code new Interval("2010-06-30T01:20/P1D"))} are equal.
     * <p>
     * However, when this method is passed a string with an offset,
     * the offset is directly parsed and stored.
     * As such, {@code Interval.parseWithOffset("2010-06-30T01:20+02:00/P1D")} and
     * {@code new Interval("2010-06-30T01:20+02:00/P1D"))} are NOT equal.
     * The object produced via this method has a zone of {@code DateTimeZone.forOffsetHours(2)}.
     * The object produced via the constructor has a zone of {@code DateTimeZone.getDefault()}.
     * 
     * @param str  the string to parse, not null
     * @since 2.9
     */
public static Interval parseWithOffset(String str) {
    int separator = str.indexOf('/');
    if (separator < 0) {
        throw new IllegalArgumentException("Format requires a '/' separator: " + str);
    }
    String leftStr = str.substring(0, separator);
    if (leftStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }
    String rightStr = str.substring(separator + 1);
    if (rightStr.length() <= 0) {
        throw new IllegalArgumentException("Format invalid: " + str);
    }
    DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser().withOffsetParsed();
    PeriodFormatter periodParser = ISOPeriodFormat.standard();
    DateTime start = null;
    Period period = null;
    // before slash
    char c = leftStr.charAt(0);
    if (c == 'P' || c == 'p') {
        period = periodParser.withParseType(PeriodType.standard()).parsePeriod(leftStr);
    } else {
        start = dateTimeParser.parseDateTime(leftStr);
    }
    // after slash
    c = rightStr.charAt(0);
    if (c == 'P' || c == 'p') {
        if (period != null) {
            throw new IllegalArgumentException("Interval composed of two durations: " + str);
        }
        period = periodParser.withParseType(PeriodType.standard()).parsePeriod(rightStr);
        return new Interval(start, period);
    } else {
        DateTime end = dateTimeParser.parseDateTime(rightStr);
        if (period != null) {
            return new Interval(period, end);
        } else {
            return new Interval(start, end);
        }
    }
}
Also used : PeriodFormatter(org.joda.time.format.PeriodFormatter) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) BaseInterval(org.joda.time.base.BaseInterval)

Example 10 with PeriodFormatter

use of org.joda.time.format.PeriodFormatter in project joda-time by JodaOrg.

the class TestPeriod_Basics method testToString_nullPeriodFormatter.

public void testToString_nullPeriodFormatter() {
    Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
    assertEquals("P1Y2M3W4DT5H6M7.008S", test.toString((PeriodFormatter) null));
}
Also used : PeriodFormatter(org.joda.time.format.PeriodFormatter) BasePeriod(org.joda.time.base.BasePeriod)

Aggregations

PeriodFormatter (org.joda.time.format.PeriodFormatter)22 PeriodFormatterBuilder (org.joda.time.format.PeriodFormatterBuilder)15 Period (org.joda.time.Period)13 DateTime (org.joda.time.DateTime)7 Duration (org.joda.time.Duration)4 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 RuleSetBean (org.akaza.openclinica.domain.rule.RuleSetBean)2 ExpressionObjectWrapper (org.akaza.openclinica.domain.rule.expression.ExpressionObjectWrapper)2 ExpressionProcessor (org.akaza.openclinica.domain.rule.expression.ExpressionProcessor)2 MalformedURLException (java.net.MalformedURLException)1 UnknownHostException (java.net.UnknownHostException)1 MessageFormat (java.text.MessageFormat)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 Matcher (java.util.regex.Matcher)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1