TimeProvider.java

  1. /*
  2.  * Copyright 2019 Heiko Scherrer
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.openwms.wms.api;

  17. import java.time.Instant;
  18. import java.time.LocalDateTime;
  19. import java.time.ZoneId;
  20. import java.time.ZonedDateTime;
  21. import java.util.Date;

  22. /**
  23.  * A TimeProvider is able to provide dates and times.
  24.  *
  25.  * @author Heiko Scherrer
  26.  */
  27. public interface TimeProvider {

  28.     /** The format pattern for all date-time with milliseconds and timezone types. */
  29.     String DATE_TIME_MILLIS_WITH_TIMEZONE = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
  30.     /** The format pattern for all date-time with timezone types. */
  31.     String DATE_TIME_WITH_TIMEZONE = "yyyy-MM-dd'T'HH:mm:ssXXX";

  32.     /**
  33.      * Returns the current date and time considering the configured timezone.
  34.      *
  35.      * @return Timezone aware Date
  36.      */
  37.     default Date nowAsDate() {
  38.         return Date.from(ZonedDateTime.of(LocalDateTime.now(), ZoneId.systemDefault()).toInstant());
  39.     }

  40.     /**
  41.      * Returns the current date and time considering the configured timezone.
  42.      *
  43.      * @return Timezone aware DateTime
  44.      */
  45.     default ZonedDateTime nowAsZonedDateTime() {
  46.         return nowAsZonedDateTime(ZoneId.systemDefault());
  47.     }

  48.     /**
  49.      * Returns the current date and time considering the configured timezone.
  50.      *
  51.      * @param zoneId ZoneId
  52.      * @return Timezone aware DateTime
  53.      */
  54.     default ZonedDateTime nowAsZonedDateTime(ZoneId zoneId) {
  55.         return ZonedDateTime.of(LocalDateTime.now(), zoneId);
  56.     }

  57.     /**
  58.      * Returns the current date and time of the system considering the configured timezone.
  59.      *
  60.      * @return Timezone aware Date
  61.      */
  62.     default Instant now() {
  63.         return ZonedDateTime.of(LocalDateTime.now(), ZoneId.systemDefault()).toInstant();
  64.     }
  65. }