Class LocalDateCodec

  • All Implemented Interfaces:
    Codec<java.time.LocalDate>, Decoder<java.time.LocalDate>, Encoder<java.time.LocalDate>


    public class LocalDateCodec
    extends java.lang.Object
    LocalDate Codec.

    Encodes and decodes LocalDate objects to and from DateTime.

    Converts the LocalDate values to and from ZoneOffset.UTC.

    Note: Requires Java 8 or greater.

    Since:
    3.7
    MongoDB documentation
    reference/bson-types
    • Constructor Summary

      Constructors 
      Constructor Description
      LocalDateCodec​()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.time.LocalDate decode​(BsonReader reader, DecoderContext decoderContext)
      Decodes a BSON value from the given reader into an instance of the type parameter T.
      void encode​(BsonWriter writer, java.time.LocalDate value, EncoderContext encoderContext)
      Encode an instance of the type parameter T into a BSON value.
      java.lang.Class<java.time.LocalDate> getEncoderClass​()
      Returns the Class instance that this encodes.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • LocalDateCodec

        public LocalDateCodec​()
    • Method Detail

      • decode

        public java.time.LocalDate decode​(BsonReader reader,
                                          DecoderContext decoderContext)
        Description copied from interface: Decoder
        Decodes a BSON value from the given reader into an instance of the type parameter T.
        Parameters:
        reader - the BSON reader
        decoderContext - the decoder context
        Returns:
        an instance of the type parameter T.
      • encode

        public void encode​(BsonWriter writer,
                           java.time.LocalDate value,
                           EncoderContext encoderContext)
        Encode an instance of the type parameter T into a BSON value.

        Converts the LocalDate to ZoneOffset.UTC via LocalDate.atStartOfDay(ZoneId).

        Parameters:
        writer - the BSON writer to encode into
        value - the value to encode
        encoderContext - the encoder context
        Throws:
        CodecConfigurationException - if the LocalDate cannot be converted to a valid Bson DateTime.
      • getEncoderClass

        public java.lang.Class<java.time.LocalDate> getEncoderClass​()
        Description copied from interface: Encoder
        Returns the Class instance that this encodes. This is necessary because Java does not reify generic types.
        Returns:
        the Class instance that this encodes.