本文整理汇总了Java中org.joda.time.DateTime.plusYears方法的典型用法代码示例。如果您正苦于以下问题:Java DateTime.plusYears方法的具体用法?Java DateTime.plusYears怎么用?Java DateTime.plusYears使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.joda.time.DateTime
的用法示例。
在下文中一共展示了DateTime.plusYears方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNextCheckouts
import org.joda.time.DateTime; //导入方法依赖的package包/类
/**
* Get future checkouts
*
* @param limit
* @param offset
* @return
* @throws IOException
*/
public List<Reservation> getNextCheckouts(long limit, long offset) throws IOException {
try {
QueryBuilder<Reservation, String> builder = dao.queryBuilder();
DateTime now = new DateTime();
DateTime start = new DateTime(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth(), 0, 0).minusHours(2);
DateTime future = start.plusYears(1);
Where<Reservation, String> where = builder.where();
where.between(Reservation.DATEEND_FIELD_NAME, start.toDate(), future.toDate());
builder.orderBy(Reservation.DATEEND_FIELD_NAME, true);
builder.limit(limit);
if (offset > 0) {
builder.offset(offset);
}
List<Reservation> results = builder.query();
refresh(results);
return results;
} catch (Exception e) {
throw new IOException(e);
}
}
示例2: testTimePlus
import org.joda.time.DateTime; //导入方法依赖的package包/类
/**
* 时间加减操作,plus 负数时是向前推移
*/
@Test
public void testTimePlus() {
// 获取当前时间
DateTime dt = new DateTime();
String currentTime = dt.toString("yyyy-MM-dd HH:mm:ss");
System.out.println("currentTime: " + currentTime);
// 相对当前时间 向后5天,5天后
DateTime plus5Days = dt.plusDays(5);
String plus5DaysStr = plus5Days.toString("yyyy-MM-dd HH:mm:ss");
System.out.println("plus 5 days: " + plus5DaysStr);
// 相对当前时间 向后5个小时,5小时后
DateTime plus5Hours = dt.plusHours(5);
String plus5HoursStr = plus5Hours.toString("yyyy-MM-dd HH:mm:ss");
System.out.println("plus 5 hours: " + plus5HoursStr);
// 相对当前时间,向后5分钟,5分钟后
DateTime plus5Minutes = dt.plusMinutes(5);
String plus5MinutesStr = plus5Minutes.toString("yyyy-MM-dd HH:mm:ss");
System.out.println("plus 5 minutes: " + plus5MinutesStr);
// 相对当前时间,向前5年,5年前
DateTime plus5Years = dt.plusYears(-5);
String plus5YearsStr = plus5Years.toString("yyyy-MM-dd HH:mm:ss");
System.out.println("5 years ago: " + plus5YearsStr);
// 相对当前时间,向前5个月
DateTime plusMonths = dt.plusMonths(-5);
String plusMonthsStr = plusMonths.toString("yyyy-MM-dd HH:mm:ss");
System.out.println("5 month ago: " + plusMonthsStr);
}
示例3: parseRfc3164Time
import org.joda.time.DateTime; //导入方法依赖的package包/类
/**
* Parse the RFC3164 date format. This is trickier than it sounds because this
* format does not specify a year so we get weird edge cases at year
* boundaries. This implementation tries to "do what I mean".
* @param ts RFC3164-compatible timestamp to be parsed
* @return Typical (for Java) milliseconds since the UNIX epoch
*/
protected long parseRfc3164Time(String ts) {
DateTime now = DateTime.now();
int year = now.getYear();
ts = TWO_SPACES.matcher(ts).replaceFirst(" ");
DateTime date;
try {
date = rfc3164Format.parseDateTime(ts);
} catch (IllegalArgumentException e) {
logger.debug("rfc3164 date parse failed on (" + ts + "): invalid format", e);
return 0;
}
// rfc3164 dates are really dumb.
/*
* Some code to try and add some smarts to the year insertion as without a year in the message
* we need to make some educated guessing.
* First set the "fixed" to be the timestamp with the current year.
* If the "fixed" time is more than one month in the future then roll it back a year.
* If the "fixed" time is more than eleven months in the past then roll it forward a year.
* This gives us a 12 month rolling window (11 months in the past, 1 month in the future) of
* timestamps.
*/
if (date != null) {
DateTime fixed = date.withYear(year);
// flume clock is ahead or there is some latency, and the year rolled
if (fixed.isAfter(now) && fixed.minusMonths(1).isAfter(now)) {
fixed = date.minusYears(1);
// flume clock is behind and the year rolled
} else if (fixed.isBefore(now) && fixed.plusMonths(1).isBefore(now)) {
fixed = date.plusYears(1);
}
date = fixed;
}
if (date == null) {
return 0;
}
return date.getMillis();
}