Package net.time4j

Class TemporalType<S,​T>

java.lang.Object
net.time4j.TemporalType<S,​T>
Type Parameters:
S - source type in other library
T - target type in Time4J
All Implemented Interfaces:
Converter<S,​T>
Direct Known Subclasses:
JDBCAdapter, XMLAdapter

public abstract class TemporalType<S,​T> extends Object implements Converter<S,​T>

Serves as bridge to temporal types of JDK or other date and time libraries.

All singleton instances are defined as static constants and are immutable.

Since:
2.0
Author:
Meno Hochschild
  • Field Details

    • JAVA_UTIL_DATE

      public static final TemporalType<Date,​Moment> JAVA_UTIL_DATE

      Bridge between a traditional Java timestamp of type java.util.Date and the class Moment.

      The conversion does not take into account any UTC-leapseconds. The supported value range is smaller than in the class Moment. Example:

        java.util.Date instant = new java.util.Date(86401 * 1000);
        Moment ut = TemporalType.JAVA_UTIL_DATE.translate(instant);
        System.out.println(ut);
        // output: 1970-01-02T00:00:01Z
       
      Since:
      2.0
    • MILLIS_SINCE_UNIX

      public static final TemporalType<Long,​Moment> MILLIS_SINCE_UNIX

      Bridge between a traditional Java timestamp as count of milliseconds since UNIX-epoch and the class Moment.

      The conversion does not take into account any UTC-leapseconds. The supported value range is smaller than in the class Moment. Example:

        long instant = 86401 * 1000L;
        Moment ut = TemporalType.MILLIS_SINCE_UNIX.translate(instant);
        System.out.println(ut);
        // output: 1970-01-02T00:00:01Z
       
      Since:
      2.0
    • JAVA_UTIL_CALENDAR

      public static final TemporalType<Calendar,​ZonalDateTime> JAVA_UTIL_CALENDAR

      Bridge between a traditional Java calendar of type java.util.Calendar and the class ZonalDateTime.

      The conversion tries to keep the instant and zone data involved. A change of the local timestamp part of Calendar is possible, however. This concerns the conversion of any non-gregorian calendar.

      Since:
      3.37/4.32
    • JAVA_UTIL_TIMEZONE

      public static final TemporalType<TimeZone,​Timezone> JAVA_UTIL_TIMEZONE

      Bridge between a traditional Java timezone of type java.util.TimeZone and the class net.time4j.tz.Timezone.

      The conversion tries to keep the data and rules of the zone to be converted.

      Since:
      3.37/4.32
    • LOCAL_DATE

      public static final TemporalType<LocalDate,​PlainDate> LOCAL_DATE

      Bridge between the JSR-310-class java.time.LocalDate and the class PlainDate.

      The conversion is always exact. Example:

        PlainDate date = TemporalType.LOCAL_DATE.translate(LocalDate.of(2015, 4, 30));
        System.out.println(date);
        // output: 2015-04-30
       
      Since:
      4.0
    • LOCAL_TIME

      public static final TemporalType<LocalTime,​PlainTime> LOCAL_TIME

      Bridge between the JSR-310-class java.time.LocalTime and the class PlainTime.

      The conversion is exact with the exception of midnight at end of day (T24:00). The special time T24:00 will be mapped to 00:00 in class LocalTime. Example:

        PlainTime time = TemporalType.LOCAL_TIME.translate(LocalTime.of(17, 45));
        System.out.println(time);
        // output: T17:45
       
      Since:
      4.0
    • LOCAL_DATE_TIME

      public static final TemporalType<LocalDateTime,​PlainTimestamp> LOCAL_DATE_TIME

      Bridge between the JSR-310-class java.time.LocalDateTime and the class PlainTimestamp.

      The conversion is always exact. Example:

        PlainTimestamp tsp = TemporalType.LOCAL_DATE_TIME.translate(LocalDateTime.of(2015, 4, 30, 17, 45));
        System.out.println(tsp);
        // output: 2015-04-30T17:45
       
      Since:
      4.0
    • INSTANT

      public static final TemporalType<Instant,​Moment> INSTANT

      Bridge between the JSR-310-class java.time.Instant and the class Moment.

      The conversion is usually exact. However, leap seconds will always be ignored. The outer value range limits of the class Moment are a tiny bit smaller. Example:

        Moment moment = TemporalType.INSTANT.translate(Instant.ofEpochSecond(86401, 450_000_000));
        System.out.println(moment);
        // output: 1970-01-02T00:00:01,450000000Z
       
      Since:
      4.0
    • ZONED_DATE_TIME

      public static final TemporalType<ZonedDateTime,​ZonalDateTime> ZONED_DATE_TIME

      Bridge between the JSR-310-class java.time.ZonedDateTime and the class ZonalDateTime.

      The conversion is usually exact. However, leap seconds will always be ignored. The outer value range limits of the class ZonalDateTime are a tiny bit different. Example:

        Moment moment = TemporalType.ZONED_DATE_TIME.translate(Instant.ofEpochSecond(86401, 450_000_000));
        System.out.println(moment);
        // Ausgabe: 1970-01-02T00:00:01,450000000Z
       
      Since:
      4.0
    • THREETEN_DURATION

      public static final TemporalType<Duration,​Duration<ClockUnit>> THREETEN_DURATION

      Bridge between the JSR-310-class java.time.Duration and the class net.time4j.Duration.

      The conversion is usually exact but will always perform a normalization on the side of Time4J. Example:

        Duration<ClockUnit> duration = TemporalType.THREETEN_DURATION.translate(java.time.Duration.ofSeconds(65));
        System.out.println(duration);
        // output: PT1M5S
       
      Since:
      4.0
    • THREETEN_PERIOD

      public static final TemporalType<Period,​Duration<CalendarUnit>> THREETEN_PERIOD

      Bridge between the JSR-310-class java.time.Period and the class net.time4j.Duration.

      Note that mixed signs in original period like "P1M-30D" will be rejected by Time4J. This is a normalizing conversion. Example for a correct input:

        Duration<CalendarUnit> duration = TemporalType.THREETEN_PERIOD.translate(Period.of(3, 13, 45));
        System.out.println(duration);
        // output: P4Y1M45D
       

      Note: The algorithm to apply a negative duration is slightly different. Example:

           System.out.println(
             LocalDate.of(2015, 7, 1).minus(Period.of(0, 1, 1))); // 2015-05-31
           System.out.println(
             PlainDate.of(2015, 7, 1).minus(Duration.ofCalendarUnits(0, 1, 1))); // 2015-05-30
       
      Since:
      4.0
    • CLOCK

      public static final TemporalType<Clock,​TimeSource<?>> CLOCK

      Bridge between the JSR-310-class java.time.Clock and the interface net.time4j.base.TimeSource.

      The conversion will always ignore leap seconds and initially use ZoneId.systemDefault().

      Since:
      4.0
  • Method Details

    • translate

      public abstract T translate(S source)

      Converts the external type to a type in Time4J.

      Specified by:
      translate in interface Converter<S,​T>
      Parameters:
      source - external object
      Returns:
      translated Time4J-object
      Throws:
      ArithmeticException - in case of numerical overflow
      ChronoException - if conversion fails
      Since:
      2.0
    • from

      public abstract S from(T time4j)

      Converts the Time4J-type to an external type.

      Specified by:
      from in interface Converter<S,​T>
      Parameters:
      time4j - Time4J-object
      Returns:
      translated object of external type
      Throws:
      ArithmeticException - in case of numerical overflow
      ChronoException - if conversion fails
      Since:
      2.0