當前位置: 首頁>>代碼示例>>Java>>正文


Java DateTime.withYear方法代碼示例

本文整理匯總了Java中org.joda.time.DateTime.withYear方法的典型用法代碼示例。如果您正苦於以下問題:Java DateTime.withYear方法的具體用法?Java DateTime.withYear怎麽用?Java DateTime.withYear使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.joda.time.DateTime的用法示例。


在下文中一共展示了DateTime.withYear方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: 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();
}
 
開發者ID:moueimei,項目名稱:flume-release-1.7.0,代碼行數:52,代碼來源:SyslogParser.java


注:本文中的org.joda.time.DateTime.withYear方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。