本文整理匯總了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();
}