001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.common.parsers.date;
015
016
017import java.time.format.DateTimeFormatter;
018import java.time.format.DateTimeFormatterBuilder;
019import java.time.format.ResolverStyle;
020import java.time.format.SignStyle;
021import java.time.temporal.ChronoField;
022
023/**
024 * Factories for date parsing related instances.
025 */
026public class DateParsers {
027
028  /**
029   * {@link DateTimeFormatter} for ISO Year-MonthOfYear-DayOfMonth (4 digits year)
030   * This formatter does NOT handle time and timezone.
031   */
032  public static final DateTimeFormatter ISO_LOCAL_DATE =
033          new DateTimeFormatterBuilder()
034                  .appendValue(ChronoField.YEAR, 4, 4, SignStyle.NEVER)
035                  .appendLiteral('-')
036                  .appendValue(ChronoField.MONTH_OF_YEAR, 1, 2, SignStyle.NEVER)
037                  .appendLiteral('-')
038                  .appendValue(ChronoField.DAY_OF_MONTH, 1, 2, SignStyle.NEVER)
039                  .toFormatter().withResolverStyle(ResolverStyle.STRICT);
040
041  /**
042   * {@link DateTimeFormatter} for ISO Year (4 digits)
043   */
044  public static final DateTimeFormatter ISO_YEAR =
045          new DateTimeFormatterBuilder()
046                  .appendValue(ChronoField.YEAR, 4, 4, SignStyle.NEVER)
047                  .toFormatter().withResolverStyle(ResolverStyle.STRICT);
048
049  /**
050   * {@link DateTimeFormatter} for ISO Year-MonthOfYear (4 digits year)
051   */
052  public static final DateTimeFormatter ISO_YEAR_MONTH =
053          new DateTimeFormatterBuilder()
054                  .appendValue(ChronoField.YEAR, 4, 4, SignStyle.NEVER)
055                  .appendLiteral('-')
056                  .appendValue(ChronoField.MONTH_OF_YEAR, 1, 2, SignStyle.NEVER)
057                  .toFormatter().withResolverStyle(ResolverStyle.STRICT);
058
059
060//  public static final DateTimeMultiParser ISO_PARSER = DateTimeParserBuilder.newMultiParserListBuilder()
061//          .appendDateTimeFormatter(ISO_LOCAL_DATE, DateComponentOrdering.YMD, 8)
062//          .appendDateTimeFormatter(ISO_YEAR_MONTH, DateComponentOrdering.YM, 6)
063//          .appendDateTimeFormatter(ISO_YEAR, DateComponentOrdering.Y, 4)
064//          .build();
065
066  /**
067   * Return a pre-configured {@link TemporalParser} instance.
068   */
069  public static TemporalParser defaultTemporalParser() {
070    return new TextDateParser();
071  }
072
073  /**
074   * Get a new instance of the default implementation of TemporalParser that handles
075   * numerical dates.
076   */
077  public static TemporalParser defaultNumericalDateParser() {
078    return ThreeTenNumericalDateParser.newInstance();
079  }
080
081}