本文整理汇总了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;
}