forked from javaevolved/javaevolved.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava-time-basics.json
More file actions
58 lines (58 loc) · 2.06 KB
/
Copy pathjava-time-basics.json
File metadata and controls
58 lines (58 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{
"id": 70,
"slug": "java-time-basics",
"title": "java.time API basics",
"category": "datetime",
"difficulty": "beginner",
"jdkVersion": "8",
"oldLabel": "Pre-Java 8",
"modernLabel": "Java 8+",
"oldApproach": "Date + Calendar",
"modernApproach": "java.time.*",
"oldCode": "// Mutable, confusing, zero-indexed months\nCalendar cal = Calendar.getInstance();\ncal.set(2025, 0, 15); // January = 0!\nDate date = cal.getTime();\n// not thread-safe",
"modernCode": "LocalDate date = LocalDate.of(\n 2025, Month.JANUARY, 15);\nLocalTime time = LocalTime.of(14, 30);\nInstant now = Instant.now();\n// immutable, thread-safe",
"summary": "Use immutable, clear date/time types instead of Date and Calendar.",
"explanation": "java.time provides LocalDate, LocalTime, LocalDateTime, Instant, ZonedDateTime — all immutable and thread-safe. Months are 1-indexed. No more Calendar.JANUARY = 0 confusion.",
"whyModernWins": [
{
"icon": "🔒",
"title": "Immutable",
"desc": "Date/time values can't be accidentally modified."
},
{
"icon": "📖",
"title": "Clear API",
"desc": "Month.JANUARY, not 0. DayOfWeek.MONDAY, not 2."
},
{
"icon": "🛡️",
"title": "Thread-safe",
"desc": "No synchronization needed — share freely across threads."
}
],
"support": {
"state": "available",
"description": "Widely available since JDK 8 (March 2014)"
},
"prev": "errors/record-based-errors",
"next": "datetime/duration-and-period",
"related": [
"datetime/instant-precision",
"datetime/duration-and-period",
"datetime/date-formatting"
],
"docs": [
{
"title": "LocalDate",
"href": "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/time/LocalDate.html"
},
{
"title": "LocalTime",
"href": "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/time/LocalTime.html"
},
{
"title": "LocalDateTime",
"href": "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/time/LocalDateTime.html"
}
]
}