ServerTimestamp
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = [ElementType.METHOD, ElementType.FIELD])
annotationServerTimestamp
Annotation used to mark a timestamp field to be populated with a server timestamp. If a POJO being written containsnull for a @ServerTimestamp-annotated field, it will be replaced with a server-generated timestamp.
Kotlin Note
When applying this annotation to a property of a Kotlin class, the@get use-site target should always be used. There is no need to use the@set use-site target as this annotation isonly considered whenwriting instances into Firestore, and is ignored whenreading instances from Firestore. Here is an example of a class that can both be written into and read from Firestore whosefoo property will be populated with the server timestamp in Firestore if its value is null:
dataclassPojo(@get:ServerTimestampvarfoo:Timestamp?=null){constructor():this(null)// Used by Firestore to create new instances}
If the class only needs to bewritten into Firestore (and not read from Firestore) then the class can be simplified as follows:
dataclassPojo(@get:ServerTimestampvalfoo:Timestamp?=null)
var can be tightened toval and the secondary no-argument constructor can be omitted.Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-07-21 UTC.