org.joda.time.format.DateTimeFormatter.withDefaultYear()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.3k)|赞(0)|评价(0)|浏览(134)

本文整理了Java中org.joda.time.format.DateTimeFormatter.withDefaultYear()方法的一些代码示例,展示了DateTimeFormatter.withDefaultYear()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。DateTimeFormatter.withDefaultYear()方法的具体详情如下:
包路径:org.joda.time.format.DateTimeFormatter
类名称:DateTimeFormatter
方法名:withDefaultYear

DateTimeFormatter.withDefaultYear介绍

[英]Returns a new formatter that will use the specified default year.

The default year is used when parsing in the case where there is a month or a day but not a year. Specifically, it is used if there is a field parsed with a duration between the length of a month and the length of a day inclusive.

This value is typically used to move the year from 1970 to a leap year to enable February 29th to be parsed. Unless customised, the year 2000 is used.

This setting has no effect when printing.
[中]返回将使用指定默认年份的新格式化程序。
如果有一个月或一天而不是一年,则在解析时使用默认年份。具体来说,如果解析的字段的持续时间介于一个月和一天(含一天)之间,则使用该字段。
此值通常用于将年份从1970年移动到闰年,以便能够解析2月29日。除非定制,否则使用2000年。
打印时,此设置无效。

代码示例

代码示例来源:origin: Graylog2/graylog2-server

@Override
  @Nullable
  public Object convert(@Nullable String value) {
    if (isNullOrEmpty(value)) {
      return null;
    }

    LOG.debug("Trying to parse date <{}> with pattern <{}>, locale <{}>, and timezone <{}>.", value, dateFormat, locale, timeZone);
    final DateTimeFormatter formatter;
    if (containsTimeZone) {
      formatter = DateTimeFormat
          .forPattern(dateFormat)
          .withDefaultYear(YearMonth.now(timeZone).getYear())
          .withLocale(locale);
    } else {
      formatter = DateTimeFormat
          .forPattern(dateFormat)
          .withDefaultYear(YearMonth.now(timeZone).getYear())
          .withLocale(locale)
          .withZone(timeZone);
    }

    return DateTime.parse(value, formatter);
  }
}

代码示例来源:origin: apache/hive

public TimestampParser(String[] formatStrings) {
 this.formatStrings = formatStrings;
 // create formatter that includes all of the input patterns
 if (formatStrings != null && formatStrings.length > 0) {
  DateTimeParser[] parsers = new DateTimeParser[formatStrings.length];
  for (int idx = 0; idx < formatStrings.length; ++idx) {
   String formatString = formatStrings[idx];
   if (formatString.equalsIgnoreCase(millisFormatString)) {
    // Use milliseconds parser if pattern matches our special-case millis pattern string
    parsers[idx] = new MillisDateFormatParser();
   } else {
    parsers[idx] = DateTimeFormat.forPattern(formatString).getParser();
   }
  }
  fmt = new DateTimeFormatterBuilder()
      .append(null, parsers)
      .toFormatter()
      .withDefaultYear(1970);
 }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

public JodaDateFormatter(String pattern, DateTimeFormatter parser, DateTimeFormatter printer) {
  this.pattern = pattern;
  this.printer = printer.withDefaultYear(1970);
  this.parser = parser.withDefaultYear(1970);
}

代码示例来源:origin: tuplejump/stargate-core

public FormatDateTimeFormatter(String format, DateTimeFormatter parser, DateTimeFormatter printer, Locale locale) {
  this.format = format;
  this.locale = locale;
  this.printer = locale == null ? printer.withDefaultYear(1970) : printer.withLocale(locale).withDefaultYear(1970);
  this.parser = locale == null ? parser.withDefaultYear(1970) : parser.withLocale(locale).withDefaultYear(1970);
}

代码示例来源:origin: apache/servicemix-bundles

public FormatDateTimeFormatter(String format, DateTimeFormatter parser, DateTimeFormatter printer, Locale locale) {
  this.format = format;
  this.locale = Objects.requireNonNull(locale, "A locale is required as JODA otherwise uses the default locale");
  this.printer = printer.withLocale(locale).withDefaultYear(1970);
  this.parser = parser.withLocale(locale).withDefaultYear(1970);
}

代码示例来源:origin: com.strapdata.elasticsearch/elasticsearch

public FormatDateTimeFormatter(String format, DateTimeFormatter parser, DateTimeFormatter printer, Locale locale) {
  this.format = format;
  this.locale = Objects.requireNonNull(locale, "A locale is required as JODA otherwise uses the default locale");
  this.printer = printer.withLocale(locale).withDefaultYear(1970);
  this.parser = parser.withLocale(locale).withDefaultYear(1970);
}

代码示例来源:origin: org.codelibs.elasticsearch.module/ingest-common

@Override
  Function<String, DateTime> getFunction(String format, DateTimeZone timezone, Locale locale) {
    DateTimeFormatter parser = DateTimeFormat.forPattern(format).withZone(timezone).withLocale(locale);
    return text -> parser.withDefaultYear((new DateTime(DateTimeZone.UTC)).getYear()).parseDateTime(text);
  }
};

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.elasticsearch

