Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
IT NachrichtenDNA-Computer rechnet ohne Strom und bricht Tempo-Rekord(23.09.2026 um 13:39 Uhr)
IT NachrichtenApple arbeitet an Fitnesstracker ohne Display(23.09.2026 um 12:37 Uhr)
IT NachrichtenDiese Serien scheiterten mit Staffel 2(23.09.2026 um 13:20 Uhr)
IT NachrichtenDNA-Computer rechnet ohne Strom und bricht Tempo-Rekord(23.09.2026 um 13:39 Uhr)
IT NachrichtenApple arbeitet an Fitnesstracker ohne Display(23.09.2026 um 12:37 Uhr)
IT NachrichtenDiese Serien scheiterten mit Staffel 2(23.09.2026 um 13:20 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Mutable & Immutable Java DateTime API

Mutable DateTime Classes In the java.util package, many classes are mutable, meaning their internal state can change after creation. If multiple threads share the same instance, changes made in one thread can unexpectedly impact others,…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!




Mutable DateTime Classes



In the java.util package, many classes are mutable, meaning their internal state can change after creation. If multiple threads share the same instance, changes made in one thread can unexpectedly impact others, leading to bugs. These problems drove the creation of immutable classes in the java.time package introduced in Java 8.




import java.util.Date;

public class MutableDateExample {
public static void main(String[] args) {
Date sharedDate = new Date(); // Initial date

Runnable task1 = () -> {
sharedDate.setYear(2025 - 1900); // Mutate the date (Deprecated method)
System.out.println("Task 1: " + sharedDate);
};

Runnable task2 = () -> {
sharedDate.setMonth(5); // Mutate the month
System.out.println("Task 2: " + sharedDate);
};

new Thread(task1).start();
new Thread(task2).start();
}
}









Issues with Mutable Date/Time Classes



Concurrency Issues: In the example above, both tasks modify the sharedDate object concurrently. This can lead to unpredictable results because Date is mutable and not thread-safe.



Data Integrity: Modifications in one part of the code can unexpectedly affect other parts where the same date object is used, leading to incorrect data or logic errors.



Mutable class: java.util.Date, java.util.Calendar, java.util.GregorianCalendar, java.text.SimpleDateFormat, java.util.TimeZone, java.util.Locale






Immutable DateTime






java.time Package (Introduced in Java 8)



The java.time API is designed to be safe and unchangeable. Its classes are immutable, meaning once you create an object, it can't be changed. To update a date or time, you create a new object with the updated value instead of changing the original one.




  • Key Classes



    • LocalDate, LocalTime, LocalDateTime: Represent date and time without time zone information.


    • ZonedDateTime, OffsetDateTime, OffsetTime: Represent date and time with time zone or offset information.


    • Instant: Represents a specific moment in time (UTC).


    • Duration, Period: Represent time-based and date-based amounts of time.









LocalDate initialDate = LocalDate.of(2024, 8, 21); // Initial date

// Create a new date by adding 5 days
LocalDate updatedDate = initialDate.plusDays(5);

// Print the initial and updated dates
System.out.println("Initial Date: " + initialDate);
System.out.println("Updated Date: " + updatedDate);

// Print the memory addresses of the initial and updated dates
System.out.println("Initial Date Address: " + System.identityHashCode(initialDate));
System.out.println("Updated Date Address: " + System.identityHashCode(updatedDate));

// example output
// Initial Date: 2024-08-21
// Updated Date: 2024-08-26
// Initial Date Address: 1555845260
// Updated Date Address: 1590550415









Summary:




  • Mutable Classes (java.util.Date, java.util.Calendar): Prone to issues such as concurrency problems, unintended side effects, and historical bugs. Suitable only for legacy code or specific scenarios where immutability is not a concern.

  • Immutable Classes (java.time): Provide thread safety, predictable behavior, and overall better design. They should be preferred for new codebases and when working with modern Java applications.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Mutable & Immutable Java DateTime API

Thematisch verwandte Begriffe: Mutable, Immutable, Java, DateTime · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-19438 | Improper Limitation of a Pathname to a Restricted Directory ('Path Trave…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick