Java 8 introduced a new date and time API in the package java.time
that provides a comprehensive way to handle dates and times with significant improvements over the previous java.util.Date
and java.util.Calendar
classes. This new API is built on ISO standards, making it more intuitive and easier to use.
java.time.ZoneId
class for better time zone management.DateTimeFormatter
class is introduced for handling date and time formatting.Duration
and Period
classes.
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeExample {
public static void main(String[] args) {
// Current date
LocalDate currentDate = LocalDate.now();
System.out.println("Current Date: " + currentDate);
// Format the date
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String formattedDate = currentDate.format(formatter);
System.out.println("Formatted Date: " + formattedDate);
// Current date and time
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current Date and Time: " + currentDateTime);
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?