/** * Uses the specified formatter to convert this period to a String. * * @param formatter the formatter to use, null means use <code>toString()</code>. * @return the formatted string * @since 1.5 */ public String toString(PeriodFormatter formatter) { if (formatter == null) { return toString(); } return formatter.print(this); }
/** * Converts milliseconds into human readable duration string. For ex, input of 86400000L would * return "1d". * * @param millis the value in milliseconds to be converted * @return the corresponding human readable string or empty string if value cannot be converted */ public static String convertMillisToPeriod(Long millis) { String periodStr = null; if (millis != null) { Period p = new Period(new Duration(millis)); periodStr = PERIOD_FORMATTER.print(p); } return periodStr; } }
/** * Uses the specified formatter to convert this period to a String. * * @param formatter the formatter to use, null means use <code>toString()</code>. * @return the formatted string * @since 1.5 */ public String toString(PeriodFormatter formatter) { if (formatter == null) { return toString(); } return formatter.print(this); }
public String duration(PeriodFormatter formatter) { return formatter.print(duration.toPeriod()); }
DateTime myBirthDate = new DateTime(1978, 3, 26, 12, 35, 0, 0); DateTime now = new DateTime(); Period period = new Period(myBirthDate, now); PeriodFormatter formatter = new PeriodFormatterBuilder() .appendSeconds().appendSuffix(" seconds ago\n") .appendMinutes().appendSuffix(" minutes ago\n") .appendHours().appendSuffix(" hours ago\n") .appendDays().appendSuffix(" days ago\n") .appendWeeks().appendSuffix(" weeks ago\n") .appendMonths().appendSuffix(" months ago\n") .appendYears().appendSuffix(" years ago\n") .printZeroNever() .toFormatter(); String elapsed = formatter.print(period); System.out.println(elapsed);
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())); }
DateTime myBirthDate = new DateTime(1978, 3, 26, 12, 35, 0, 0); DateTime now = new DateTime(); Period period = new Period(myBirthDate, now); PeriodFormatter formatter = new PeriodFormatterBuilder() .appendYears().appendSuffix(" year, ", " years, ") .appendMonths().appendSuffix(" month, ", " months, ") .appendWeeks().appendSuffix(" week, ", " weeks, ") .appendDays().appendSuffix(" day, ", " days, ") .appendHours().appendSuffix(" hour, ", " hours, ") .appendMinutes().appendSuffix(" minute, ", " minutes, ") .appendSeconds().appendSuffix(" second", " seconds") .printZeroNever() .toFormatter(); String elapsed = formatter.print(period); System.out.println(elapsed + " ago");
DateTime dt = new DateTime(); DateTime twoHoursLater = dt.plusHours(2).plusMinutes(10).plusSeconds(5); Period period = new Period(dt, twoHoursLater); PeriodFormatter HHMMSSFormater = new PeriodFormatterBuilder() .printZeroAlways() .minimumPrintedDigits(2) .appendHours().appendSeparator("-") .appendMinutes().appendSeparator("-") .appendSeconds() .toFormatter(); // produce thread-safe formatter System.out.println(HHMMSSFormater.print(period));
/** * Gets the value as a String in the ISO8601 duration format. * <p> * For example, "PT6H3M7S" represents 6 hours, 3 minutes, 7 seconds. * <p> * For more control over the output, see * {@link org.joda.time.format.PeriodFormatterBuilder PeriodFormatterBuilder}. * * @return the value as an ISO8601 string */ @ToString public String toString() { return ISOPeriodFormat.standard().print(this); }
//import org.joda.time.format.PeriodFormatter; //import org.joda.time.format.PeriodFormatterBuilder; //import org.joda.time.Duration; Duration duration = new Duration(123456); // in milliseconds PeriodFormatter formatter = new PeriodFormatterBuilder() .appendDays() .appendSuffix("d") .appendHours() .appendSuffix("h") .appendMinutes() .appendSuffix("m") .appendSeconds() .appendSuffix("s") .toFormatter(); String formatted = formatter.print(duration.toPeriod()); System.out.println(formatted);
/** * Gets the value as a String in the ISO8601 duration format. * <p> * For example, "PT6H3M7S" represents 6 hours, 3 minutes, 7 seconds. * <p> * For more control over the output, see * {@link org.joda.time.format.PeriodFormatterBuilder PeriodFormatterBuilder}. * * @return the value as an ISO8601 string */ @ToString public String toString() { return ISOPeriodFormat.standard().print(this); }
data.add(Arrays.asList("End Time", dateTimeFormatter.print(jobExecutionInfo.getEndTime()))); data.add(Arrays.asList("Duration", jobExecutionInfo.getState() == JobStateEnum.COMMITTED ? periodFormatter .print(new Period(jobExecutionInfo.getDuration().longValue())) : "-")); data.add(Arrays.asList("Tracking URL", jobExecutionInfo.getTrackingUrl())); data.add(Arrays.asList("Launcher Type", jobExecutionInfo.getLauncherType().name()));
DateTime startDate = DateTime.now(); // now() : since Joda Time 2.0 DateTime endDate = new DateTime(2011, 12, 25, 0, 0); Period period = new Period(startDate, endDate, PeriodType.dayTime()); PeriodFormatter formatter = new PeriodFormatterBuilder() .appendDays().appendSuffix(" day ", " days ") .appendHours().appendSuffix(" hour ", " hours ") .appendMinutes().appendSuffix(" minute ", " minutes ") .appendSeconds().appendSuffix(" second ", " seconds ") .toFormatter(); System.out.println(formatter.print(period));
@Override public void serialize(ReadablePeriod value, JsonGenerator gen, SerializerProvider provider) throws IOException { gen.writeString(_format.createFormatter(provider).print(value)); }
/** * Uses the specified formatter to convert this period to a String. * * @param formatter the formatter to use, null means use <code>toString()</code>. * @return the formatted string * @since 1.5 */ public String toString(PeriodFormatter formatter) { if (formatter == null) { return toString(); } return formatter.print(this); }
String string1 = "Oct 15, 2012 1:07:13 PM"; String string2 = "Oct 23, 2012 03:43:34 PM"; DateTimeFormatter dtf = DateTimeFormat.forPattern("MMM d, yyyy h:mm:ss a").withLocale(Locale.ENGLISH); DateTime dateTime1 = dtf.parseDateTime(string1); DateTime dateTime2 = dtf.parseDateTime(string2); Period period = new Period(dateTime1, dateTime2); PeriodFormatter formatter = new PeriodFormatterBuilder() .appendYears().appendSuffix(" years ") .appendMonths().appendSuffix(" months ") .appendWeeks().appendSuffix(" weeks ") .appendDays().appendSuffix(" days ") .appendHours().appendSuffix(" hours ") .appendMinutes().appendSuffix(" minutes ") .appendSeconds().appendSuffix(" seconds ") .printZeroNever() .toFormatter(); String elapsed = formatter.print(period); System.out.println(elapsed);
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)); } }
/** * Gets the value as a String in the ISO8601 duration format. * <p> * For example, "P6H3M7S" represents 6 hours, 3 minutes, 7 seconds. * <p> * For more control over the output, see * {@link org.joda.time.format.PeriodFormatterBuilder PeriodFormatterBuilder}. * * @return the value as an ISO8601 string */ @ToString public String toString() { return ISOPeriodFormat.standard().print(this); }
this.periodFormatter.print(new Duration(this.startTime, System.currentTimeMillis()).toPeriod()); String timeSpentOnTopicMsg = String.format("Time spent on topic %s:%d = %s", key.getTopic(), key.getPartition(), timeSpentOnPartition); if (this.numRecordsReadForCurrentPartition != 0) { String timeSpentOnPartition = this.periodFormatter.print(new Duration(this.startTime, System.currentTimeMillis()).toPeriod()); log.info("Time spent on this partition = " + timeSpentOnPartition); log.info("Num of records read for this partition = " + this.numRecordsReadForCurrentPartition);
/** {@inheritDoc} */ @Override public String marshal(final Duration v) { if (v.equals(Duration.ZERO)) { return "0"; } return DEFAULT_PERIOD_FORMATTER.print(v.toPeriod().normalizedStandard()); }