Java
Date and Time
Java 8+ Date-Time API.
By EZ4Code Team
datetimedate
Code
import java.time.*;
import java.time.format.*;
// Current date-time
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.now();
ZonedDateTime zoned = ZonedDateTime.now();
// Create specific date
LocalDate birthday = LocalDate.of(2000, 1, 1);
// Date arithmetic
LocalDate nextWeek = date.plusWeeks(1);
LocalDate lastMonth = date.minusMonths(1);
// Format
String formatted = dateTime.format(
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// Parse
LocalDate parsed = LocalDate.parse("2024-01-15");
// Duration and Period
Duration duration = Duration.between(time, LocalTime.MIDNIGHT);
Period period = Period.between(birthday, date);Explanation
The java.time package provides immutable, thread-safe date-time classes.
More Java Snippets
String Operations and StringBuilder
Manipulate strings with split, join, substring, and StringBuilder in Java.
Stream API
Process collections using the Stream API.
Lambda Expressions
Simplify code with Lambda expressions.
Optional
Handle null values elegantly.
Collection Operations
Common operations on List, Set, and Map.
Exception Handling
try-catch-finally and custom exceptions.