Skip to content

Commit 07b4471

Browse files
committed
DOCSP-30481: replace date with LocalDateTime (#108)
# Pull Request Info [PR Reviewing Guidelines](https://github.com/mongodb/docs-java/blob/master/REVIEWING.md) JIRA - https://jira.mongodb.org/browse/DOCSP-30481 Staging: - [Documents](https://docs-mongodbcom-staging.corp.mongodb.com/kotlin/docsworker-xlarge/DOCSP-30481-localdatetime/fundamentals/data-formats/documents/) - [Extended JSON](https://docs-mongodbcom-staging.corp.mongodb.com/kotlin/docsworker-xlarge/DOCSP-30481-localdatetime/fundamentals/data-formats/document-data-format-extended-json/) ## Self-Review Checklist - [ ] Is this free of any warnings or errors in the RST? - [ ] Did you run a spell-check? - [ ] Did you run a grammar-check? - [ ] Are all the links working? (cherry picked from commit baabb3c)
1 parent 26df973 commit 07b4471

10 files changed

+26
-32
lines changed

examples/src/test/kotlin/DocumentsTest.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import org.bson.types.ObjectId
1111
import org.junit.jupiter.api.AfterAll
1212
import org.junit.jupiter.api.Assertions.*
1313
import org.junit.jupiter.api.TestInstance
14-
import java.time.LocalDate
14+
import java.time.Instant
15+
import java.time.LocalDateTime
1516
import java.time.ZoneId
17+
import java.time.ZoneOffset
1618
import java.util.*
1719
import kotlin.test.*
1820

@@ -40,10 +42,7 @@ internal class DocumentsTest {
4042
.append("name", "Gabriel García Márquez")
4143
.append(
4244
"dateOfDeath",
43-
Date.from(
44-
LocalDate.of(2014, 4, 17)
45-
.atStartOfDay(ZoneId.systemDefault()).toInstant()
46-
)
45+
LocalDateTime.of(2014, 4, 17, 4, 0)
4746
)
4847
.append(
4948
"novels", listOf(
@@ -96,8 +95,7 @@ internal class DocumentsTest {
9695
.append(
9796
"dateOfDeath",
9897
BsonDateTime(
99-
LocalDate.of(2014, 4, 17)
100-
.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()
98+
LocalDateTime.of(2014, 4, 17, 0, 0).atZone(ZoneId.of("America/New_York")).toInstant().toEpochMilli()
10199
)
102100
)
103101
.append(
@@ -135,7 +133,7 @@ internal class DocumentsTest {
135133

136134
val doc = collection.find(Filters.eq("name", "Gabriel García Márquez")).firstOrNull()
137135
doc?.let {
138-
println("_id: ${it.getObjectId("_id").value}, name: ${it.getString("name").value}, dateOfDeath: ${Date(it.getDateTime("dateOfDeath").value)}")
136+
println("_id: ${it.getObjectId("_id").value}, name: ${it.getString("name").value}, dateOfDeath: ${Instant.ofEpochMilli(it.getDateTime("dateOfDeath").value).atZone(ZoneId.of("America/New_York")).toLocalDateTime()}")
139137

140138
it.getArray("novels").forEach { novel ->
141139
val novelDocument = novel.asDocument()

examples/src/test/kotlin/EjsonTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import org.junit.jupiter.api.Assertions.*
99
import org.junit.jupiter.api.TestInstance
1010
import java.io.BufferedWriter
1111
import java.io.OutputStreamWriter
12-
import java.time.Instant
13-
import java.time.ZoneOffset
12+
import java.time.*
1413
import java.time.format.DateTimeFormatter
1514
import java.util.*
1615
import kotlin.test.*
1716

17+
1818
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
1919
internal class EjsonTest {
2020

@@ -94,15 +94,15 @@ internal class EjsonTest {
9494
val settings = JsonWriterSettings.builder()
9595
.outputMode(JsonMode.RELAXED)
9696
.objectIdConverter { value, writer -> writer.writeString(value.toHexString()) }
97-
.dateTimeConverter { value, writer ->
98-
val zonedDateTime = Instant.ofEpochMilli(value).atZone(ZoneOffset.UTC)
99-
writer.writeString(DateTimeFormatter.ISO_DATE_TIME.format(zonedDateTime))
97+
.timestampConverter { value, writer ->
98+
val ldt = LocalDateTime.ofInstant(Instant.ofEpochSecond(value.time.toLong()), ZoneOffset.UTC)
99+
writer.writeString(ldt.format(DateTimeFormatter.ISO_DATE_TIME))
100100
}
101101
.build()
102102

103103
val doc = Document()
104104
.append("_id", ObjectId("507f1f77bcf86cd799439012"))
105-
.append("createdAt", Date.from(Instant.ofEpochMilli(1601499609000L)))
105+
.append("createdAt", BsonTimestamp(1601516589,1))
106106
.append("myNumber", 4794261)
107107

108108
println(doc.toJson(settings))

examples/src/test/kotlin/UpdatesBuildersTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class UpdatesBuildersTest {
166166
// :snippet-start: current-timestamp-update
167167
// Create a new instance of the collection with the flexible `Document` type
168168
// to allow for the changing of the `lastModified` field to a `BsonTimestamp`
169-
// from a `Date`.
169+
// from a `LocalDateTime`.
170170
val collection = database.getCollection<Document>("paint_orders")
171171

172172
val filter = Filters.eq("_id", 1)

source/examples/generated/DocumentsTest.snippet.create-bson-document.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ val author = BsonDocument()
44
.append(
55
"dateOfDeath",
66
BsonDateTime(
7-
LocalDate.of(2014, 4, 17)
8-
.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()
7+
LocalDateTime.of(2014, 4, 17, 0, 0).atZone(ZoneId.of("America/New_York")).toInstant().toEpochMilli()
98
)
109
)
1110
.append(

source/examples/generated/DocumentsTest.snippet.create-document.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ val author = Document("_id", ObjectId())
22
.append("name", "Gabriel García Márquez")
33
.append(
44
"dateOfDeath",
5-
Date.from(
6-
LocalDate.of(2014, 4, 17)
7-
.atStartOfDay(ZoneId.systemDefault()).toInstant()
8-
)
5+
LocalDateTime.of(2014, 4, 17, 4, 0)
96
)
107
.append(
118
"novels", listOf(

source/examples/generated/DocumentsTest.snippet.retrieve-bson-document.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
val doc = collection.find(Filters.eq("name", "Gabriel García Márquez")).firstOrNull()
44
doc?.let {
5-
println("_id: ${it.getObjectId("_id").value}, name: ${it.getString("name").value}, dateOfDeath: ${Date(it.getDateTime("dateOfDeath").value)}")
5+
println("_id: ${it.getObjectId("_id").value}, name: ${it.getString("name").value}, dateOfDeath: ${Instant.ofEpochMilli(it.getDateTime("dateOfDeath").value).atZone(ZoneId.of("America/New_York")).toLocalDateTime()}")
66

77
it.getArray("novels").forEach { novel ->
88
val novelDocument = novel.asDocument()
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
val settings = JsonWriterSettings.builder()
22
.outputMode(JsonMode.RELAXED)
33
.objectIdConverter { value, writer -> writer.writeString(value.toHexString()) }
4-
.dateTimeConverter { value, writer ->
5-
val zonedDateTime = Instant.ofEpochMilli(value).atZone(ZoneOffset.UTC)
6-
writer.writeString(DateTimeFormatter.ISO_DATE_TIME.format(zonedDateTime))
4+
.timestampConverter { value, writer ->
5+
val ldt = LocalDateTime.ofInstant(Instant.ofEpochSecond(value.time.toLong()), ZoneOffset.UTC)
6+
writer.writeString(ldt.format(DateTimeFormatter.ISO_DATE_TIME))
77
}
88
.build()
99

1010
val doc = Document()
1111
.append("_id", ObjectId("507f1f77bcf86cd799439012"))
12-
.append("createdAt", Date.from(Instant.ofEpochMilli(1601499609000L)))
12+
.append("createdAt", BsonTimestamp(1601516589,1))
1313
.append("myNumber", 4794261)
1414

1515
println(doc.toJson(settings))

source/examples/generated/UpdatesBuildersTest.snippet.current-timestamp-update.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Create a new instance of the collection with the flexible `Document` type
22
// to allow for the changing of the `lastModified` field to a `BsonTimestamp`
3-
// from a `Date`.
3+
// from a `LocalDateTime`.
44
val collection = database.getCollection<Document>("paint_orders")
55

66
val filter = Filters.eq("_id", 1)

source/fundamentals/data-formats/document-data-format-extended-json.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,14 @@ expressions, to simplify the Relaxed mode JSON output.
271271
.. output::
272272
:language: javascript
273273

274-
{"_id": "507f1f77bcf86cd799439012", "createdAt": "2020-09-30T21:00:09Z", "myNumber": 4794261}
274+
{"_id": "507f1f77bcf86cd799439012", "createdAt": "2020-10-01T01:43:09", "myNumber": 4794261}
275275

276276
// Without specifying the converters, the Relaxed mode JSON output
277277
// should look something like this:
278-
{"_id": {"$oid": "507f1f77bcf86cd799439012"}, "createdAt": {"$date": "2020-09-30T21:00:09Z"}, "myNumber": 4794261}
278+
{"_id": {"$oid": "507f1f77bcf86cd799439012"}, "createdAt": {"$timestamp": {"t": 1601516589, "i": 1}}, "myNumber": 4794261}
279279

280280
For more information about the methods and classes mentioned in this section,
281281
see the following API Documentation:
282282

283283
- `Converter <{+api+}/apidocs/bson/org/bson/json/Converter.html>`__
284-
- `JsonWriterSettings.Builder <{+api+}/apidocs/bson/org/bson/json/JsonWriterSettings.Builder.html>`__
284+
- `JsonWriterSettings.Builder <{+api+}/apidocs/bson/org/bson/json/JsonWriterSettings.Builder.html>`__

source/fundamentals/data-formats/documents.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ frequently-used BSON and Kotlin types:
8181
* - Boolean
8282
- ``kotlin.Boolean``
8383
* - Date
84-
- ``java.util.Date``
84+
- ``java.time.LocalDateTime``
8585
* - Document
8686
- ``org.bson.Document``
8787
* - Double
@@ -209,7 +209,7 @@ data from the collection using the following code:
209209
.. output::
210210
:language: none
211211

212-
_id: 5fb5fad05f734e3794741a35, name: Gabriel García Márquez, dateOfDeath: Thu Apr 17 00:00:00 EDT 2014
212+
_id: 5fb5fad05f734e3794741a35, name: Gabriel García Márquez, dateOfDeath: 2014-04-17T00:00
213213
title: One Hundred Years of Solitude, yearPublished: 1967
214214
title: Chronicle of a Death Foretold, yearPublished: 1981
215215
title: Love in the Time of Cholera, yearPublished: 1985

0 commit comments

Comments
 (0)