All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.util.time.LocalDateUtils Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
package org.opentripplanner.util.time;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeParseException;

public class LocalDateUtils {

  private LocalDateUtils() {}

  /**
   * Parse a string as a local date or a duration relative to the given local date.
   * 

* If today is {@code 2022-11-30}, then: *

    *
  • {@code 2022-01-15 is 2022-01-15} (absolute)
  • *
  • {@code "0d" or "P0d" is 2022-11-30}
  • *
  • {@code "3d" or "P3d" is 2022-12-03}
  • *
  • {@code "-P1Y1D" is 2021-11-29} (minus one year and one day)
  • *
  • {@code "P1Y-1D" is 2023-11-29} (plus one year, minus one day)
  • *
* * The Period is parsed using {@link Period#parse(CharSequence)}. */ public static LocalDate asRelativeLocalDate(String text, LocalDate timeZero) throws DateTimeParseException { if (text.startsWith("-") || text.startsWith("P")) { return timeZero.plus(Period.parse(text)); } else { return LocalDate.parse(text); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy