当前位置: 首页>>代码示例>>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;未经允许,请勿转载。