public FormatDateTimeFormatter(String format, DateTimeFormatter parser, DateTimeFormatter printer, Locale locale) {
  this.format = format;
  this.locale = Objects.requireNonNull(locale, "A locale is required as JODA otherwise uses the default locale");
  this.printer = printer.withLocale(locale).withDefaultYear(1970);
  this.parser = parser.withLocale(locale).withDefaultYear(1970);
}

代码示例来源:origin: org.codelibs/elasticsearch-querybuilders

public FormatDateTimeFormatter(String format, DateTimeFormatter parser, DateTimeFormatter printer, Locale locale) {
  this.format = format;
  this.locale = Objects.requireNonNull(locale, "A locale is required as JODA otherwise uses the default locale");
  this.printer = printer.withLocale(locale).withDefaultYear(1970);
  this.parser = parser.withLocale(locale).withDefaultYear(1970);
}

代码示例来源:origin: micromata/projectforge

protected DateTimeFormatter getDateTimeFormatter(final String pattern, final Locale locale)
{
 return DateTimeFormat.forPattern(pattern).withLocale(locale).withZone(timeZone).withPivotYear(PIVOT_YEAR).withDefaultYear(currentYear);
}

代码示例来源:origin: org.graylog2/graylog2-server

@Override
  @Nullable
  public Object convert(@Nullable String value) {
    if (isNullOrEmpty(value)) {
      return null;
    }

    LOG.debug("Trying to parse date <{}> with pattern <{}> and timezone <{}>.", value, dateFormat, timeZone);
    final DateTimeFormatter formatter = DateTimeFormat
        .forPattern(dateFormat)
        .withLocale(locale)
        .withDefaultYear(YearMonth.now(timeZone).getYear())
        .withZone(timeZone);
    return DateTime.parse(value, formatter);
  }
}

代码示例来源:origin: com.github.mkolisnyk/cucumber-report-generator

public static String transformPathString(String input) {
  String output = input;
  String datePattern = "DATE\\(([^)]+)\\)";
  String varPattern = "\\$\\{(.*)}";
  if (output == null) {
    return null;
  }
  while (output.matches("(.*)" + datePattern + "(.*)")) {
    String format = output.split("DATE\\(")[1].split("\\)")[0].trim();
    String value = DateTimeFormat.forPattern(format)
      .withDefaultYear(new DateTime().get(DateTimeFieldType.yearOfEra())).withLocale(Locale.US)
      .print(new DateTime());
    output = output.replaceFirst(datePattern, value);
  }
  while (output.matches("(.*)" + varPattern + "(.*)")) {
    String name = output.split("\\$\\{")[1].split("}")[0].trim();
    if (StringUtils.isNotBlank(System.getProperty(name))) {
      output = output.replaceFirst(varPattern, System.getProperty(name));
    } else {
      output = output.replaceFirst(varPattern, System.getenv(name));
    }
  }
  return output;
}
public static String replaceHtmlEntitiesWithCodes(String input) throws IOException {

代码示例来源:origin: org.apache.hive/hive-common

public TimestampParser(String[] formatStrings) {
 this.formatStrings = formatStrings;
 // create formatter that includes all of the input patterns
 if (formatStrings != null && formatStrings.length > 0) {
  DateTimeParser[] parsers = new DateTimeParser[formatStrings.length];
  for (int idx = 0; idx < formatStrings.length; ++idx) {
   String formatString = formatStrings[idx];
   if (formatString.equalsIgnoreCase(millisFormatString)) {
    // Use milliseconds parser if pattern matches our special-case millis pattern string
    parsers[idx] = new MillisDateFormatParser();
   } else {
    parsers[idx] = DateTimeFormat.forPattern(formatString).getParser();
   }
  }
  fmt = new DateTimeFormatterBuilder()
      .append(null, parsers)
      .toFormatter()
      .withDefaultYear(1970);
 }
}

相关文章