List of usage examples for org.joda.time.format ISOPeriodFormat alternateExtended
public static PeriodFormatter alternateExtended()
From source file:colossal.pipe.BaseOptions.java
License:Apache License
private Period parseDuration() { PeriodFormatter[] toTry = { PeriodFormat.getDefault(), ISOPeriodFormat.standard(), ISOPeriodFormat.alternate(), ISOPeriodFormat.alternateExtended(), ISOPeriodFormat.alternateExtendedWithWeeks(), ISOPeriodFormat.alternateWithWeeks() }; for (PeriodFormatter f : toTry) { try {//from w w w .j av a 2s .c o m return f.parsePeriod(duration); } catch (IllegalArgumentException iae) { // skip to next } } throw new IllegalArgumentException("Can't parse: " + duration); }
From source file:com.ethercis.servicemanager.common.IsoDateJoda.java
License:Apache License
/** * Calculate the difference of given millis. * @param diffMillis The elapsed time/*from www .ja va 2s . co m*/ * @param trimDateIfPossible true: * <pre> * 3000->00:00:03 * 380000->00:06:20 * 5692439078->1581:13:59.078 * </pre> * false: * <pre> * 3000->P0000-00-00T00:00:03 * 5692439078->P0000-00-00T1581:13:59.078 * </pre> * * @return The ISO 8601 Period like "P3Y6M4DT12H30M17S" */ public static String getDifference(long diffMillis, boolean trimDateIfPossible) { if (diffMillis == 0) return ""; Period period = new Period(diffMillis); /* PeriodFormatter myFormatter = new PeriodFormatterBuilder() .printZeroAlways() .appendYears() .appendSuffix(" year", " years") .appendSeparator(" and ") .appendMonths() .appendSuffix(" month", " months") .toFormatter(); */ /*if (true) */ { // 3000->P0000-00-00T00:00:03 // 5692439078->P0000-00-00T1581:13:59.078 PeriodFormatter formatter = ISOPeriodFormat.alternateExtended(); String periodStr = formatter.print(period); if (trimDateIfPossible) { if (periodStr.startsWith("P0000-00-00T")) periodStr = periodStr.substring("P0000-00-00T".length()); } return periodStr; } /* else { // 3000->PT3S // 5692439078->PT1581H13M59.078S return period.toString(); } */ }