Package 

Class JavaUtilDateTimeAdapter


  • 
    public final class JavaUtilDateTimeAdapter
    
                        

    A Moshi JSON adapter for handling date serialization and deserialization between JSON and Java Date objects.

    This adapter provides bidirectional conversion:

    • Serialization: Converts Date objects to ISO 8601 formatted strings in UTC timezone

    • Deserialization: Converts nanosecond precision epoch timestamps (as strings) to Date objects

    The adapter handles two different date formats depending on the direction of conversion:

    • For outgoing JSON: Uses ISO 8601 format (yyyy-MM-dd'T'HH:mm:ss.SSS'Z')

    • For incoming JSON: Expects nanosecond epoch timestamps as string values

    • Method Summary

      Modifier and Type Method Description
      final String toJson(Date value) Converts a Date object to its JSON string representation.
      final Date fromJson(String value) Converts a JSON string representation to a Date object.
      • Methods inherited from class java.lang.Object

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

      • JavaUtilDateTimeAdapter

        JavaUtilDateTimeAdapter()
    • Method Detail

      • toJson

         final String toJson(Date value)

        Converts a Date object to its JSON string representation.

        Serializes the provided date using ISO 8601 format in UTC timezone. This method is automatically called by Moshi when serializing objects that contain Date fields.

        Parameters:
        value - The Date object to serialize
      • fromJson

         final Date fromJson(String value)

        Converts a JSON string representation to a Date object.

        Deserializes date values from JSON, expecting nanosecond precision epoch timestamps as string values. The method converts nanoseconds to milliseconds by dividing by 1,000,000 since Java Date uses millisecond precision.

        This method is automatically called by Moshi when deserializing JSON that contains date fields.

        Parameters:
        value - String representation of nanosecond epoch timestamp