本文整理匯總了Java中org.joda.time.Interval.toPeriod方法的典型用法代碼示例。如果您正苦於以下問題:Java Interval.toPeriod方法的具體用法?Java Interval.toPeriod怎麽用?Java Interval.toPeriod使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.joda.time.Interval
的用法示例。
在下文中一共展示了Interval.toPeriod方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: formatDuration
import org.joda.time.Interval; //導入方法依賴的package包/類
public static String formatDuration(long duration)
{
// Using Joda Time
DateTime now = new DateTime(); // Now
DateTime plus = now.plus(new Duration(duration * 1000));
// Define and calculate the interval of time
Interval interval = new Interval(now.getMillis(), plus.getMillis());
Period period = interval.toPeriod(PeriodType.time());
// Define the period formatter for pretty printing
String ampersand = " & ";
PeriodFormatter pf = new PeriodFormatterBuilder().appendHours().appendSuffix(ds("hour"), ds("hours"))
.appendSeparator(" ", ampersand).appendMinutes().appendSuffix(ds("minute"), ds("minutes"))
.appendSeparator(ampersand).appendSeconds().appendSuffix(ds("second"), ds("seconds")).toFormatter();
return pf.print(period).trim();
}
示例2: intervalTest
import org.joda.time.Interval; //導入方法依賴的package包/類
@Test
public void intervalTest() {
// 獲取當前日期到本月最後一天還剩多少天
LocalDate now = LocalDate.now();
LocalDate lastDayOfMonth = LocalDate.now().dayOfMonth().withMaximumValue();
System.out.println(Days.daysBetween(now, lastDayOfMonth).getDays());
System.out.println(DateTime.now().toString(DEFAULT_DATE_FORMATTER));
System.out.println(DateTime.now().dayOfMonth().withMaximumValue().toString(DEFAULT_DATE_FORMATTER));
Interval interval = new Interval(DateTime.now(), DateTime.now().dayOfMonth().withMaximumValue());
Period p = interval.toPeriod();
System.out.println(interval.toDuration().getStandardDays());
System.out.format("時間相差:%s 年, %s 月, %s 天, %s 時, %s 分, %s 秒",
p.getYears(), p.getMonths(), p.getDays(), p.getHours(), p.getMinutes(), p.getSeconds());
}
示例3: calculateDuration
import org.joda.time.Interval; //導入方法依賴的package包/類
/**
* Method that will calculate the duration between two given times in string format
*
* @param fromDateTime the from time
* @param toDateTime the to time
* @return the duration
*/
public String calculateDuration(String fromDateTime, String toDateTime) {
DateTime from = fillDateFormatter.parseDateTime(fromDateTime);
DateTime to = fillDateFormatter.parseDateTime(toDateTime);
Interval interval = new Interval(from, to);
Period period = interval.toPeriod();
return durationFormatter.print(period);
}
示例4: getTimesInInterval
import org.joda.time.Interval; //導入方法依賴的package包/類
/**
* Returns list of {@link DateTime} that belong to specified {@link OrgDateTime}
* and are within specified time interval.
*
* @param orgDateTime {@link OrgDateTime}
* @param fromTime Inclusive
* @param beforeTime Exclusive. Can be null in which case limit has to be specified
* @param useRepeater Use repeater from {@link OrgDateTime}
* @param limit When {@code orgTime} has a repeater, limit the number of results to this number
* @return List of times within specified interval
*/
public static List<DateTime> getTimesInInterval(
OrgDateTime orgDateTime,
ReadableInstant fromTime,
ReadableInstant beforeTime,
boolean useRepeater,
int limit) {
DateTime time = new DateTime(orgDateTime.getCalendar());
List<DateTime> result = new ArrayList<>();
/* If Org time has no repeater or it should be ignored,
* just check if time part is within the interval.
*/
if (!useRepeater || !orgDateTime.hasRepeater() || orgDateTime.getRepeater().getValue() == 0) {
if (!time.isBefore(fromTime) && (beforeTime == null || time.isBefore(beforeTime))) {
result.add(time);
}
} else {
if (beforeTime == null && limit == 0) {
throw new IllegalArgumentException("When beforeTime is not specified, limit is mandatory");
}
if (limit > MAX_INSTANTS_IN_INTERVAL) {
limit = MAX_INSTANTS_IN_INTERVAL;
}
OrgRepeater repeater = orgDateTime.getRepeater();
DateTime curr = time;
if (time.isBefore(fromTime)) {
/* How many periods between time and start of interval. */
Interval gap = new Interval(time, fromTime);
Period intervalPeriod = gap.toPeriod(OrgDateTimeUtils.getPeriodType(repeater.getUnit()));
int units = intervalPeriod.getValue(0);
/* How many units to add to get just after the start of interval.
* This is multiples of repeater's value.
*/
int repeatTimes = units / repeater.getValue();
int addUnits = repeater.getValue() * (repeatTimes + 1);
/* Time just after the interval we are interested in. */
curr = time.withFieldAdded(OrgDateTimeUtils.getDurationFieldType(repeater.getUnit()), addUnits);
}
while (beforeTime == null || curr.isBefore(beforeTime)) {
result.add(curr);
/* Check if limit has been reached. */
if (limit > 0 && result.size() >= limit) {
break;
}
/* Shift. */
curr = curr.withFieldAdded(
OrgDateTimeUtils.getDurationFieldType(repeater.getUnit()),
repeater.getValue()
);
}
}
return result;
}