Skip to content

Commit eea0459

Browse files
committed
Chapter on Date
1 parent 4d5a97e commit eea0459

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ Make them do one. -->
529529
- [Time Zones](./time/time_zones.md)
530530
- [ZonedDateTime](./time/zoned_date_time.md)
531531
- [OffsetDateTime](./time/offset_date_time.md)
532+
- [Date](./time/date.md)
532533
- [ArrayList](./array_list.md)
533534
- [Ubiquity](./array_list/ubiquity.md)
534535
- [Add an item](./array_list/add_an_item.md)

src/time/date.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,59 @@
11
# Date
2+
3+
There is one class related to time that isn't much like the others: `java.util.Date`.
4+
5+
Java did not originally come with the `java.time` classes. At first it just had `Date`.
6+
7+
```java
8+
import java.util.Date;
9+
10+
class Main {
11+
void main() {
12+
Date date = new Date();
13+
System.out.println(date);
14+
}
15+
}
16+
```
17+
18+
`Date` is somewhat of a chimera between `Instant` and `ZonedDateTime`. It represents an instant in time but specifically in the UTC timezone.[^gmt]
19+
20+
It is important to know about because a lot of code, including code that comes with Java, makes use of it.
21+
22+
Whenever you see `Date` in the wild you should usually turn it into an `Instant` by
23+
calling `.toInstant()`.
24+
25+
```java
26+
import java.time.Instant;
27+
import java.util.Date;
28+
29+
class Main {
30+
void main() {
31+
Date date = new Date();
32+
System.out.println(date);
33+
34+
Instant instant = date.toInstant();
35+
System.out.println(instant);
36+
}
37+
}
38+
```
39+
40+
You can also construct a `Date` from an `Instant` using `Date.from`. This is useful if there is some code that wants a `Date` as an argument.
41+
42+
```java
43+
import java.time.Instant;
44+
import java.util.Date;
45+
46+
class Main {
47+
void main() {
48+
var instant = Instant.now();
49+
System.out.println(instant);
50+
51+
Date date = Date.from(instant);
52+
System.out.println(date);
53+
}
54+
}
55+
```
56+
57+
To be clear though, `Date` has problems. We aren't ready to explain all of them yet. Just treat `Date` as haunted, as in by ghosts, and use the `java.time` alternatives when you can.
58+
59+
[^gmt]: You will notice that when we print out the date we get GMT. GMT is basically the same as UTC, though [the documentation for `Date`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Date.html) explains the difference.

src/time/local_date.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ You can make a `LocalDate` with `LocalDate.of`.
1212
import java.time.LocalDate;
1313

1414
void main() {
15-
var jan10 = LocalDate.of(2024, 10, 1);
15+
var jan10 = LocalDate.of(2024, 1, 10);
1616
System.out.println(jan10);
1717
}
1818
```

src/time/local_date_time.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import java.time.LocalDateTime;
1414

1515
class Main {
1616
void main() {
17-
var jan10 = LocalDate.of(2024, 10, 1);
17+
var jan10 = LocalDate.of(2024, 1, 10);
1818
var tenTwentyFour = LocalTime.of(10, 24, 0);
1919
System.out.println(LocalDateTime.of(jan10, tenTwentyFour));
2020
}

src/time/zoned_date_time.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import java.time.ZoneId;
1515

1616
class Main {
1717
void main() {
18-
var jan10 = LocalDate.of(2024, 10, 1);
18+
var jan10 = LocalDate.of(2024, 1, 10);
1919
var tenTwentyFour = LocalTime.of(10, 24, 0);
2020
var est = ZoneId.of("US/Eastern");
2121

0 commit comments

Comments
 (0)