/** * Normalizes this period using standard rules, assuming a 12 month year, * 7 day week, 24 hour day, 60 minute hour and 60 second minute. * <p> * This method allows you to normalize a period. * However to achieve this it makes the assumption that all years are * 12 months, all weeks are 7 days, all days are 24 hours, * all hours are 60 minutes and all minutes are 60 seconds. This is not * true when daylight savings time is considered, and may also not be true * for some chronologies. However, it is included as it is a useful operation * for many applications and business rules. * <p> * If the period contains years or months, then the months will be * normalized to be between 0 and 11. The days field and below will be * normalized as necessary, however this will not overflow into the months * field. Thus a period of 1 year 15 months will normalize to 2 years 3 months. * But a period of 1 month 40 days will remain as 1 month 40 days. * <p> * The result will always have a <code>PeriodType</code> of standard, thus * days will be grouped into weeks. * * @return a normalized period equivalent to this period * @throws ArithmeticException if any field is too large to be represented * @since 1.5 */ public Period normalizedStandard() { return normalizedStandard(PeriodType.standard()); }
/** * Normalizes this period using standard rules, assuming a 12 month year, * 7 day week, 24 hour day, 60 minute hour and 60 second minute. * <p> * This method allows you to normalize a period. * However to achieve this it makes the assumption that all years are * 12 months, all weeks are 7 days, all days are 24 hours, * all hours are 60 minutes and all minutes are 60 seconds. This is not * true when daylight savings time is considered, and may also not be true * for some chronologies. However, it is included as it is a useful operation * for many applications and business rules. * <p> * If the period contains years or months, then the months will be * normalized to be between 0 and 11. The days field and below will be * normalized as necessary, however this will not overflow into the months * field. Thus a period of 1 year 15 months will normalize to 2 years 3 months. * But a period of 1 month 40 days will remain as 1 month 40 days. * <p> * The result will always have a <code>PeriodType</code> of standard, thus * days will be grouped into weeks. * * @return a normalized period equivalent to this period * @throws ArithmeticException if any field is too large to be represented * @since 1.5 */ public Period normalizedStandard() { return normalizedStandard(PeriodType.standard()); }
public static void main(String[] args) { PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder() .appendDays() .appendSuffix(" day", " days") .appendSeparator(" and ") .appendMinutes() .appendSuffix(" minute", " minutes") .appendSeparator(" and ") .appendSeconds() .appendSuffix(" second", " seconds") .toFormatter(); Period period = new Period(72, 24, 12, 0); System.out.println(daysHoursMinutes.print(period)); System.out.println(daysHoursMinutes.print(period.normalizedStandard())); }
final Period rotationPeriod = config.rotationPeriod().normalizedStandard(); final DateTime now = Tools.nowUTC();
public static String humanDuration(final Duration dur) { final Period p = dur.toPeriod().normalizedStandard(); if (dur.getStandardSeconds() == 0) { return "0 seconds"; } else if (dur.getStandardSeconds() < 60) { return format("%d second%s", p.getSeconds(), p.getSeconds() > 1 ? "s" : ""); } else if (dur.getStandardMinutes() < 60) { return format("%d minute%s", p.getMinutes(), p.getMinutes() > 1 ? "s" : ""); } else if (dur.getStandardHours() < 24) { return format("%d hour%s", p.getHours(), p.getHours() > 1 ? "s" : ""); } else { return format("%d day%s", dur.getStandardDays(), dur.getStandardDays() > 1 ? "s" : ""); } }
@Override public String getAsString() { if (isNull()) { return "null"; } switch (valueType()) { case BYTES: return AccessorUtilities.bytesToString(getBytes()); case DOUBLE: return Double.toString(getDouble()); case INTEGER: return Integer.toString(getInt()); case LONG: return Long.toString(getLong()); case STRING: return "\"" + getString() + "\""; case DECIMAL: return getDecimal().toPlainString(); case PERIOD: return getPeriod().normalizedStandard().toString(); default: throw new IllegalArgumentException("Unsupported type " + valueType()); } } }
final Period normalized = period.normalizedStandard(); int years = normalized.getYears(); int months = normalized.getMonths();
public static Duration difference(Time t1, Time t2) { // TODO: Difference does not work between days of the week // Get duration from this t1 to t2 if (t1 == null || t2 == null) return null; Instant i1 = t1.getJodaTimeInstant(); Instant i2 = t2.getJodaTimeInstant(); if (i1 == null || i2 == null) return null; Duration d = new DurationWithMillis(i2.getMillis() - i1.getMillis()); Duration g1 = t1.getGranularity(); Duration g2 = t2.getGranularity(); Duration g = Duration.max(g1, g2); if (g != null) { Period p = g.getJodaTimePeriod(); p = p.normalizedStandard(); Period p2 = JodaTimeUtils.discardMoreSpecificFields(d.getJodaTimePeriod(), p.getFieldType(p.size() - 1), i1.getChronology()); return new DurationWithFields(p2); } else { return d; } }
/** * Normalizes this period using standard rules, assuming a 12 month year, * 7 day week, 24 hour day, 60 minute hour and 60 second minute. * <p> * This method allows you to normalize a period. * However to achieve this it makes the assumption that all years are * 12 months, all weeks are 7 days, all days are 24 hours, * all hours are 60 minutes and all minutes are 60 seconds. This is not * true when daylight savings time is considered, and may also not be true * for some chronologies. However, it is included as it is a useful operation * for many applications and business rules. * <p> * If the period contains years or months, then the months will be * normalized to be between 0 and 11. The days field and below will be * normalized as necessary, however this will not overflow into the months * field. Thus a period of 1 year 15 months will normalize to 2 years 3 months. * But a period of 1 month 40 days will remain as 1 month 40 days. * <p> * The result will always have a <code>PeriodType</code> of standard, thus * days will be grouped into weeks. * * @return a normalized period equivalent to this period * @throws ArithmeticException if any field is too large to be represented * @since 1.5 */ public Period normalizedStandard() { return normalizedStandard(PeriodType.standard()); }
import org.joda.time.*; import org.joda.time.format.*; public class Test { public static void main(String[] args) { // 2 days, 4 hours, 4 minutes Period period = new Period(0, 0, 0, 2, 4, 4, 0, 0); // Actually we're fine with seconds etc as well Period hoursAndMinutes = period.normalizedStandard(PeriodType.time()); PeriodFormatter formatter = new PeriodFormatterBuilder() .appendHours() .appendSuffix(" hour", " hours") .appendMinutes() .appendSuffix(" minute", " minutes") .toFormatter(); System.out.println(formatter.print(hoursAndMinutes)); } }
p = p.normalizedStandard(periodType); } else {
Period period = new Period(); // prints 00:00:00 System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds())); period = period.plusSeconds(60 * 60 * 12); // prints 00:00:43200 System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds())); period = period.normalizedStandard(); // prints 12:00:00 System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
/** * Creates a new Period that can be used properly with the "Uptime" styles * returned by this utility class. This period normalizes the internal * fields to only include days, hours, minutes, and seconds. * @param durationInMillis The length of the duration in milliseconds * @return A new normalized Period */ public static Period createDayHourMinSecPeriod(long durationInMillis) { return new Period(durationInMillis).normalizedStandard(dayHourMinSecPeriodType); }
import org.joda.time.Period; class Normalize { public static void main(String[] args) { Period period = new Period(1325965615539L); System.out.println(period); System.out.println(period.normalizedStandard()); } } // outputs PT368323H46M55.539S P2192W2DT19H46M55.539S
LocalDate start_a = new LocalDate( "1993-08-16" ); LocalDate stop_a = new LocalDate( "2014-09-09" ); Period period_a = new Period( start_a , stop_a ); LocalDate start_b = new LocalDate( "1995-02-14" ); LocalDate stop_b = new LocalDate( "2001-05-18" ); Period period_b = new Period( start_b , stop_b ); Period periodCombined = period_a.plus( period_b ); Period periodCombinedNormalized = periodCombined.normalizedStandard(); // Assumes that all years are 12 months, all weeks are 7 days, all days are 24 hours, all hours are 60 minutes and all minutes are 60 seconds. Not true for Daylight Saving Time and other anomalies.
DateTimeZone timeZone = DateTimeZone.forID( "America/Montreal" ); // or DateTimeZone.UTC DateTime start_a = new DateTime( "1993-08-16" , timeZone ); // Could call `.withTimeAtStartOfDay()` but that is the default behavior for date-only string. DateTime stop_a = new DateTime( "2014-09-09" , timeZone ); Interval interval_a = new Interval( start_a , stop_a ); Period period_a = interval_a.toPeriod(); DateTime start_b = new DateTime( "1995-02-14" , timeZone ); DateTime stop_b = new DateTime( "2001-05-18" , timeZone ); Interval interval_b = new Interval( start_b , stop_b ); Period period_b = interval_b.toPeriod(); Period periodCombined = period_a.plus( period_b ); Period periodCombinedNormalized = periodCombined.normalizedStandard(); // Assumes that all years are 12 months, all weeks are 7 days, all days are 24 hours, all hours are 60 minutes and all minutes are 60 seconds. Not true for Daylight Saving Time and other anomalies.
private void logCacheLoadTime(long startTime) { Period cacheLoadPeriod = new Period(startTime, System.currentTimeMillis()); LOGGER.info("Cache load finished in {} ({} seconds)", PeriodFormat.getDefault().print(cacheLoadPeriod.normalizedStandard()), cacheLoadPeriod.toStandardSeconds()); }
private static void printScriptRunTime(DateTime startTime) { DateTime endTime = new DateTime(); Duration duration = new Duration(startTime, endTime); Period period = duration.toPeriod().normalizedStandard(PeriodType.time()); log.info("Pig script completed in " + PeriodFormat.getDefault().print(period) + " (" + duration.getMillis() + " ms)"); }
/** {@inheritDoc} */ @Override public String marshal(final Duration v) { if (v.equals(Duration.ZERO)) { return "0"; } return DEFAULT_PERIOD_FORMATTER.print(v.toPeriod().normalizedStandard()); }
String[] input = { "01:10", "01:10", "01:10", "01:10", "01:10", "01:10" }; PeriodFormatter pf = new PeriodFormatterBuilder() .minimumPrintedDigits(2).printZeroAlways() .appendHours().appendLiteral(":").appendMinutes().toFormatter(); Period period = Period.ZERO; for (String s : input) { period = period.plus(pf.parsePeriod(s)); } System.out.println("duration=" + pf.print(period.normalizedStandard())); // output: duration=07:00